Skip to content

Commit c70809b

Browse files
author
karenx
committed
cherrypick
1 parent 6200124 commit c70809b

File tree

10 files changed

+927
-0
lines changed

10 files changed

+927
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/*
2+
* SPDX-License-Identifier: Apache-2.0
3+
*
4+
* The OpenSearch Contributors require contributions made to
5+
* this file be licensed under the Apache-2.0 license or a
6+
* compatible open source license.
7+
*/
8+
package org.opensearch.transport.grpc.proto.request.search.query;
9+
10+
import org.opensearch.index.query.QueryBuilder;
11+
import org.opensearch.protobufs.QueryContainer;
12+
import org.opensearch.transport.grpc.spi.QueryBuilderProtoConverter;
13+
14+
/**
15+
* Converter for Fuzzy queries.
16+
* This class implements the QueryBuilderProtoConverter interface to provide Fuzzy query support
17+
* for the gRPC transport module.
18+
*/
19+
public class FuzzyQueryBuilderProtoConverter implements QueryBuilderProtoConverter {
20+
21+
/**
22+
* Constructs a new FuzzyQueryBuilderProtoConverter.
23+
*/
24+
public FuzzyQueryBuilderProtoConverter() {
25+
// Default constructor
26+
}
27+
28+
@Override
29+
public QueryContainer.QueryContainerCase getHandledQueryCase() {
30+
return QueryContainer.QueryContainerCase.FUZZY;
31+
}
32+
33+
@Override
34+
public QueryBuilder fromProto(QueryContainer queryContainer) {
35+
if (queryContainer == null || !queryContainer.hasFuzzy()) {
36+
throw new IllegalArgumentException("QueryContainer does not contain a Fuzzy query");
37+
}
38+
39+
return FuzzyQueryBuilderProtoUtils.fromProto(queryContainer.getFuzzy());
40+
}
41+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,176 @@
1+
/*
2+
* SPDX-License-Identifier: Apache-2.0
3+
*
4+
* The OpenSearch Contributors require contributions made to
5+
* this file be licensed under the Apache-2.0 license or a
6+
* compatible open source license.
7+
*/
8+
package org.opensearch.transport.grpc.proto.request.search.query;
9+
10+
import org.opensearch.common.unit.Fuzziness;
11+
import org.opensearch.index.query.AbstractQueryBuilder;
12+
import org.opensearch.index.query.FuzzyQueryBuilder;
13+
import org.opensearch.protobufs.FuzzyQuery;
14+
15+
/**
16+
* Utility class for converting FuzzyQuery Protocol Buffers to OpenSearch objects.
17+
* This class provides methods to transform Protocol Buffer representations of fuzzy queries
18+
* into their corresponding OpenSearch FuzzyQueryBuilder implementations for search operations.
19+
*/
20+
public class FuzzyQueryBuilderProtoUtils {
21+
22+
private FuzzyQueryBuilderProtoUtils() {
23+
// Utility class, no instances
24+
}
25+
26+
/**
27+
* Converts a Protocol Buffer FuzzyQuery to an OpenSearch FuzzyQueryBuilder.
28+
* Similar to {@link FuzzyQueryBuilder#fromXContent(org.opensearch.core.xcontent.XContentParser)}, this method
29+
* parses the Protocol Buffer representation and creates a properly configured
30+
* FuzzyQueryBuilder with the appropriate field name, value, fuzziness, prefix length,
31+
* max expansions, transpositions, rewrite method, boost, and query name.
32+
*
33+
* @param fuzzyQueryProto The Protocol Buffer FuzzyQuery object
34+
* @return A configured FuzzyQueryBuilder instance
35+
* @throws IllegalArgumentException if the field name or value is null or empty
36+
*/
37+
public static FuzzyQueryBuilder fromProto(FuzzyQuery fuzzyQueryProto) {
38+
String fieldName = null;
39+
String value = null;
40+
String queryName = null;
41+
float boost = AbstractQueryBuilder.DEFAULT_BOOST;
42+
String fuzziness = null;
43+
int prefixLength = FuzzyQueryBuilder.DEFAULT_PREFIX_LENGTH;
44+
int maxExpansions = FuzzyQueryBuilder.DEFAULT_MAX_EXPANSIONS;
45+
boolean transpositions = FuzzyQueryBuilder.DEFAULT_TRANSPOSITIONS;
46+
String rewrite = null;
47+
48+
// Extract field name from the protobuf
49+
if (!fuzzyQueryProto.getField().isEmpty()) {
50+
fieldName = fuzzyQueryProto.getField();
51+
} else {
52+
throw new IllegalArgumentException("Field name cannot be null or empty for fuzzy query");
53+
}
54+
55+
// Extract value from the protobuf
56+
if (fuzzyQueryProto.hasValue()) {
57+
switch (fuzzyQueryProto.getValue().getValueCase()) {
58+
case STRING_VALUE:
59+
value = fuzzyQueryProto.getValue().getStringValue();
60+
break;
61+
case BOOL_VALUE:
62+
value = String.valueOf(fuzzyQueryProto.getValue().getBoolValue());
63+
break;
64+
case GENERAL_NUMBER:
65+
// Handle GeneralNumber case
66+
switch (fuzzyQueryProto.getValue().getGeneralNumber().getValueCase()) {
67+
case INT32_VALUE:
68+
value = String.valueOf(fuzzyQueryProto.getValue().getGeneralNumber().getInt32Value());
69+
break;
70+
case INT64_VALUE:
71+
value = String.valueOf(fuzzyQueryProto.getValue().getGeneralNumber().getInt64Value());
72+
break;
73+
case FLOAT_VALUE:
74+
value = String.valueOf(fuzzyQueryProto.getValue().getGeneralNumber().getFloatValue());
75+
break;
76+
case DOUBLE_VALUE:
77+
value = String.valueOf(fuzzyQueryProto.getValue().getGeneralNumber().getDoubleValue());
78+
break;
79+
default:
80+
throw new IllegalArgumentException("Unsupported GeneralNumber type for fuzzy query");
81+
}
82+
break;
83+
default:
84+
throw new IllegalArgumentException("Unsupported value type for fuzzy query");
85+
}
86+
} else {
87+
throw new IllegalArgumentException("Value cannot be null for fuzzy query");
88+
}
89+
90+
// Extract optional parameters
91+
if (fuzzyQueryProto.hasXName() && !fuzzyQueryProto.getXName().isEmpty()) {
92+
queryName = fuzzyQueryProto.getXName();
93+
}
94+
95+
if (fuzzyQueryProto.hasBoost()) {
96+
boost = fuzzyQueryProto.getBoost();
97+
}
98+
99+
// Extract fuzziness
100+
if (fuzzyQueryProto.hasFuzziness()) {
101+
switch (fuzzyQueryProto.getFuzziness().getFuzzinessCase()) {
102+
case STRING_VALUE:
103+
fuzziness = fuzzyQueryProto.getFuzziness().getStringValue();
104+
break;
105+
case INT32_VALUE:
106+
fuzziness = String.valueOf(fuzzyQueryProto.getFuzziness().getInt32Value());
107+
break;
108+
default:
109+
// Use default fuzziness
110+
break;
111+
}
112+
}
113+
114+
// Extract prefix length
115+
if (fuzzyQueryProto.getPrefixLength() != 0) {
116+
prefixLength = fuzzyQueryProto.getPrefixLength();
117+
}
118+
119+
// Extract max expansions
120+
if (fuzzyQueryProto.getMaxExpansions() != 0) {
121+
maxExpansions = fuzzyQueryProto.getMaxExpansions();
122+
}
123+
124+
// Extract transpositions
125+
transpositions = fuzzyQueryProto.getTranspositions();
126+
127+
// Extract rewrite method if specified
128+
if (fuzzyQueryProto.getRewrite() != FuzzyQuery.MultiTermQueryRewrite.MULTI_TERM_QUERY_REWRITE_UNSPECIFIED) {
129+
switch (fuzzyQueryProto.getRewrite()) {
130+
case MULTI_TERM_QUERY_REWRITE_CONSTANT_SCORE:
131+
rewrite = "constant_score";
132+
break;
133+
case MULTI_TERM_QUERY_REWRITE_CONSTANT_SCORE_BOOLEAN:
134+
rewrite = "constant_score_boolean";
135+
break;
136+
case MULTI_TERM_QUERY_REWRITE_SCORING_BOOLEAN:
137+
rewrite = "scoring_boolean";
138+
break;
139+
case MULTI_TERM_QUERY_REWRITE_TOP_TERMS_N:
140+
rewrite = "top_terms_N";
141+
break;
142+
case MULTI_TERM_QUERY_REWRITE_TOP_TERMS_BLENDED_FREQS_N:
143+
rewrite = "top_terms_blended_freqs_N";
144+
break;
145+
case MULTI_TERM_QUERY_REWRITE_TOP_TERMS_BOOST_N:
146+
rewrite = "top_terms_boost_N";
147+
break;
148+
default:
149+
// Use default rewrite method
150+
break;
151+
}
152+
}
153+
154+
// Create and configure FuzzyQueryBuilder
155+
FuzzyQueryBuilder fuzzyQuery = new FuzzyQueryBuilder(fieldName, value);
156+
fuzzyQuery.boost(boost);
157+
158+
if (fuzziness != null) {
159+
fuzzyQuery.fuzziness(Fuzziness.build(fuzziness));
160+
}
161+
162+
fuzzyQuery.prefixLength(prefixLength);
163+
fuzzyQuery.maxExpansions(maxExpansions);
164+
fuzzyQuery.transpositions(transpositions);
165+
166+
if (queryName != null) {
167+
fuzzyQuery.queryName(queryName);
168+
}
169+
170+
if (rewrite != null) {
171+
fuzzyQuery.rewrite(rewrite);
172+
}
173+
174+
return fuzzyQuery;
175+
}
176+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/*
2+
* SPDX-License-Identifier: Apache-2.0
3+
*
4+
* The OpenSearch Contributors require contributions made to
5+
* this file be licensed under the Apache-2.0 license or a
6+
* compatible open source license.
7+
*/
8+
package org.opensearch.transport.grpc.proto.request.search.query;
9+
10+
import org.opensearch.index.query.QueryBuilder;
11+
import org.opensearch.protobufs.QueryContainer;
12+
import org.opensearch.transport.grpc.spi.QueryBuilderProtoConverter;
13+
14+
/**
15+
* Converter for MatchBoolPrefix queries.
16+
* This class implements the QueryBuilderProtoConverter interface to provide MatchBoolPrefix query support
17+
* for the gRPC transport module.
18+
*/
19+
public class MatchBoolPrefixQueryBuilderProtoConverter implements QueryBuilderProtoConverter {
20+
21+
/**
22+
* Constructs a new MatchBoolPrefixQueryBuilderProtoConverter.
23+
*/
24+
public MatchBoolPrefixQueryBuilderProtoConverter() {
25+
// Default constructor
26+
}
27+
28+
@Override
29+
public QueryContainer.QueryContainerCase getHandledQueryCase() {
30+
return QueryContainer.QueryContainerCase.MATCH_BOOL_PREFIX;
31+
}
32+
33+
@Override
34+
public QueryBuilder fromProto(QueryContainer queryContainer) {
35+
if (queryContainer == null || !queryContainer.hasMatchBoolPrefix()) {
36+
throw new IllegalArgumentException("QueryContainer does not contain a MatchBoolPrefix query");
37+
}
38+
39+
return MatchBoolPrefixQueryBuilderProtoUtils.fromProto(queryContainer.getMatchBoolPrefix());
40+
}
41+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
/*
2+
* SPDX-License-Identifier: Apache-2.0
3+
*
4+
* The OpenSearch Contributors require contributions made to
5+
* this file be licensed under the Apache-2.0 license or a
6+
* compatible open source license.
7+
*/
8+
package org.opensearch.transport.grpc.proto.request.search.query;
9+
10+
import org.opensearch.index.query.AbstractQueryBuilder;
11+
import org.opensearch.index.query.MatchBoolPrefixQueryBuilder;
12+
import org.opensearch.index.query.Operator;
13+
import org.opensearch.protobufs.MatchBoolPrefixQuery;
14+
15+
/**
16+
* Utility class for converting MatchBoolPrefixQuery Protocol Buffers to OpenSearch objects.
17+
* This class provides methods to transform Protocol Buffer representations of match_bool_prefix queries
18+
* into their corresponding OpenSearch MatchBoolPrefixQueryBuilder implementations for search operations.
19+
*/
20+
public class MatchBoolPrefixQueryBuilderProtoUtils {
21+
22+
private MatchBoolPrefixQueryBuilderProtoUtils() {
23+
// Utility class, no instances
24+
}
25+
26+
/**
27+
* Converts a Protocol Buffer MatchBoolPrefixQuery to an OpenSearch MatchBoolPrefixQueryBuilder.
28+
* Similar to {@link MatchBoolPrefixQueryBuilder#fromXContent(org.opensearch.core.xcontent.XContentParser)}, this method
29+
* parses the Protocol Buffer representation and creates a properly configured
30+
* MatchBoolPrefixQueryBuilder with the appropriate field name, value, operator, analyzer,
31+
* minimum_should_match, boost, and query name.
32+
*
33+
* @param matchBoolPrefixQueryProto The Protocol Buffer MatchBoolPrefixQuery object
34+
* @return A configured MatchBoolPrefixQueryBuilder instance
35+
* @throws IllegalArgumentException if the field name or value is null or empty
36+
*/
37+
public static MatchBoolPrefixQueryBuilder fromProto(MatchBoolPrefixQuery matchBoolPrefixQueryProto) {
38+
String fieldName = null;
39+
String value = null;
40+
String queryName = null;
41+
float boost = AbstractQueryBuilder.DEFAULT_BOOST;
42+
Operator operator = Operator.OR; // Default operator for MatchBoolPrefixQuery
43+
String analyzer = null;
44+
String minimumShouldMatch = null;
45+
46+
// Extract field name from the protobuf
47+
if (!matchBoolPrefixQueryProto.getField().isEmpty()) {
48+
fieldName = matchBoolPrefixQueryProto.getField();
49+
} else {
50+
throw new IllegalArgumentException("Field name cannot be null or empty for match_bool_prefix query");
51+
}
52+
53+
// Extract query value from the protobuf
54+
if (!matchBoolPrefixQueryProto.getQuery().isEmpty()) {
55+
value = matchBoolPrefixQueryProto.getQuery();
56+
} else {
57+
throw new IllegalArgumentException("Query cannot be null or empty for match_bool_prefix query");
58+
}
59+
60+
// Extract optional parameters
61+
if (matchBoolPrefixQueryProto.hasXName() && !matchBoolPrefixQueryProto.getXName().isEmpty()) {
62+
queryName = matchBoolPrefixQueryProto.getXName();
63+
}
64+
65+
if (matchBoolPrefixQueryProto.hasBoost()) {
66+
boost = matchBoolPrefixQueryProto.getBoost();
67+
}
68+
69+
// Extract operator
70+
if (matchBoolPrefixQueryProto.getOperator() == org.opensearch.protobufs.Operator.OPERATOR_AND) {
71+
operator = Operator.AND;
72+
}
73+
74+
// Extract analyzer
75+
if (!matchBoolPrefixQueryProto.getAnalyzer().isEmpty()) {
76+
analyzer = matchBoolPrefixQueryProto.getAnalyzer();
77+
}
78+
79+
// Extract minimum_should_match
80+
if (matchBoolPrefixQueryProto.hasMinimumShouldMatch()) {
81+
switch (matchBoolPrefixQueryProto.getMinimumShouldMatch().getMinimumShouldMatchCase()) {
82+
case STRING_VALUE:
83+
minimumShouldMatch = matchBoolPrefixQueryProto.getMinimumShouldMatch().getStringValue();
84+
break;
85+
case INT32_VALUE:
86+
minimumShouldMatch = String.valueOf(matchBoolPrefixQueryProto.getMinimumShouldMatch().getInt32Value());
87+
break;
88+
default:
89+
// Use default minimum_should_match
90+
break;
91+
}
92+
}
93+
94+
// Create and configure MatchBoolPrefixQueryBuilder
95+
MatchBoolPrefixQueryBuilder matchBoolPrefixQuery = new MatchBoolPrefixQueryBuilder(fieldName, value);
96+
matchBoolPrefixQuery.boost(boost);
97+
matchBoolPrefixQuery.operator(operator);
98+
99+
if (analyzer != null) {
100+
matchBoolPrefixQuery.analyzer(analyzer);
101+
}
102+
103+
if (minimumShouldMatch != null) {
104+
matchBoolPrefixQuery.minimumShouldMatch(minimumShouldMatch);
105+
}
106+
107+
if (queryName != null) {
108+
matchBoolPrefixQuery.queryName(queryName);
109+
}
110+
111+
return matchBoolPrefixQuery;
112+
}
113+
}

0 commit comments

Comments
 (0)