diff --git a/core/src/main/java/org/opensearch/sql/utils/OperatorUtils.java b/core/src/main/java/org/opensearch/sql/utils/OperatorUtils.java index 26a21251eb..f4ece6a190 100644 --- a/core/src/main/java/org/opensearch/sql/utils/OperatorUtils.java +++ b/core/src/main/java/org/opensearch/sql/utils/OperatorUtils.java @@ -22,8 +22,9 @@ public class OperatorUtils { * @return if text matches pattern returns true; else return false. */ public static ExprBooleanValue matches(ExprValue text, ExprValue pattern) { - return ExprBooleanValue - .of(Pattern.compile(patternToRegex(pattern.stringValue())).matcher(text.stringValue()) + return ExprBooleanValue.of( + Pattern.compile(patternToRegex(pattern.stringValue()), Pattern.CASE_INSENSITIVE) + .matcher(text.stringValue()) .matches()); } diff --git a/docs/user/dql/expressions.rst b/docs/user/dql/expressions.rst index a167ce29b5..89fcd7f0af 100644 --- a/docs/user/dql/expressions.rst +++ b/docs/user/dql/expressions.rst @@ -149,12 +149,12 @@ Here is an example for different type of comparison operators:: LIKE ---- -expr LIKE pattern. The expr is string value, pattern is supports literal text, a percent ( % ) character for a wildcard, and an underscore ( _ ) character for a single character match:: +expr LIKE pattern. The expr is string value, pattern is supports literal text, a percent ( % ) character for a wildcard, and an underscore ( _ ) character for a single character match, pattern is case insensitive:: - os> SELECT 'axyzb' LIKE 'a%b', 'acb' LIKE 'a_b', 'axyzb' NOT LIKE 'a%b', 'acb' NOT LIKE 'a_b'; + os> SELECT 'axyzb' LIKE 'a%b', 'acb' LIKE 'A_B', 'axyzb' NOT LIKE 'a%b', 'acb' NOT LIKE 'a_b'; fetched rows / total rows = 1/1 +----------------------+--------------------+--------------------------+------------------------+ - | 'axyzb' LIKE 'a%b' | 'acb' LIKE 'a_b' | 'axyzb' NOT LIKE 'a%b' | 'acb' NOT LIKE 'a_b' | + | 'axyzb' LIKE 'a%b' | 'acb' LIKE 'A_B' | 'axyzb' NOT LIKE 'a%b' | 'acb' NOT LIKE 'a_b' | |----------------------+--------------------+--------------------------+------------------------| | True | True | False | False | +----------------------+--------------------+--------------------------+------------------------+ diff --git a/docs/user/ppl/functions/string.rst b/docs/user/ppl/functions/string.rst index 361bc2ef37..0503759cbd 100644 --- a/docs/user/ppl/functions/string.rst +++ b/docs/user/ppl/functions/string.rst @@ -87,7 +87,7 @@ LIKE Description >>>>>>>>>>> -Usage: like(string, PATTERN) return true if the string match the PATTERN. +Usage: like(string, PATTERN) return true if the string match the PATTERN, PATTERN is case insensitive. There are two wildcards often used in conjunction with the LIKE operator: @@ -96,7 +96,7 @@ There are two wildcards often used in conjunction with the LIKE operator: Example:: - os> source=people | eval `LIKE('hello world', '_ello%')` = LIKE('hello world', '_ello%') | fields `LIKE('hello world', '_ello%')` + os> source=people | eval `LIKE('hello world', '_ello%')` = LIKE('hello world', '_ELLO%') | fields `LIKE('hello world', '_ello%')` fetched rows / total rows = 1/1 +---------------------------------+ | LIKE('hello world', '_ello%') | diff --git a/opensearch/src/main/java/org/opensearch/sql/opensearch/storage/script/filter/lucene/LikeQuery.java b/opensearch/src/main/java/org/opensearch/sql/opensearch/storage/script/filter/lucene/LikeQuery.java index dbf0e5544c..ad47ba437a 100644 --- a/opensearch/src/main/java/org/opensearch/sql/opensearch/storage/script/filter/lucene/LikeQuery.java +++ b/opensearch/src/main/java/org/opensearch/sql/opensearch/storage/script/filter/lucene/LikeQuery.java @@ -32,6 +32,6 @@ public QueryBuilder build(FunctionExpression func) { */ protected WildcardQueryBuilder createBuilder(String field, String query) { String matchText = StringUtils.convertSqlWildcardToLucene(query); - return QueryBuilders.wildcardQuery(field, matchText); + return QueryBuilders.wildcardQuery(field, matchText).caseInsensitive(true); } } diff --git a/opensearch/src/test/java/org/opensearch/sql/opensearch/storage/script/filter/FilterQueryBuilderTest.java b/opensearch/src/test/java/org/opensearch/sql/opensearch/storage/script/filter/FilterQueryBuilderTest.java index cea4e2488a..f3e4fe7afd 100644 --- a/opensearch/src/test/java/org/opensearch/sql/opensearch/storage/script/filter/FilterQueryBuilderTest.java +++ b/opensearch/src/test/java/org/opensearch/sql/opensearch/storage/script/filter/FilterQueryBuilderTest.java @@ -132,6 +132,7 @@ void should_build_wildcard_query_for_like_expression() { + " \"wildcard\" : {\n" + " \"name\" : {\n" + " \"wildcard\" : \"*John?\",\n" + + " \"case_insensitive\" : true,\n" + " \"boost\" : 1.0\n" + " }\n" + " }\n" @@ -282,6 +283,7 @@ void should_use_keyword_for_multi_field_in_like_expression() { + " \"wildcard\" : {\n" + " \"name.keyword\" : {\n" + " \"wildcard\" : \"John*\",\n" + + " \"case_insensitive\" : true,\n" + " \"boost\" : 1.0\n" + " }\n" + " }\n"