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

Add support for wildcard_query function #156

Merged
merged 35 commits into from
Nov 25, 2022
Merged
Show file tree
Hide file tree
Changes from 14 commits
Commits
Show all changes
35 commits
Select commit Hold shift + click to select a range
a96a740
Implemented wildcard_query and added tests in core
Nov 2, 2022
885f04c
Implemented and added tests for sql
Nov 2, 2022
4d0dadb
Implemented and added tests for ppl
Nov 3, 2022
9c25e66
Implemented and added tests for lucene
Nov 3, 2022
efe0ee5
Fixed test for like expression
Nov 3, 2022
bedf662
Added parameters to wildcard_query
Nov 4, 2022
afbeb44
Added integration tests for ppl and sql
Nov 4, 2022
e25d4da
Added docs for doctests
Nov 4, 2022
84fcf88
Pulled from 2.x
Nov 7, 2022
4708ae2
Fixed issues introduced during merging
Nov 7, 2022
e05e5c5
Addressed PR comment
Nov 7, 2022
988b40b
Added annotation that was deleted from merging
Nov 8, 2022
5c225ce
Fixed merge conflict issues
Nov 8, 2022
0b9752c
Addressed some PR comments and handled escaping wildcards
Nov 9, 2022
a2ca906
Added tests for wildcard conversion and created data for testing
Nov 11, 2022
68c97d6
Added javadoc
Nov 14, 2022
a873b27
Changed index name
Nov 14, 2022
518c09f
Temporarily changed jackson_version to run GH actions
Nov 14, 2022
1101ca3
Added comparison test for wildcard conversion
Nov 14, 2022
fc8883c
Removed PPL implementation of wildcard_query
Nov 14, 2022
ae46f96
Reverted ppl docs change
Nov 14, 2022
a9f5be7
Made namedArgument a static function
Nov 14, 2022
bba63ce
Removed extra space
Nov 14, 2022
8b7b442
Merge branch 'integ-add-wildcardquery' of github.com:Bit-Quill/opense…
Nov 15, 2022
4af9eef
Fixed LIKE query
Nov 17, 2022
57aac8e
Fixed LIKE tests and added more tests
Nov 18, 2022
0a4af9d
Addressed PR comments
Nov 18, 2022
288e29c
Implemented converting text field to keyword. Still needs testing
Nov 21, 2022
1fb4973
Added test cases for LIKE in sql and ppl
Nov 22, 2022
34ffe13
Addressed PR comments regarding docs
Nov 24, 2022
eed3294
Fixed backslashes in docs
Nov 24, 2022
86fa737
Added missed backticks in docs
Nov 24, 2022
b54a934
Moved escaping wildcard test to common/utils
Nov 25, 2022
fede123
Fixed merge conflicts
Nov 25, 2022
9da6140
Fixed checkstyle error
Nov 25, 2022
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
4 changes: 4 additions & 0 deletions core/src/main/java/org/opensearch/sql/expression/DSL.java
Original file line number Diff line number Diff line change
Expand Up @@ -719,6 +719,10 @@ public FunctionExpression match_bool_prefix(Expression... args) {
return compile(BuiltinFunctionName.MATCH_BOOL_PREFIX, args);
}

public FunctionExpression wildcard_query(Expression... args) {
Yury-Fridlyand marked this conversation as resolved.
Show resolved Hide resolved
return compile(BuiltinFunctionName.WILDCARD_QUERY, args);
}

