Skip to content

Commit

Permalink
Move queryBuildActions for all types of queries to `FunctionParamet…
Browse files Browse the repository at this point in the history
…erRepository`.

Signed-off-by: Yury-Fridlyand <[email protected]>
  • Loading branch information
Yury-Fridlyand committed Oct 28, 2022
1 parent b5ad3c9 commit 827a0cf
Show file tree
Hide file tree
Showing 8 changed files with 182 additions and 220 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,50 +10,191 @@
import java.util.Arrays;
import java.util.Map;
import java.util.stream.Collectors;
import lombok.experimental.UtilityClass;
import org.opensearch.common.unit.Fuzziness;
import org.opensearch.common.xcontent.LoggingDeprecationHandler;
import org.opensearch.index.query.MatchBoolPrefixQueryBuilder;
import org.opensearch.index.query.MatchPhrasePrefixQueryBuilder;
import org.opensearch.index.query.MatchPhraseQueryBuilder;
import org.opensearch.index.query.MatchQueryBuilder;
import org.opensearch.index.query.MultiMatchQueryBuilder;
import org.opensearch.index.query.Operator;
import org.opensearch.index.query.QueryStringQueryBuilder;
import org.opensearch.index.query.SimpleQueryStringBuilder;
import org.opensearch.index.query.SimpleQueryStringFlag;
import org.opensearch.index.query.support.QueryParsers;
import org.opensearch.index.search.MatchQuery;
import org.opensearch.sql.data.model.ExprValue;

