Skip to content

Commit

Permalink
Change LIKE operator case-insensitive match (#1160)
Browse files Browse the repository at this point in the history
Signed-off-by: Peng Huo <penghuo@gmail.com>
  • Loading branch information
penghuo authored Dec 12, 2022
1 parent c5e8fc0 commit f8fbef7
Show file tree
Hide file tree
Showing 5 changed files with 11 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}

Expand Down
6 changes: 3 additions & 3 deletions docs/user/dql/expressions.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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 |
+----------------------+--------------------+--------------------------+------------------------+
Expand Down
4 changes: 2 additions & 2 deletions docs/user/ppl/functions/string.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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:

Expand All @@ -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%') |
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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"
Expand Down

0 comments on commit f8fbef7

Please sign in to comment.