|
6 | 6 | package org.opensearch.sql.opensearch.storage.script.filter.lucene.relevance; |
7 | 7 |
|
8 | 8 | import com.google.common.collect.ImmutableMap; |
9 | | -import java.util.Iterator; |
10 | | -import java.util.function.BiFunction; |
| 9 | +import java.util.Map; |
11 | 10 | import org.opensearch.index.query.MatchQueryBuilder; |
12 | 11 | import org.opensearch.index.query.Operator; |
13 | | -import org.opensearch.index.query.QueryBuilder; |
14 | 12 | import org.opensearch.index.query.QueryBuilders; |
15 | | -import org.opensearch.sql.data.model.ExprValue; |
16 | | -import org.opensearch.sql.exception.SemanticCheckException; |
17 | | -import org.opensearch.sql.expression.Expression; |
18 | | -import org.opensearch.sql.expression.FunctionExpression; |
19 | | -import org.opensearch.sql.expression.NamedArgumentExpression; |
20 | | -import org.opensearch.sql.opensearch.storage.script.filter.lucene.LuceneQuery; |
21 | 13 |
|
22 | | -public class MatchQuery extends LuceneQuery { |
23 | | - private final BiFunction<MatchQueryBuilder, ExprValue, MatchQueryBuilder> analyzer = |
24 | | - (b, v) -> b.analyzer(v.stringValue()); |
25 | | - private final BiFunction<MatchQueryBuilder, ExprValue, MatchQueryBuilder> synonymsPhrase = |
26 | | - (b, v) -> b.autoGenerateSynonymsPhraseQuery(Boolean.parseBoolean(v.stringValue())); |
27 | | - private final BiFunction<MatchQueryBuilder, ExprValue, MatchQueryBuilder> fuzziness = |
28 | | - (b, v) -> b.fuzziness(v.stringValue()); |
29 | | - private final BiFunction<MatchQueryBuilder, ExprValue, MatchQueryBuilder> maxExpansions = |
30 | | - (b, v) -> b.maxExpansions(Integer.parseInt(v.stringValue())); |
31 | | - private final BiFunction<MatchQueryBuilder, ExprValue, MatchQueryBuilder> prefixLength = |
32 | | - (b, v) -> b.prefixLength(Integer.parseInt(v.stringValue())); |
33 | | - private final BiFunction<MatchQueryBuilder, ExprValue, MatchQueryBuilder> fuzzyTranspositions = |
34 | | - (b, v) -> b.fuzzyTranspositions(Boolean.parseBoolean(v.stringValue())); |
35 | | - private final BiFunction<MatchQueryBuilder, ExprValue, MatchQueryBuilder> fuzzyRewrite = |
36 | | - (b, v) -> b.fuzzyRewrite(v.stringValue()); |
37 | | - private final BiFunction<MatchQueryBuilder, ExprValue, MatchQueryBuilder> lenient = |
38 | | - (b, v) -> b.lenient(Boolean.parseBoolean(v.stringValue())); |
39 | | - private final BiFunction<MatchQueryBuilder, ExprValue, MatchQueryBuilder> operator = |
40 | | - (b, v) -> b.operator(Operator.fromString(v.stringValue())); |
41 | | - private final BiFunction<MatchQueryBuilder, ExprValue, MatchQueryBuilder> minimumShouldMatch = |
42 | | - (b, v) -> b.minimumShouldMatch(v.stringValue()); |
43 | | - private final BiFunction<MatchQueryBuilder, ExprValue, MatchQueryBuilder> zeroTermsQuery = |
44 | | - (b, v) -> b.zeroTermsQuery( |
45 | | - org.opensearch.index.search.MatchQuery.ZeroTermsQuery.valueOf(v.stringValue())); |
46 | | - private final BiFunction<MatchQueryBuilder, ExprValue, MatchQueryBuilder> boost = |
47 | | - (b, v) -> b.boost(Float.parseFloat(v.stringValue())); |
48 | | - |
49 | | - ImmutableMap<Object, Object> argAction = ImmutableMap.builder() |
50 | | - .put("analyzer", analyzer) |
51 | | - .put("auto_generate_synonyms_phrase_query", synonymsPhrase) |
52 | | - .put("fuzziness", fuzziness) |
53 | | - .put("max_expansions", maxExpansions) |
54 | | - .put("prefix_length", prefixLength) |
55 | | - .put("fuzzy_transpositions", fuzzyTranspositions) |
56 | | - .put("fuzzy_rewrite", fuzzyRewrite) |
57 | | - .put("lenient", lenient) |
58 | | - .put("operator", operator) |
59 | | - .put("minimum_should_match", minimumShouldMatch) |
60 | | - .put("zero_terms_query", zeroTermsQuery) |
61 | | - .put("boost", boost) |
62 | | - .build(); |
| 14 | +/** |
| 15 | + * Initializes MatchQueryBuilder from a FunctionExpression. |
| 16 | + */ |
| 17 | +public class MatchQuery extends RelevanceQuery<MatchQueryBuilder> { |
| 18 | + /** |
| 19 | + * Default constructor for MatchQuery configures how RelevanceQuery.build() handles |
| 20 | + * named arguments. |
| 21 | + */ |
| 22 | + public MatchQuery() { |
| 23 | + super(ImmutableMap.<String, QueryBuilderStep<MatchQueryBuilder>>builder() |
| 24 | + .put("analyzer", (b, v) -> b.analyzer(v.stringValue())) |
| 25 | + .put("auto_generate_synonyms_phrase_query", |
| 26 | + (b, v) -> b.autoGenerateSynonymsPhraseQuery(Boolean.parseBoolean(v.stringValue()))) |
| 27 | + .put("fuzziness", (b, v) -> b.fuzziness(v.stringValue())) |
| 28 | + .put("max_expansions", (b, v) -> b.maxExpansions(Integer.parseInt(v.stringValue()))) |
| 29 | + .put("prefix_length", (b, v) -> b.prefixLength(Integer.parseInt(v.stringValue()))) |
| 30 | + .put("fuzzy_transpositions", |
| 31 | + (b, v) -> b.fuzzyTranspositions(Boolean.parseBoolean(v.stringValue()))) |
| 32 | + .put("fuzzy_rewrite", (b, v) -> b.fuzzyRewrite(v.stringValue())) |
| 33 | + .put("lenient", (b, v) -> b.lenient(Boolean.parseBoolean(v.stringValue()))) |
| 34 | + .put("operator", (b, v) -> b.operator(Operator.fromString(v.stringValue()))) |
| 35 | + .put("minimum_should_match", (b, v) -> b.minimumShouldMatch(v.stringValue())) |
| 36 | + .put("zero_terms_query", (b, v) -> b.zeroTermsQuery( |
| 37 | + org.opensearch.index.search.MatchQuery.ZeroTermsQuery.valueOf(v.stringValue()))) |
| 38 | + .put("boost", (b, v) -> b.boost(Float.parseFloat(v.stringValue()))) |
| 39 | + .build()); |
| 40 | + } |
63 | 41 |
|
64 | 42 | @Override |
65 | | - public QueryBuilder build(FunctionExpression func) { |
66 | | - Iterator<Expression> iterator = func.getArguments().iterator(); |
67 | | - NamedArgumentExpression field = (NamedArgumentExpression) iterator.next(); |
68 | | - NamedArgumentExpression query = (NamedArgumentExpression) iterator.next(); |
69 | | - MatchQueryBuilder queryBuilder = QueryBuilders.matchQuery( |
70 | | - field.getValue().valueOf(null).stringValue(), |
71 | | - query.getValue().valueOf(null).stringValue()); |
72 | | - while (iterator.hasNext()) { |
73 | | - NamedArgumentExpression arg = (NamedArgumentExpression) iterator.next(); |
74 | | - if (!argAction.containsKey(arg.getArgName())) { |
75 | | - throw new SemanticCheckException(String |
76 | | - .format("Parameter %s is invalid for match function.", arg.getArgName())); |
77 | | - } |
78 | | - ((BiFunction<MatchQueryBuilder, ExprValue, MatchQueryBuilder>) argAction |
79 | | - .get(arg.getArgName())) |
80 | | - .apply(queryBuilder, arg.getValue().valueOf(null)); |
81 | | - } |
82 | | - return queryBuilder; |
| 43 | + protected MatchQueryBuilder createQueryBuilder(String field, String query) { |
| 44 | + return QueryBuilders.matchQuery(field, query); |
83 | 45 | } |
84 | 46 | } |
0 commit comments