-
Notifications
You must be signed in to change notification settings - Fork 0
Add support for wildcard_query function #156
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
Changes from 1 commit
a96a740
885f04c
4d0dadb
9c25e66
efe0ee5
bedf662
afbeb44
e25d4da
84fcf88
4708ae2
e05e5c5
988b40b
5c225ce
0b9752c
a2ca906
68c97d6
a873b27
518c09f
1101ca3
fc8883c
ae46f96
a9f5be7
bba63ce
8b7b442
4af9eef
57aac8e
0a4af9d
288e29c
1fb4973
34ffe13
eed3294
86fa737
b54a934
fede123
9da6140
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
/* | ||
* Copyright OpenSearch Contributors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package org.opensearch.sql.opensearch.storage.script.filter.lucene; | ||
|
||
import org.opensearch.index.query.QueryBuilder; | ||
import org.opensearch.index.query.QueryBuilders; | ||
import org.opensearch.index.query.WildcardQueryBuilder; | ||
import org.opensearch.sql.common.utils.StringUtils; | ||
import org.opensearch.sql.data.model.ExprValue; | ||
import org.opensearch.sql.expression.Expression; | ||
import org.opensearch.sql.expression.FunctionExpression; | ||
import org.opensearch.sql.expression.ReferenceExpression; | ||
|
||
public class LikeQuery extends LuceneQuery { | ||
@Override | ||
public QueryBuilder build(FunctionExpression func) { | ||
ReferenceExpression ref = (ReferenceExpression) func.getArguments().get(0); | ||
Expression expr = func.getArguments().get(1); | ||
ExprValue literalValue = expr.valueOf(); | ||
|
||
return createBuilder(ref.toString(), literalValue.stringValue()); | ||
} | ||
|
||
protected WildcardQueryBuilder createBuilder(String field, String query) { | ||
String matchText = StringUtils.convertSqlWildcardToLucene(query); | ||
return QueryBuilders.wildcardQuery(field, matchText); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -18,6 +18,7 @@ | |
import org.junit.jupiter.params.ParameterizedTest; | ||
import org.junit.jupiter.params.provider.MethodSource; | ||
import org.opensearch.sql.common.antlr.SyntaxCheckException; | ||
import org.opensearch.sql.common.utils.StringUtils; | ||
import org.opensearch.sql.data.model.ExprValue; | ||
import org.opensearch.sql.data.type.ExprType; | ||
import org.opensearch.sql.exception.SemanticCheckException; | ||
|
@@ -77,19 +78,19 @@ public void test_SemanticCheckException_when_invalid_parameter() { | |
|
||
@Test | ||
public void test_escaping_sql_wildcards() { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. A better home for this test is in the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks. Addressed in b54a934 |
||
assertEquals("%", wildcardQueryQuery.convertSqlWildcardToLucene("\\%")); | ||
assertEquals("\\*", wildcardQueryQuery.convertSqlWildcardToLucene("\\*")); | ||
assertEquals("_", wildcardQueryQuery.convertSqlWildcardToLucene("\\_")); | ||
assertEquals("\\?", wildcardQueryQuery.convertSqlWildcardToLucene("\\?")); | ||
assertEquals("%*", wildcardQueryQuery.convertSqlWildcardToLucene("\\%%")); | ||
assertEquals("*%", wildcardQueryQuery.convertSqlWildcardToLucene("%\\%")); | ||
assertEquals("%*%", wildcardQueryQuery.convertSqlWildcardToLucene("\\%%\\%")); | ||
assertEquals("*%*", wildcardQueryQuery.convertSqlWildcardToLucene("%\\%%")); | ||
assertEquals("_?", wildcardQueryQuery.convertSqlWildcardToLucene("\\__")); | ||
assertEquals("?_", wildcardQueryQuery.convertSqlWildcardToLucene("_\\_")); | ||
assertEquals("_?_", wildcardQueryQuery.convertSqlWildcardToLucene("\\__\\_")); | ||
assertEquals("?_?", wildcardQueryQuery.convertSqlWildcardToLucene("_\\__")); | ||
assertEquals("%\\*_\\?", wildcardQueryQuery.convertSqlWildcardToLucene("\\%\\*\\_\\?")); | ||
assertEquals("%", StringUtils.convertSqlWildcardToLucene("\\%")); | ||
assertEquals("\\*", StringUtils.convertSqlWildcardToLucene("\\*")); | ||
assertEquals("_", StringUtils.convertSqlWildcardToLucene("\\_")); | ||
assertEquals("\\?", StringUtils.convertSqlWildcardToLucene("\\?")); | ||
assertEquals("%*", StringUtils.convertSqlWildcardToLucene("\\%%")); | ||
assertEquals("*%", StringUtils.convertSqlWildcardToLucene("%\\%")); | ||
assertEquals("%*%", StringUtils.convertSqlWildcardToLucene("\\%%\\%")); | ||
assertEquals("*%*", StringUtils.convertSqlWildcardToLucene("%\\%%")); | ||
assertEquals("_?", StringUtils.convertSqlWildcardToLucene("\\__")); | ||
assertEquals("?_", StringUtils.convertSqlWildcardToLucene("_\\_")); | ||
assertEquals("_?_", StringUtils.convertSqlWildcardToLucene("\\__\\_")); | ||
assertEquals("?_?", StringUtils.convertSqlWildcardToLucene("_\\__")); | ||
assertEquals("%\\*_\\?", StringUtils.convertSqlWildcardToLucene("\\%\\*\\_\\?")); | ||
} | ||
|
||
private class WildcardQueryExpression extends FunctionExpression { | ||
|
Uh oh!
There was an error while loading. Please reload this page.