-
Notifications
You must be signed in to change notification settings - Fork 141
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
…1010) This maintains backwards compatibility with the v1 engine.
- Loading branch information
1 parent
a4c7cf7
commit f8c58f1
Showing
18 changed files
with
727 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
84 changes: 84 additions & 0 deletions
84
integ-test/src/test/java/org/opensearch/sql/sql/QueryIT.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
/* | ||
* 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 QueryIT extends SQLIntegTestCase { | ||
@Override | ||
public void init() throws IOException { | ||
loadIndex(Index.BEER); | ||
} | ||
|
||
@Test | ||
public void all_fields_test() throws IOException { | ||
String query = "SELECT * FROM " | ||
+ TEST_INDEX_BEER + " WHERE query('*:taste')"; | ||
JSONObject result = executeJdbcRequest(query); | ||
assertEquals(16, result.getInt("total")); | ||
} | ||
|
||
@Test | ||
public void mandatory_params_test() throws IOException { | ||
String query = "SELECT Id FROM " | ||
+ TEST_INDEX_BEER + " WHERE query('Tags:taste OR Body:taste')"; | ||
JSONObject result = executeJdbcRequest(query); | ||
assertEquals(16, result.getInt("total")); | ||
} | ||
|
||
@Test | ||
public void all_params_test() throws IOException { | ||
String query = "SELECT Id FROM " + TEST_INDEX_BEER | ||
+ " WHERE query('Tags:taste', escape=false," | ||
+ "allow_leading_wildcard=true, enable_position_increments=true," | ||
+ "fuzziness= 1, fuzzy_rewrite='constant_score', max_determinized_states = 10000," | ||
+ "analyzer='standard', analyze_wildcard = false, quote_field_suffix = '.exact'," | ||
+ "auto_generate_synonyms_phrase_query=true, boost = 0.77," | ||
+ "quote_analyzer='standard', phrase_slop=0, rewrite='constant_score', type='best_fields'," | ||
+ "tie_breaker=0.3, time_zone='Canada/Pacific', default_operator='or'," | ||
+ "fuzzy_transpositions = false, lenient = true, fuzzy_max_expansions = 25," | ||
+ "minimum_should_match = '2<-25% 9<-3', fuzzy_prefix_length = 7);"; | ||
JSONObject result = executeJdbcRequest(query); | ||
assertEquals(8, result.getInt("total")); | ||
} | ||
|
||
@Test | ||
public void wildcard_test() throws IOException { | ||
String query1 = "SELECT Id FROM " | ||
+ TEST_INDEX_BEER + " WHERE query('Tags:taste')"; | ||
JSONObject result1 = executeJdbcRequest(query1); | ||
String query2 = "SELECT Id FROM " | ||
+ TEST_INDEX_BEER + " WHERE query('*:taste')"; | ||
JSONObject result2 = executeJdbcRequest(query2); | ||
assertNotEquals(result2.getInt("total"), result1.getInt("total")); | ||
|
||
String query3 = "SELECT Id FROM " + TEST_INDEX_BEER | ||
+ " WHERE query('Tags:tas*');"; | ||
JSONObject result3 = executeJdbcRequest(query3); | ||
assertEquals(8, result3.getInt("total")); | ||
|
||
String query4 = "SELECT Id FROM " + TEST_INDEX_BEER | ||
+ " WHERE query('Tags:tas?e');"; | ||
JSONObject result4 = executeJdbcRequest(query3); | ||
assertEquals(8, result4.getInt("total")); | ||
} | ||
|
||
@Test | ||
public void query_string_and_query_return_the_same_results_test() throws IOException { | ||
String query1 = "SELECT Id FROM " | ||
+ TEST_INDEX_BEER + " WHERE query('Tags:taste')"; | ||
JSONObject result1 = executeJdbcRequest(query1); | ||
String query2 = "SELECT Id FROM " | ||
+ TEST_INDEX_BEER + " WHERE query_string(['Tags'],'taste')"; | ||
JSONObject result2 = executeJdbcRequest(query2); | ||
assertEquals(result2.getInt("total"), result1.getInt("total")); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
72 changes: 72 additions & 0 deletions
72
...va/org/opensearch/sql/opensearch/storage/script/filter/lucene/relevance/NoFieldQuery.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
/* | ||
* Copyright OpenSearch Contributors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package org.opensearch.sql.opensearch.storage.script.filter.lucene.relevance; | ||
|
||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.Objects; | ||
import java.util.stream.Collectors; | ||
import org.opensearch.index.query.QueryBuilder; | ||
import org.opensearch.sql.common.antlr.SyntaxCheckException; | ||
import org.opensearch.sql.exception.SemanticCheckException; | ||
import org.opensearch.sql.expression.FunctionExpression; | ||
import org.opensearch.sql.expression.NamedArgumentExpression; | ||
|
||
/** | ||
* Base class to represent relevance queries that have no 'fields' array as an argument. | ||
* | ||
* @param <T> The builder class for the OpenSearch query. | ||
*/ | ||
abstract class NoFieldQuery<T extends QueryBuilder> extends RelevanceQuery<T> { | ||
public NoFieldQuery(Map<String, QueryBuilderStep<T>> queryBuildActions) { | ||
super(queryBuildActions); | ||
} | ||
|
||
@Override | ||
protected void ignoreArguments(List<NamedArgumentExpression> arguments) { | ||
arguments.removeIf(a -> a.getArgName().equalsIgnoreCase("query")); | ||
} | ||
|
||
@Override | ||
protected void checkValidArguments(String argNormalized, T queryBuilder) { | ||
if (!getQueryBuildActions().containsKey(argNormalized)) { | ||
throw new SemanticCheckException( | ||
String.format("Parameter %s is invalid for %s function.", | ||
argNormalized, getQueryName())); | ||
} | ||
} | ||
/** | ||
* Override build function because RelevanceQuery requires 2 fields, | ||
* but NoFieldQuery must have no fields. | ||
* | ||
* @param func : Contains function name and passed in arguments. | ||
* @return : QueryBuilder object | ||
*/ | ||
|
||
@Override | ||
public QueryBuilder build(FunctionExpression func) { | ||
var arguments = func.getArguments().stream().map( | ||
a -> (NamedArgumentExpression) a).collect(Collectors.toList()); | ||
if (arguments.size() < 1) { | ||
throw new SyntaxCheckException(String.format( | ||
"%s requires at least one parameter", func.getFunctionName())); | ||
} | ||
|
||
return loadArguments(arguments); | ||
} | ||
|
||
|
||
@Override | ||
public T createQueryBuilder(List<NamedArgumentExpression> arguments) { | ||
// Extract 'query' | ||
var query = arguments.stream().filter(a -> a.getArgName().equalsIgnoreCase("query")).findFirst() | ||
.orElseThrow(() -> new SemanticCheckException("'query' parameter is missing")); | ||
|
||
return createBuilder(query.getValue().valueOf(null).stringValue()); | ||
} | ||
|
||
protected abstract T createBuilder(String query); | ||
} |
40 changes: 40 additions & 0 deletions
40
...java/org/opensearch/sql/opensearch/storage/script/filter/lucene/relevance/QueryQuery.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
/* | ||
* Copyright OpenSearch Contributors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package org.opensearch.sql.opensearch.storage.script.filter.lucene.relevance; | ||
|
||
import org.opensearch.index.query.QueryBuilders; | ||
import org.opensearch.index.query.QueryStringQueryBuilder; | ||
|
||
/** | ||
* Class for Lucene query that builds the 'query' query. | ||
*/ | ||
public class QueryQuery extends NoFieldQuery<QueryStringQueryBuilder> { | ||
|
||
private final String queryQueryName = "query"; | ||
|
||
/** | ||
* Default constructor for QueryQuery configures how RelevanceQuery.build() handles | ||
* named arguments by calling the constructor of QueryStringQuery. | ||
*/ | ||
public QueryQuery() { | ||
super(FunctionParameterRepository.QueryStringQueryBuildActions); | ||
} | ||
|
||
/** | ||
* Builds QueryBuilder with query value and other default parameter values set. | ||
* | ||
* @param query : Query value for query_string query | ||
* @return : Builder for query query | ||
*/ | ||
protected QueryStringQueryBuilder createBuilder(String query) { | ||
return QueryBuilders.queryStringQuery(query); | ||
} | ||
|
||
@Override | ||
public String getQueryName() { | ||
return queryQueryName; | ||
} | ||
} |
Oops, something went wrong.