Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change LIKE operator case-insensitive match #1160

Merged
merged 2 commits into from
Dec 12, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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