Skip to content

Commit f8fbef7

Browse files
authored
Change LIKE operator case-insensitive match (#1160)
Signed-off-by: Peng Huo <penghuo@gmail.com>
1 parent c5e8fc0 commit f8fbef7

5 files changed

Lines changed: 11 additions & 8 deletions

File tree

core/src/main/java/org/opensearch/sql/utils/OperatorUtils.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,9 @@ public class OperatorUtils {
2222
* @return if text matches pattern returns true; else return false.
2323
*/
2424
public static ExprBooleanValue matches(ExprValue text, ExprValue pattern) {
25-
return ExprBooleanValue
26-
.of(Pattern.compile(patternToRegex(pattern.stringValue())).matcher(text.stringValue())
25+
return ExprBooleanValue.of(
26+
Pattern.compile(patternToRegex(pattern.stringValue()), Pattern.CASE_INSENSITIVE)
27+
.matcher(text.stringValue())
2728
.matches());
2829
}
2930

docs/user/dql/expressions.rst

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -149,12 +149,12 @@ Here is an example for different type of comparison operators::
149149
LIKE
150150
----
151151

152-
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::
152+
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::
153153

154-
os> SELECT 'axyzb' LIKE 'a%b', 'acb' LIKE 'a_b', 'axyzb' NOT LIKE 'a%b', 'acb' NOT LIKE 'a_b';
154+
os> SELECT 'axyzb' LIKE 'a%b', 'acb' LIKE 'A_B', 'axyzb' NOT LIKE 'a%b', 'acb' NOT LIKE 'a_b';
155155
fetched rows / total rows = 1/1
156156
+----------------------+--------------------+--------------------------+------------------------+
157-
| 'axyzb' LIKE 'a%b' | 'acb' LIKE 'a_b' | 'axyzb' NOT LIKE 'a%b' | 'acb' NOT LIKE 'a_b' |
157+
| 'axyzb' LIKE 'a%b' | 'acb' LIKE 'A_B' | 'axyzb' NOT LIKE 'a%b' | 'acb' NOT LIKE 'a_b' |
158158
|----------------------+--------------------+--------------------------+------------------------|
159159
| True | True | False | False |
160160
+----------------------+--------------------+--------------------------+------------------------+

docs/user/ppl/functions/string.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ LIKE
8787
Description
8888
>>>>>>>>>>>
8989

90-
Usage: like(string, PATTERN) return true if the string match the PATTERN.
90+
Usage: like(string, PATTERN) return true if the string match the PATTERN, PATTERN is case insensitive.
9191

9292
There are two wildcards often used in conjunction with the LIKE operator:
9393

@@ -96,7 +96,7 @@ There are two wildcards often used in conjunction with the LIKE operator:
9696

9797
Example::
9898

99-
os> source=people | eval `LIKE('hello world', '_ello%')` = LIKE('hello world', '_ello%') | fields `LIKE('hello world', '_ello%')`
99+
os> source=people | eval `LIKE('hello world', '_ello%')` = LIKE('hello world', '_ELLO%') | fields `LIKE('hello world', '_ello%')`
100100
fetched rows / total rows = 1/1
101101
+---------------------------------+
102102
| LIKE('hello world', '_ello%') |

opensearch/src/main/java/org/opensearch/sql/opensearch/storage/script/filter/lucene/LikeQuery.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,6 @@ public QueryBuilder build(FunctionExpression func) {
3232
*/
3333
protected WildcardQueryBuilder createBuilder(String field, String query) {
3434
String matchText = StringUtils.convertSqlWildcardToLucene(query);
35-
return QueryBuilders.wildcardQuery(field, matchText);
35+
return QueryBuilders.wildcardQuery(field, matchText).caseInsensitive(true);
3636
}
3737
}

opensearch/src/test/java/org/opensearch/sql/opensearch/storage/script/filter/FilterQueryBuilderTest.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,7 @@ void should_build_wildcard_query_for_like_expression() {
132132
+ " \"wildcard\" : {\n"
133133
+ " \"name\" : {\n"
134134
+ " \"wildcard\" : \"*John?\",\n"
135+
+ " \"case_insensitive\" : true,\n"
135136
+ " \"boost\" : 1.0\n"
136137
+ " }\n"
137138
+ " }\n"
@@ -282,6 +283,7 @@ void should_use_keyword_for_multi_field_in_like_expression() {
282283
+ " \"wildcard\" : {\n"
283284
+ " \"name.keyword\" : {\n"
284285
+ " \"wildcard\" : \"John*\",\n"
286+
+ " \"case_insensitive\" : true,\n"
285287
+ " \"boost\" : 1.0\n"
286288
+ " }\n"
287289
+ " }\n"

0 commit comments

Comments
 (0)