Skip to content

Commit 5da6ead

Browse files
authored
[GRPC] Implement GRPC ConstantScoreQuery, FuzzyQuery, MatchBoolPrefixQuery, MatchPhrasePrefix, PrefixQuery, MatchQuery (#19854)
* [GRPC] Implement GRPC ConstantScoreQuerym, FuzzyQuery, MatchBoolPrefixQuery, MatchPhrasePrefix, PrefixQuery, MatchQuery Signed-off-by: lucy66hw <[email protected]> Signed-off-by: xil <[email protected]> * Add changelog Signed-off-by: xil <[email protected]> * remove unused log Signed-off-by: xil <[email protected]> * Add missing javadoc Signed-off-by: xil <[email protected]> * Add UT Signed-off-by: xil <[email protected]> * spotlessApply Signed-off-by: xil <[email protected]> * Add UT Signed-off-by: xil <[email protected]> * update ut Signed-off-by: xil <[email protected]> --------- Signed-off-by: lucy66hw <[email protected]> Signed-off-by: xil <[email protected]>
1 parent f229797 commit 5da6ead

30 files changed

+2475
-12
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
1919
- Add cluster defaults for merge autoThrottle, maxMergeThreads, and maxMergeCount; Add segment size filter to the merged segment warmer ([#19629](https://github.com/opensearch-project/OpenSearch/pull/19629))
2020
- Add build-tooling to run in FIPS environment ([#18921](https://github.com/opensearch-project/OpenSearch/pull/18921))
2121
- Add SMILE/CBOR/YAML document format support to Bulk GRPC endpoint ([#19744](https://github.com/opensearch-project/OpenSearch/pull/19744))
22+
- Implement GRPC ConstantScoreQuery, FuzzyQuery, MatchBoolPrefixQuery, MatchPhrasePrefix, PrefixQuery, MatchQuery ([#19854](https://github.com/opensearch-project/OpenSearch/pull/19854))
2223

2324
### Changed
2425
- Faster `terms` query creation for `keyword` field with index and docValues enabled ([#19350](https://github.com/opensearch-project/OpenSearch/pull/19350))

modules/transport-grpc/src/main/java/org/opensearch/transport/grpc/proto/request/search/OperatorProtoUtils.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ private OperatorProtoUtils() {
2626
* @param op
2727
* @return
2828
*/
29-
protected static Operator fromEnum(org.opensearch.protobufs.Operator op) {
29+
public static Operator fromEnum(org.opensearch.protobufs.Operator op) {
3030
switch (op) {
3131
case OPERATOR_AND:
3232
return Operator.AND;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
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+
import org.opensearch.transport.grpc.spi.QueryBuilderProtoConverterRegistry;
14+
15+
/**
16+
* Converter for ConstantScore queries.
17+
* This class implements the QueryBuilderProtoConverter interface to provide ConstantScore query support
18+
* for the gRPC transport module.
19+
*/
20+
public class ConstantScoreQueryBuilderProtoConverter implements QueryBuilderProtoConverter {
21+
22+
/**
23+
* Default constructor for ConstantScoreQueryBuilderProtoConverter.
24+
*/
25+
public ConstantScoreQueryBuilderProtoConverter() {}
26+
27+
private QueryBuilderProtoConverterRegistry registry;
28+
29+
@Override
30+
public void setRegistry(QueryBuilderProtoConverterRegistry registry) {
31+
this.registry = registry;
32+
}
33+
34+
@Override
35+
public QueryContainer.QueryContainerCase getHandledQueryCase() {
36+
return QueryContainer.QueryContainerCase.CONSTANT_SCORE;
37+
}
38+
39+
@Override
40+
public QueryBuilder fromProto(QueryContainer queryContainer) {
41+
if (queryContainer == null || queryContainer.getQueryContainerCase() != QueryContainer.QueryContainerCase.CONSTANT_SCORE) {
42+
throw new IllegalArgumentException("QueryContainer does not contain a ConstantScore query");
43+
}
44+
45+
return ConstantScoreQueryBuilderProtoUtils.fromProto(queryContainer.getConstantScore(), registry);
46+
}
47+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
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.ConstantScoreQueryBuilder;
12+
import org.opensearch.index.query.QueryBuilder;
13+
import org.opensearch.protobufs.ConstantScoreQuery;
14+
import org.opensearch.transport.grpc.spi.QueryBuilderProtoConverterRegistry;
15+
16+
/**
17+
* Utility class for converting ConstantScoreQuery Protocol Buffers to OpenSearch objects.
18+
* This class provides methods to transform Protocol Buffer representations of constant score queries
19+
* into their corresponding OpenSearch ConstantScoreQueryBuilder implementations for search operations.
20+
*/
21+
class ConstantScoreQueryBuilderProtoUtils {
22+
23+
private ConstantScoreQueryBuilderProtoUtils() {
24+
// Utility class, no instances
25+
}
26+
27+
/**
28+
* Converts a Protocol Buffer ConstantScoreQuery to an OpenSearch ConstantScoreQueryBuilder.
29+
* Similar to {@link ConstantScoreQueryBuilder#fromXContent(org.opensearch.core.xcontent.XContentParser)}, this method
30+
* parses the Protocol Buffer representation and creates a properly configured
31+
* ConstantScoreQueryBuilder with the appropriate filter query, boost, and query name.
32+
*
33+
* @param constantScoreQueryProto The Protocol Buffer ConstantScoreQuery object
34+
* @param registry The registry to use for converting the nested filter query
35+
* @return A configured ConstantScoreQueryBuilder instance
36+
*/
37+
static ConstantScoreQueryBuilder fromProto(ConstantScoreQuery constantScoreQueryProto, QueryBuilderProtoConverterRegistry registry) {
38+
if (registry == null) {
39+
throw new IllegalArgumentException("QueryBuilderProtoConverterRegistry cannot be null");
40+
}
41+
String queryName = null;
42+
float boost = AbstractQueryBuilder.DEFAULT_BOOST;
43+
44+
if (!constantScoreQueryProto.hasFilter()) {
45+
throw new IllegalArgumentException("ConstantScore query must have a filter query");
46+
}
47+
48+
QueryBuilder query = registry.fromProto(constantScoreQueryProto.getFilter());
49+
if (query == null) {
50+
throw new IllegalArgumentException("Filter query cannot be null for constant_score query");
51+
}
52+
53+
if (constantScoreQueryProto.hasBoost()) {
54+
boost = constantScoreQueryProto.getBoost();
55+
}
56+
57+
if (constantScoreQueryProto.hasXName()) {
58+
queryName = constantScoreQueryProto.getXName();
59+
}
60+
61+
ConstantScoreQueryBuilder constantScoreBuilder = new ConstantScoreQueryBuilder(query);
62+
constantScoreBuilder.boost(boost);
63+
constantScoreBuilder.queryName(queryName);
64+
return constantScoreBuilder;
65+
}
66+
}
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,95 @@
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+
import org.opensearch.protobufs.MultiTermQueryRewrite;
15+
import org.opensearch.transport.grpc.proto.response.common.FieldValueProtoUtils;
16+
import org.opensearch.transport.grpc.util.ProtobufEnumUtils;
17+
18+
/**
19+
* Utility class for converting FuzzyQuery Protocol Buffers to OpenSearch objects.
20+
* This class provides methods to transform Protocol Buffer representations of fuzzy queries
21+
* into their corresponding OpenSearch FuzzyQueryBuilder implementations for search operations.
22+
*/
23+
class FuzzyQueryBuilderProtoUtils {
24+
25+
private FuzzyQueryBuilderProtoUtils() {
26+
// Utility class, no instances
27+
}
28+
29+
/**
30+
* Converts a Protocol Buffer FuzzyQuery to an OpenSearch FuzzyQueryBuilder.
31+
* Similar to {@link FuzzyQueryBuilder#fromXContent(org.opensearch.core.xcontent.XContentParser)}, this method
32+
* parses the Protocol Buffer representation and creates a properly configured
33+
* FuzzyQueryBuilder with the appropriate field name, value, fuzziness, prefix length,
34+
* max expansions, transpositions, rewrite method, boost, and query name.
35+
*
36+
* @param fuzzyQueryProto The Protocol Buffer FuzzyQuery object
37+
* @return A configured FuzzyQueryBuilder instance
38+
* @throws IllegalArgumentException if the field name or value is null or empty
39+
*/
40+
static FuzzyQueryBuilder fromProto(FuzzyQuery fuzzyQueryProto) {
41+
String fieldName = fuzzyQueryProto.getField();
42+
Object value = FieldValueProtoUtils.fromProto(fuzzyQueryProto.getValue(), false);
43+
Fuzziness fuzziness = FuzzyQueryBuilder.DEFAULT_FUZZINESS;
44+
int prefixLength = FuzzyQueryBuilder.DEFAULT_PREFIX_LENGTH;
45+
int maxExpansions = FuzzyQueryBuilder.DEFAULT_MAX_EXPANSIONS;
46+
boolean transpositions = FuzzyQueryBuilder.DEFAULT_TRANSPOSITIONS;
47+
String rewrite = null;
48+
String queryName = null;
49+
float boost = AbstractQueryBuilder.DEFAULT_BOOST;
50+
51+
if (fuzzyQueryProto.hasBoost()) {
52+
boost = fuzzyQueryProto.getBoost();
53+
}
54+
55+
if (fuzzyQueryProto.hasFuzziness()) {
56+
org.opensearch.protobufs.Fuzziness fuzzinessProto = fuzzyQueryProto.getFuzziness();
57+
if (fuzzinessProto.hasString()) {
58+
fuzziness = Fuzziness.build(fuzzinessProto.getString());
59+
} else if (fuzzinessProto.hasInt32()) {
60+
fuzziness = Fuzziness.fromEdits(fuzzinessProto.getInt32());
61+
}
62+
}
63+
64+
if (fuzzyQueryProto.hasPrefixLength()) {
65+
prefixLength = fuzzyQueryProto.getPrefixLength();
66+
}
67+
68+
if (fuzzyQueryProto.hasMaxExpansions()) {
69+
maxExpansions = fuzzyQueryProto.getMaxExpansions();
70+
}
71+
72+
if (fuzzyQueryProto.hasTranspositions()) {
73+
transpositions = fuzzyQueryProto.getTranspositions();
74+
}
75+
76+
if (fuzzyQueryProto.hasRewrite()) {
77+
MultiTermQueryRewrite rewriteEnum = fuzzyQueryProto.getRewrite();
78+
if (rewriteEnum != MultiTermQueryRewrite.MULTI_TERM_QUERY_REWRITE_UNSPECIFIED) {
79+
rewrite = ProtobufEnumUtils.convertToString(rewriteEnum);
80+
}
81+
}
82+
83+
if (fuzzyQueryProto.hasXName()) {
84+
queryName = fuzzyQueryProto.getXName();
85+
}
86+
87+
return new FuzzyQueryBuilder(fieldName, value).fuzziness(fuzziness)
88+
.prefixLength(prefixLength)
89+
.maxExpansions(maxExpansions)
90+
.transpositions(transpositions)
91+
.rewrite(rewrite)
92+
.boost(boost)
93+
.queryName(queryName);
94+
}
95+
}
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+
}

0 commit comments

Comments
 (0)