public FunctionExpression now(Expression... args) {
return compile(BuiltinFunctionName.NOW, args);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,9 @@ public enum BuiltinFunctionName {
QUERY(FunctionName.of("query")),
MATCH_QUERY(FunctionName.of("match_query")),
MATCHQUERY(FunctionName.of("matchquery")),
MULTI_MATCH(FunctionName.of("multi_match"));
MULTI_MATCH(FunctionName.of("multi_match")),
WILDCARDQUERY(FunctionName.of("wildcardquery")),
WILDCARD_QUERY(FunctionName.of("wildcard_query"));

private final FunctionName name;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ public void register(BuiltinFunctionRepository repository) {
repository.register(match_phrase(BuiltinFunctionName.MATCH_PHRASE));
repository.register(match_phrase(BuiltinFunctionName.MATCHPHRASE));
repository.register(match_phrase_prefix());
repository.register(wildcard_query(BuiltinFunctionName.WILDCARD_QUERY));
repository.register(wildcard_query(BuiltinFunctionName.WILDCARDQUERY));
}

private static FunctionResolver match_bool_prefix() {
Expand Down Expand Up @@ -79,6 +81,11 @@ private static FunctionResolver query_string() {
return new RelevanceFunctionResolver(funcName, STRUCT);
}

private static FunctionResolver wildcard_query(BuiltinFunctionName wildcardQuery) {
FunctionName funcName = wildcardQuery.getName();
return new RelevanceFunctionResolver(funcName, STRING);
}

public static class OpenSearchFunction extends FunctionExpression {
private final FunctionName functionName;
private final List<Expression> arguments;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -541,6 +541,34 @@ void query_string_expression_two_fields() {
AstDSL.unresolvedArg("query", stringLiteral("query_value"))));
}

@Test
void wildcard_query_expression() {
assertAnalyzeEqual(
dsl.wildcard_query(
dsl.namedArgument("field", DSL.literal("test")),
dsl.namedArgument("query", DSL.literal("query_value*"))),
AstDSL.function("wildcard_query",
unresolvedArg("field", stringLiteral("test")),
unresolvedArg("query", stringLiteral("query_value*"))));
}

@Test
void wildcard_query_expression_all_params() {
assertAnalyzeEqual(
dsl.wildcard_query(
dsl.namedArgument("field", DSL.literal("test")),
dsl.namedArgument("query", DSL.literal("query_value*")),
dsl.namedArgument("boost", DSL.literal("1.5")),
dsl.namedArgument("case_insensitive", DSL.literal("true")),
dsl.namedArgument("rewrite", DSL.literal("scoring_boolean"))),
AstDSL.function("wildcard_query",
unresolvedArg("field", stringLiteral("test")),
unresolvedArg("query", stringLiteral("query_value*")),
unresolvedArg("boost", stringLiteral("1.5")),
unresolvedArg("case_insensitive", stringLiteral("true")),
unresolvedArg("rewrite", stringLiteral("scoring_boolean"))));
}

@Test
public void match_phrase_prefix_all_params() {
assertAnalyzeEqual(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -197,4 +197,12 @@ void query_string() {
fields.getValue(), query.getValue()),
expr.toString());
}

@Test
void wildcard_query() {
FunctionExpression expr = dsl.wildcard_query(field, query);
assertEquals(String.format("wildcard_query(field=%s, query=%s)",
field.getValue(), query.getValue()),
expr.toString());
}
}
42 changes: 42 additions & 0 deletions docs/user/dql/functions.rst
Original file line number Diff line number Diff line change
Expand Up @@ -3127,3 +3127,45 @@ Example::
|----------------+---------------+-----------------+------------------|
| DATE | INTEGER | DATETIME | STRUCT |
+----------------+---------------+-----------------+------------------+


WILDCARD_QUERY
------------

Description
>>>>>>>>>>>

``wildcard_query(field_expression, query_expression[, option=<option_value>]*)``

The wildcard_query function maps to the wildcard_query query used in search engine. It returns documents that match provided text in the specified field.
acarbonetto marked this conversation as resolved.
Show resolved Hide resolved
Supported wildcard characters can be found here: https://opensearch.org/docs/latest/opensearch/query-dsl/term/#wildcards

Available parameters include:

- boost
- case_insensitive
- rewrite

For backward compatibility, wildcardquery is also supported and mapped to wildcard_query query as well.

Example with only ``field`` and ``query`` expressions, and all other parameters are set default values::

os> select Id, Tags from beer where wildcard_query(Tags, 'champagn*');
GumpacG marked this conversation as resolved.
Show resolved Hide resolved
fetched rows / total rows = 2/2
+------+-----------------------------------------+
| Id | Tags |
|------+-----------------------------------------|
| 7419 | champagne |
| 7424 | alcohol-level yeast home-brew champagne |
+------+-----------------------------------------+

Another example to show how to set custom values for the optional parameters::

os> select Id, Tags from beer where wildcard_query(Tags, 'champagn*', boost=0.7, case_insensitive=true, rewrite='constant_score');
fetched rows / total rows = 2/2
+------+-----------------------------------------+
| Id | Tags |
|------+-----------------------------------------|
| 7419 | champagne |
| 7424 | alcohol-level yeast home-brew champagne |
+------+-----------------------------------------+
39 changes: 39 additions & 0 deletions docs/user/ppl/functions/relevance.rst
Original file line number Diff line number Diff line change
Expand Up @@ -357,3 +357,42 @@ Limitations

The relevance functions are available to execute only in OpenSearch DSL but not in memory as of now, so the relevance search might fail for queries that are too complex to translate into DSL if the relevance function is following after a complex PPL query. To make your queries always work-able, it is recommended to place the relevance commands as close to the search command as possible, to ensure the relevance functions are eligible to push down. For example, a complex query like ``search source = people | rename firstname as name | dedup account_number | fields name, account_number, balance, employer | where match(employer, 'Open Search') | stats count() by city`` could fail because it is difficult to translate to DSL, but it would be better if we rewrite it to an equivalent query as ``search source = people | where match(employer, 'Open Search') | rename firstname as name | dedup account_number | fields name, account_number, balance, employer | stats count() by city`` by moving the where command with relevance function to the second command right after the search command, and the relevance would be optimized and executed smoothly in OpenSearch DSL. See `Optimization <../../optimization/optimization.rst>`_ to get more details about the query engine optimization.


WILDCARD_QUERY
------------

Description
>>>>>>>>>>>

``wildcard_query(field_expression, query_expression[, option=<option_value>]*)``

The wildcard_query function maps to the wildcard_query query used in search engine. It returns documents that match provided text in the specified field.
Supported wildcard characters can be found here: https://opensearch.org/docs/latest/opensearch/query-dsl/term/#wildcards

Available parameters include:

- boost
- case_insensitive
- rewrite

Example with only ``field`` and ``query`` expressions, and all other parameters are set default values::

os> source=beer | where wildcard_query(Tags, 'champagn*') | fields Id,Tags;
fetched rows / total rows = 2/2
+------+-----------------------------------------+
| Id | Tags |
|------+-----------------------------------------|
| 7419 | champagne |
| 7424 | alcohol-level yeast home-brew champagne |
+------+-----------------------------------------+

Another example to show how to set custom values for the optional parameters::

os> source=beer | where wildcard_query(Tags, 'champagn*', boost=0.7, case_insensitive=true, rewrite='constant_score') | fields Id, Tags;
fetched rows / total rows = 2/2
+------+-----------------------------------------+
| Id | Tags |
|------+-----------------------------------------|
| 7419 | champagne |
| 7424 | alcohol-level yeast home-brew champagne |
+------+-----------------------------------------+
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/*
* Copyright OpenSearch Contributors
* SPDX-License-Identifier: Apache-2.0
*/

package org.opensearch.sql.ppl;

import static org.opensearch.sql.legacy.TestsConstants.TEST_INDEX_BEER;

import java.io.IOException;
import org.json.JSONObject;
import org.junit.Test;

public class WildcardQueryIT extends PPLIntegTestCase {

@Override
public void init() throws IOException {
loadIndex(Index.BEER);
}

@Test
public void wildcard_query_function_test() throws IOException {
String query = "source=" + TEST_INDEX_BEER + " | where wildcard_query(Tags, 't*')";
JSONObject result = executeQuery(query);
assertEquals(10, result.getInt("total"));
}

@Test
public void test_wildcard_query_sql_wildcard_conversion() throws IOException {
forestmvey marked this conversation as resolved.
Show resolved Hide resolved
// Test conversion from wildcard % to *
String query1 = "source=" + TEST_INDEX_BEER + " | where wildcard_query(Tags, 't*')";
JSONObject result1 = executeQuery(query1);
assertEquals(10, result1.getInt("total"));

String query2 = "source=" + TEST_INDEX_BEER + " | where wildcard_query(Tags, 't%')";
JSONObject result2 = executeQuery(query2);
assertEquals(10, result2.getInt("total"));

assertEquals(result1.getInt("total"), result2.getInt("total"));


// Test conversion from wildcard _ to ?
String query3 = "source=" + TEST_INDEX_BEER + " | where wildcard_query(Tags, 'tast?')";
JSONObject result3 = executeQuery(query3);
assertEquals(8, result3.getInt("total"));

String query4 = "source=" + TEST_INDEX_BEER + " | where wildcard_query(Tags, 'tast_')";
JSONObject result4 = executeQuery(query4);
assertEquals(8, result4.getInt("total"));

assertEquals(result3.getInt("total"), result4.getInt("total"));
}

@Test
public void all_params_test() throws IOException {
String query = "source=" + TEST_INDEX_BEER
+ "| where wildcard_query(Tags, 'tast_', boost = 0.9,"
+ "case_insensitive=true, rewrite='constant_score')";
JSONObject result = executeQuery(query);
assertEquals(8, result.getInt("total"));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
/*
* Copyright OpenSearch Contributors
* SPDX-License-Identifier: Apache-2.0
*/


package org.opensearch.sql.sql;

import static org.opensearch.sql.legacy.TestsConstants.TEST_INDEX_BEER;

import java.io.IOException;
import org.json.JSONObject;
import org.junit.Test;
import org.opensearch.sql.legacy.SQLIntegTestCase;

public class WildcardQueryIT extends SQLIntegTestCase {

@Override
protected void init() throws Exception {
loadIndex(Index.BEER);
}

@Test
public void test_wildcard_query_function() throws IOException {
forestmvey marked this conversation as resolved.
Show resolved Hide resolved
String query1 = "SELECT * FROM " + TEST_INDEX_BEER + " WHERE wildcard_query(Tags, 't*')";
JSONObject result1 = executeJdbcRequest(query1);
assertEquals(10, result1.getInt("total"));

String query2 = "SELECT * FROM " + TEST_INDEX_BEER + " WHERE wildcardquery(Tags, 't*')";
JSONObject result2 = executeJdbcRequest(query2);
assertEquals(10, result2.getInt("total"));

assertEquals(result1.getInt("total"), result2.getInt("total"));
}

@Test
public void test_wildcard_query_sql_wildcard_conversion() throws IOException {
forestmvey marked this conversation as resolved.
Show resolved Hide resolved
// Test conversion from wildcard % to *
String query1 = "SELECT * FROM " + TEST_INDEX_BEER + " WHERE wildcard_query(Tags, 't*')";
JSONObject result1 = executeJdbcRequest(query1);
assertEquals(10, result1.getInt("total"));

String query2 = "SELECT * FROM " + TEST_INDEX_BEER + " WHERE wildcard_query(Tags, 't%')";
JSONObject result2 = executeJdbcRequest(query2);
assertEquals(10, result2.getInt("total"));

assertEquals(result1.getInt("total"), result2.getInt("total"));


// Test conversion from wildcard _ to ?
String query3 = "SELECT * FROM " + TEST_INDEX_BEER + " WHERE wildcard_query(Tags, 'tast?')";
JSONObject result3 = executeJdbcRequest(query3);
assertEquals(8, result3.getInt("total"));

String query4 = "SELECT * FROM " + TEST_INDEX_BEER + " WHERE wildcard_query(Tags, 'tast_')";
JSONObject result4 = executeJdbcRequest(query4);
assertEquals(8, result4.getInt("total"));

assertEquals(result3.getInt("total"), result4.getInt("total"));
}

@Test
public void all_params_test() throws IOException {
String query = "SELECT Id FROM " + TEST_INDEX_BEER
+ "WHERE wildcard_query(Tags, 'tast_', boost = 0.9,"
+ "case_insensitive=true, rewrite='constant_score')";
JSONObject result = executeJdbcRequest(query);
assertEquals(8, result.getInt("total"));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
import org.opensearch.sql.opensearch.storage.script.filter.lucene.RangeQuery;
import org.opensearch.sql.opensearch.storage.script.filter.lucene.RangeQuery.Comparison;
import org.opensearch.sql.opensearch.storage.script.filter.lucene.TermQuery;
import org.opensearch.sql.opensearch.storage.script.filter.lucene.WildcardQuery;
import org.opensearch.sql.opensearch.storage.script.filter.lucene.relevance.MatchBoolPrefixQuery;
import org.opensearch.sql.opensearch.storage.script.filter.lucene.relevance.MatchPhrasePrefixQuery;
import org.opensearch.sql.opensearch.storage.script.filter.lucene.relevance.MatchPhraseQuery;
Expand All @@ -37,6 +36,7 @@
import org.opensearch.sql.opensearch.storage.script.filter.lucene.relevance.QueryQuery;
import org.opensearch.sql.opensearch.storage.script.filter.lucene.relevance.QueryStringQuery;
import org.opensearch.sql.opensearch.storage.script.filter.lucene.relevance.SimpleQueryStringQuery;
import org.opensearch.sql.opensearch.storage.script.filter.lucene.relevance.WildcardQuery;
import org.opensearch.sql.opensearch.storage.serialization.ExpressionSerializer;

@RequiredArgsConstructor
Expand Down Expand Up @@ -69,6 +69,8 @@ public class FilterQueryBuilder extends ExpressionNodeVisitor<QueryBuilder, Obje
.put(BuiltinFunctionName.QUERY_STRING.getName(), new QueryStringQuery())
.put(BuiltinFunctionName.MATCH_BOOL_PREFIX.getName(), new MatchBoolPrefixQuery())
.put(BuiltinFunctionName.MATCH_PHRASE_PREFIX.getName(), new MatchPhrasePrefixQuery())
.put(BuiltinFunctionName.WILDCARD_QUERY.getName(), new WildcardQuery())
.put(BuiltinFunctionName.WILDCARDQUERY.getName(), new WildcardQuery())
.build();

/**
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import org.opensearch.index.query.QueryStringQueryBuilder;
import org.opensearch.index.query.SimpleQueryStringBuilder;
import org.opensearch.index.query.SimpleQueryStringFlag;
import org.opensearch.index.query.WildcardQueryBuilder;
import org.opensearch.index.query.support.QueryParsers;
import org.opensearch.index.search.MatchQuery;
import org.opensearch.sql.data.model.ExprValue;
Expand Down Expand Up @@ -166,6 +167,14 @@ public class FunctionParameterRepository {
.put("quote_field_suffix", (b, v) -> b.quoteFieldSuffix(v.stringValue()))
.build();

public static final Map<String, RelevanceQuery.QueryBuilderStep<WildcardQueryBuilder>>
WildcardQueryBuildActions = ImmutableMap.<String,
RelevanceQuery.QueryBuilderStep<WildcardQueryBuilder>>builder()
.put("boost", (b, v) -> b.boost(convertFloatValue(v, "boost")))
.put("case_insensitive", (b, v) -> b.caseInsensitive(convertBoolValue(v, "case_insensitive")))
.put("rewrite", (b, v) -> b.rewrite(checkRewrite(v, "rewrite")))
MitchellGale marked this conversation as resolved.
Show resolved Hide resolved
.build();

public static final Map<String, String> ArgumentLimitations =
ImmutableMap.<String, String>builder()
.put("boost", "Accepts only floating point values greater than 0.")
Expand Down
Loading