@UtilityClass
public class FunctionParameterRepository {
private FunctionParameterRepository() {
}

public static final Map<String, String> ArgumentLimitations;

static {
ArgumentLimitations = ImmutableMap.<String, String>builder()
.put("boost", "Accepts only floating point values greater than 0.")
.put("tie_breaker", "Accepts only floating point values in range 0 to 1.")
.put("rewrite", "Available values are: constant_score, "
+ "scoring_boolean, constant_score_boolean, top_terms_X, top_terms_boost_X, "
+ "top_terms_blended_freqs_X, where X is an integer value.")
.put("flags", String.format(
"Available values are: %s and any combinations of these separated by '|'.",
Arrays.stream(SimpleQueryStringFlag.class.getEnumConstants())
.map(Enum::toString).collect(Collectors.joining(", "))))
.put("time_zone", "For more information, follow this link: "
+ "https://docs.oracle.com/javase/8/docs/api/java/time/ZoneId.html#of-java.lang.String-")
.put("fuzziness", "Available values are: "
+ "'AUTO', 'AUTO:x,y' or z, where x, y, z - integer values.")
.put("operator", String.format("Available values are: %s.",
Arrays.stream(Operator.class.getEnumConstants())
.map(Enum::toString).collect(Collectors.joining(", "))))
.put("type", String.format("Available values are: %s.",
Arrays.stream(MultiMatchQueryBuilder.Type.class.getEnumConstants())
.map(Enum::toString).collect(Collectors.joining(", "))))
.put("zero_terms_query", String.format("Available values are: %s.",
Arrays.stream(MatchQuery.ZeroTermsQuery.class.getEnumConstants())
.map(Enum::toString).collect(Collectors.joining(", "))))
.put("int", "Accepts only integer values.")
.put("float", "Accepts only floating point values.")
.put("bool", "Accepts only boolean values: 'true' or 'false'.")
.build();
}
public static final Map<String, RelevanceQuery.QueryBuilderStep<MatchBoolPrefixQueryBuilder>>
MatchBoolPrefixQueryBuildActions = ImmutableMap.<String,
RelevanceQuery.QueryBuilderStep<MatchBoolPrefixQueryBuilder>>builder()
.put("analyzer", (b, v) -> b.analyzer(v.stringValue()))
.put("boost", (b, v) -> b.boost(convertFloatValue(v, "boost")))
.put("fuzziness", (b, v) -> b.fuzziness(convertFuzziness(v)))
.put("fuzzy_rewrite", (b, v) -> b.fuzzyRewrite(checkRewrite(v, "fuzzy_rewrite")))
.put("fuzzy_transpositions", (b, v) -> b.fuzzyTranspositions(
convertBoolValue(v, "fuzzy_transpositions")))
.put("max_expansions", (b, v) -> b.maxExpansions(convertIntValue(v, "max_expansions")))
.put("minimum_should_match", (b, v) -> b.minimumShouldMatch(v.stringValue()))
.put("operator", (b, v) -> b.operator(convertOperator(v, "operator")))
.put("prefix_length", (b, v) -> b.prefixLength(convertIntValue(v, "prefix_length")))
.build();

public static final Map<String, RelevanceQuery.QueryBuilderStep<MatchPhrasePrefixQueryBuilder>>
MatchPhrasePrefixQueryBuildActions = ImmutableMap.<String,
RelevanceQuery.QueryBuilderStep<MatchPhrasePrefixQueryBuilder>>builder()
.put("analyzer", (b, v) -> b.analyzer(v.stringValue()))
.put("boost", (b, v) -> b.boost(convertFloatValue(v, "boost")))
.put("max_expansions", (b, v) -> b.maxExpansions(convertIntValue(v, "max_expansions")))
.put("slop", (b, v) -> b.slop(convertIntValue(v, "slop")))
.put("zero_terms_query", (b, v) -> b.zeroTermsQuery(convertZeroTermsQuery(v)))
.build();

public static final Map<String, RelevanceQuery.QueryBuilderStep<MatchPhraseQueryBuilder>>
MatchPhraseQueryBuildActions = ImmutableMap.<String,
RelevanceQuery.QueryBuilderStep<MatchPhraseQueryBuilder>>builder()
.put("analyzer", (b, v) -> b.analyzer(v.stringValue()))
.put("boost", (b, v) -> b.boost(convertFloatValue(v, "boost")))
.put("slop", (b, v) -> b.slop(convertIntValue(v, "slop")))
.put("zero_terms_query", (b, v) -> b.zeroTermsQuery(convertZeroTermsQuery(v)))
.build();

public static final Map<String, RelevanceQuery.QueryBuilderStep<MatchQueryBuilder>>
MatchQueryBuildActions = ImmutableMap.<String,
RelevanceQuery.QueryBuilderStep<MatchQueryBuilder>>builder()
.put("analyzer", (b, v) -> b.analyzer(v.stringValue()))
.put("auto_generate_synonyms_phrase_query", (b, v) -> b.autoGenerateSynonymsPhraseQuery(
convertBoolValue(v, "auto_generate_synonyms_phrase_query")))
.put("boost", (b, v) -> b.boost(convertFloatValue(v, "boost")))
.put("fuzziness", (b, v) -> b.fuzziness(convertFuzziness(v)))
.put("fuzzy_rewrite", (b, v) -> b.fuzzyRewrite(checkRewrite(v, "fuzzy_rewrite")))
.put("fuzzy_transpositions", (b, v) -> b.fuzzyTranspositions(
convertBoolValue(v, "fuzzy_transpositions")))
.put("lenient", (b, v) -> b.lenient(convertBoolValue(v, "lenient")))
.put("minimum_should_match", (b, v) -> b.minimumShouldMatch(v.stringValue()))
.put("max_expansions", (b, v) -> b.maxExpansions(convertIntValue(v, "max_expansions")))
.put("operator", (b, v) -> b.operator(convertOperator(v, "operator")))
.put("prefix_length", (b, v) -> b.prefixLength(convertIntValue(v, "prefix_length")))
.put("zero_terms_query", (b, v) -> b.zeroTermsQuery(convertZeroTermsQuery(v)))
.build();

@SuppressWarnings("deprecation") // cutoffFrequency is deprecated
public static final Map<String, RelevanceQuery.QueryBuilderStep<MultiMatchQueryBuilder>>
MultiMatchQueryBuildActions = ImmutableMap.<String,
RelevanceQuery.QueryBuilderStep<MultiMatchQueryBuilder>>builder()
.put("analyzer", (b, v) -> b.analyzer(v.stringValue()))
.put("auto_generate_synonyms_phrase_query", (b, v) -> b.autoGenerateSynonymsPhraseQuery(
convertBoolValue(v, "auto_generate_synonyms_phrase_query")))
.put("boost", (b, v) -> b.boost(convertFloatValue(v, "boost")))
.put("cutoff_frequency", (b, v) -> b.cutoffFrequency(
convertFloatValue(v, "cutoff_frequency")))
.put("fuzziness", (b, v) -> b.fuzziness(convertFuzziness(v)))
.put("fuzzy_transpositions", (b, v) -> b.fuzzyTranspositions(
convertBoolValue(v, "fuzzy_transpositions")))
.put("lenient", (b, v) -> b.lenient(convertBoolValue(v, "lenient")))
.put("max_expansions", (b, v) -> b.maxExpansions(convertIntValue(v, "max_expansions")))
.put("minimum_should_match", (b, v) -> b.minimumShouldMatch(v.stringValue()))
.put("operator", (b, v) -> b.operator(convertOperator(v, "operator")))
.put("prefix_length", (b, v) -> b.prefixLength(convertIntValue(v, "prefix_length")))
.put("slop", (b, v) -> b.slop(convertIntValue(v, "slop")))
.put("tie_breaker", (b, v) -> b.tieBreaker(convertFloatValue(v, "tie_breaker")))
.put("type", (b, v) -> b.type(convertType(v)))
.put("zero_terms_query", (b, v) -> b.zeroTermsQuery(convertZeroTermsQuery(v)))
.build();

public static final Map<String, RelevanceQuery.QueryBuilderStep<QueryStringQueryBuilder>>
QueryStringQueryBuildActions = ImmutableMap.<String,
RelevanceQuery.QueryBuilderStep<QueryStringQueryBuilder>>builder()
.put("allow_leading_wildcard", (b, v) -> b.allowLeadingWildcard(
convertBoolValue(v, "allow_leading_wildcard")))
.put("analyzer", (b, v) -> b.analyzer(v.stringValue()))
.put("analyze_wildcard", (b, v) -> b.analyzeWildcard(
convertBoolValue(v, "analyze_wildcard")))
.put("auto_generate_synonyms_phrase_query", (b, v) -> b.autoGenerateSynonymsPhraseQuery(
convertBoolValue(v, "auto_generate_synonyms_phrase_query")))
.put("boost", (b, v) -> b.boost(convertFloatValue(v, "boost")))
.put("default_operator", (b, v) -> b.defaultOperator(
convertOperator(v, "default_operator")))
.put("enable_position_increments", (b, v) -> b.enablePositionIncrements(
convertBoolValue(v, "enable_position_increments")))
.put("escape", (b, v) -> b.escape(convertBoolValue(v, "escape")))
.put("fuzziness", (b, v) -> b.fuzziness(convertFuzziness(v)))
.put("fuzzy_max_expansions", (b, v) -> b.fuzzyMaxExpansions(
convertIntValue(v, "fuzzy_max_expansions")))
.put("fuzzy_prefix_length", (b, v) -> b.fuzzyPrefixLength(
convertIntValue(v, "fuzzy_prefix_length")))
.put("fuzzy_rewrite", (b, v) -> b.fuzzyRewrite(checkRewrite(v, "fuzzy_rewrite")))
.put("fuzzy_transpositions", (b, v) -> b.fuzzyTranspositions(
convertBoolValue(v, "fuzzy_transpositions")))
.put("lenient", (b, v) -> b.lenient(convertBoolValue(v, "lenient")))
.put("max_determinized_states", (b, v) -> b.maxDeterminizedStates(
convertIntValue(v, "max_determinized_states")))
.put("minimum_should_match", (b, v) -> b.minimumShouldMatch(v.stringValue()))
.put("phrase_slop", (b, v) -> b.phraseSlop(convertIntValue(v, "phrase_slop")))
.put("quote_analyzer", (b, v) -> b.quoteAnalyzer(v.stringValue()))
.put("quote_field_suffix", (b, v) -> b.quoteFieldSuffix(v.stringValue()))
.put("rewrite", (b, v) -> b.rewrite(checkRewrite(v, "rewrite")))
.put("tie_breaker", (b, v) -> b.tieBreaker(convertFloatValue(v, "tie_breaker")))
.put("time_zone", (b, v) -> b.timeZone(checkTimeZone(v)))
.put("type", (b, v) -> b.type(convertType(v)))
.build();

public static final Map<String, RelevanceQuery.QueryBuilderStep<SimpleQueryStringBuilder>>
SimpleQueryStringQueryBuildActions = ImmutableMap.<String,
RelevanceQuery.QueryBuilderStep<SimpleQueryStringBuilder>>builder()
.put("analyzer", (b, v) -> b.analyzer(v.stringValue()))
.put("analyze_wildcard", (b, v) -> b.analyzeWildcard(
convertBoolValue(v, "analyze_wildcard")))
.put("auto_generate_synonyms_phrase_query", (b, v) -> b.autoGenerateSynonymsPhraseQuery(
convertBoolValue(v, "auto_generate_synonyms_phrase_query")))
.put("boost", (b, v) -> b.boost(convertFloatValue(v, "boost")))
.put("default_operator", (b, v) -> b.defaultOperator(
convertOperator(v, "default_operator")))
.put("flags", (b, v) -> b.flags(convertFlags(v)))
.put("fuzzy_max_expansions", (b, v) -> b.fuzzyMaxExpansions(
convertIntValue(v, "fuzzy_max_expansions")))
.put("fuzzy_prefix_length", (b, v) -> b.fuzzyPrefixLength(
convertIntValue(v, "fuzzy_prefix_length")))
.put("fuzzy_transpositions", (b, v) -> b.fuzzyTranspositions(
convertBoolValue(v, "fuzzy_transpositions")))
.put("lenient", (b, v) -> b.lenient(convertBoolValue(v, "lenient")))
.put("minimum_should_match", (b, v) -> b.minimumShouldMatch(v.stringValue()))
.put("quote_field_suffix", (b, v) -> b.quoteFieldSuffix(v.stringValue()))
.build();

public static final Map<String, String> ArgumentLimitations =
ImmutableMap.<String, String>builder()
.put("boost", "Accepts only floating point values greater than 0.")
.put("tie_breaker", "Accepts only floating point values in range 0 to 1.")
.put("rewrite", "Available values are: constant_score, "
+ "scoring_boolean, constant_score_boolean, top_terms_X, top_terms_boost_X, "
+ "top_terms_blended_freqs_X, where X is an integer value.")
.put("flags", String.format(
"Available values are: %s and any combinations of these separated by '|'.",
Arrays.stream(SimpleQueryStringFlag.class.getEnumConstants())
.map(Enum::toString).collect(Collectors.joining(", "))))
.put("time_zone", "For more information, follow this link: "
+ "https://docs.oracle.com/javase/8/docs/api/java/time/ZoneId.html#of-java.lang.String-")
.put("fuzziness", "Available values are: "
+ "'AUTO', 'AUTO:x,y' or z, where x, y, z - integer values.")
.put("operator", String.format("Available values are: %s.",
Arrays.stream(Operator.class.getEnumConstants())
.map(Enum::toString).collect(Collectors.joining(", "))))
.put("type", String.format("Available values are: %s.",
Arrays.stream(MultiMatchQueryBuilder.Type.class.getEnumConstants())
.map(Enum::toString).collect(Collectors.joining(", "))))
.put("zero_terms_query", String.format("Available values are: %s.",
Arrays.stream(MatchQuery.ZeroTermsQuery.class.getEnumConstants())
.map(Enum::toString).collect(Collectors.joining(", "))))
.put("int", "Accepts only integer values.")
.put("float", "Accepts only floating point values.")
.put("bool", "Accepts only boolean values: 'true' or 'false'.")
.build();


private static String formatErrorMessage(String name, String value) {
return formatErrorMessage(name, value, name);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,7 @@

package org.opensearch.sql.opensearch.storage.script.filter.lucene.relevance;

import com.google.common.collect.ImmutableMap;
import org.opensearch.index.query.MatchBoolPrefixQueryBuilder;
import org.opensearch.index.query.Operator;
import org.opensearch.index.query.QueryBuilders;

/**
Expand All @@ -20,23 +18,7 @@ public class MatchBoolPrefixQuery
* with support of optional parameters.
*/
public MatchBoolPrefixQuery() {
super(ImmutableMap.<String, QueryBuilderStep<MatchBoolPrefixQueryBuilder>>builder()
.put("analyzer", (b, v) -> b.analyzer(v.stringValue()))
.put("boost", (b, v) -> b.boost(
FunctionParameterRepository.convertFloatValue(v, "boost")))
.put("fuzziness", (b, v) -> b.fuzziness(FunctionParameterRepository.convertFuzziness(v)))
.put("fuzzy_rewrite", (b, v) -> b.fuzzyRewrite(
FunctionParameterRepository.checkRewrite(v, "fuzzy_rewrite")))
.put("fuzzy_transpositions", (b, v) -> b.fuzzyTranspositions(
FunctionParameterRepository.convertBoolValue(v, "fuzzy_transpositions")))
.put("max_expansions", (b, v) -> b.maxExpansions(
FunctionParameterRepository.convertIntValue(v, "max_expansions")))
.put("minimum_should_match", (b, v) -> b.minimumShouldMatch(v.stringValue()))
.put("operator", (b, v) -> b.operator(
FunctionParameterRepository.convertOperator(v, "operator")))
.put("prefix_length", (b, v) -> b.prefixLength(
FunctionParameterRepository.convertIntValue(v, "prefix_length")))
.build());
super(FunctionParameterRepository.MatchBoolPrefixQueryBuildActions);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,30 +5,19 @@

package org.opensearch.sql.opensearch.storage.script.filter.lucene.relevance;

import com.google.common.collect.ImmutableMap;
import org.opensearch.index.query.MatchPhrasePrefixQueryBuilder;
import org.opensearch.index.query.QueryBuilders;

/**
* Lucene query that builds a match_phrase_prefix query.
*/
public class MatchPhrasePrefixQuery extends SingleFieldQuery<MatchPhrasePrefixQueryBuilder> {
public class MatchPhrasePrefixQuery extends SingleFieldQuery<MatchPhrasePrefixQueryBuilder> {
/**
* Default constructor for MatchPhrasePrefixQuery configures how RelevanceQuery.build() handles
* named arguments.
*/
public MatchPhrasePrefixQuery() {
super(ImmutableMap.<String, QueryBuilderStep<MatchPhrasePrefixQueryBuilder>>builder()
.put("analyzer", (b, v) -> b.analyzer(v.stringValue()))
.put("boost", (b, v) -> b.boost(
FunctionParameterRepository.convertFloatValue(v, "boost")))
.put("max_expansions", (b, v) -> b.maxExpansions(
FunctionParameterRepository.convertIntValue(v, "max_expansions")))
.put("slop", (b, v) -> b.slop(
FunctionParameterRepository.convertIntValue(v, "slop")))
.put("zero_terms_query", (b, v) -> b.zeroTermsQuery(
FunctionParameterRepository.convertZeroTermsQuery(v)))
.build());
super(FunctionParameterRepository.MatchPhrasePrefixQueryBuildActions);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,8 @@

package org.opensearch.sql.opensearch.storage.script.filter.lucene.relevance;

import com.google.common.collect.ImmutableMap;
import java.util.Iterator;
import java.util.List;
import java.util.Objects;
import java.util.function.BiFunction;
import org.opensearch.index.query.MatchPhraseQueryBuilder;
import org.opensearch.index.query.QueryBuilder;
import org.opensearch.index.query.QueryBuilders;
import org.opensearch.sql.data.model.ExprValue;
import org.opensearch.sql.exception.SemanticCheckException;
import org.opensearch.sql.expression.Expression;
import org.opensearch.sql.expression.FunctionExpression;
import org.opensearch.sql.expression.NamedArgumentExpression;
import org.opensearch.sql.opensearch.storage.script.filter.lucene.LuceneQuery;

/**
* Lucene query that builds a match_phrase query.
Expand All @@ -29,15 +17,7 @@ public class MatchPhraseQuery extends SingleFieldQuery<MatchPhraseQueryBuilder>
* named arguments.
*/
public MatchPhraseQuery() {
super(ImmutableMap.<String, QueryBuilderStep<MatchPhraseQueryBuilder>>builder()
.put("analyzer", (b, v) -> b.analyzer(v.stringValue()))
.put("boost", (b, v) -> b.boost(
FunctionParameterRepository.convertFloatValue(v, "boost")))
.put("slop", (b, v) -> b.slop(
FunctionParameterRepository.convertIntValue(v, "slop")))
.put("zero_terms_query", (b, v) -> b.zeroTermsQuery(
FunctionParameterRepository.convertZeroTermsQuery(v)))
.build());
super(FunctionParameterRepository.MatchPhraseQueryBuildActions);
}

@Override
Expand Down
Loading

0 comments on commit 827a0cf

Please sign in to comment.