Skip to content

Commit 2eb2551

Browse files
committed
[GRPC] Implement Boosting and BimpleQueryString queries
1 parent 672039d commit 2eb2551

File tree

12 files changed

+1305
-9
lines changed

12 files changed

+1305
-9
lines changed
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 Boosting queries.
17+
* This class implements the QueryBuilderProtoConverter interface to provide Boosting query support
18+
* for the gRPC transport module.
19+
*/
20+
public class BoostingQueryBuilderProtoConverter implements QueryBuilderProtoConverter {
21+
22+
/**
23+
* Default constructor for BoostingQueryBuilderProtoConverter.
24+
*/
25+
public BoostingQueryBuilderProtoConverter() {}
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.BOOSTING;
37+
}
38+
39+
@Override
40+
public QueryBuilder fromProto(QueryContainer queryContainer) {
41+
if (queryContainer == null || queryContainer.getQueryContainerCase() != QueryContainer.QueryContainerCase.BOOSTING) {
42+
throw new IllegalArgumentException("QueryContainer does not contain a Boosting query");
43+
}
44+
45+
return BoostingQueryBuilderProtoUtils.fromProto(queryContainer.getBoosting(), registry);
46+
}
47+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
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.BoostingQueryBuilder;
12+
import org.opensearch.index.query.QueryBuilder;
13+
import org.opensearch.protobufs.BoostingQuery;
14+
import org.opensearch.protobufs.QueryContainer;
15+
import org.opensearch.transport.grpc.spi.QueryBuilderProtoConverterRegistry;
16+
17+
/**
18+
* Utility class for converting BoostingQuery Protocol Buffers to OpenSearch query objects.
19+
* This class provides methods to transform Protocol Buffer representations of boosting queries
20+
* into their corresponding OpenSearch BoostingQueryBuilder implementations for search operations.
21+
*/
22+
class BoostingQueryBuilderProtoUtils {
23+
24+
private BoostingQueryBuilderProtoUtils() {
25+
// Utility class, no instances
26+
}
27+
28+
/**
29+
* Converts a Protocol Buffer BoostingQuery to an OpenSearch BoostingQueryBuilder.
30+
* Similar to {@link BoostingQueryBuilder#fromXContent(org.opensearch.core.xcontent.XContentParser)}, this method
31+
* parses the Protocol Buffer representation and creates a properly configured
32+
* BoostingQueryBuilder with the appropriate positive, negative, negative_boost, boost, and name settings.
33+
*
34+
* @param boostingQueryProto The Protocol Buffer BoostingQuery to convert
35+
* @param registry The registry to use for converting nested queries
36+
* @return A configured BoostingQueryBuilder instance
37+
* @throws IllegalArgumentException if required fields are missing
38+
*/
39+
static BoostingQueryBuilder fromProto(BoostingQuery boostingQueryProto, QueryBuilderProtoConverterRegistry registry) {
40+
// Extract fields in the order they appear in fromXContent
41+
QueryBuilder positiveQuery = null;
42+
QueryBuilder negativeQuery = null;
43+
float negativeBoost = -1;
44+
float boost = AbstractQueryBuilder.DEFAULT_BOOST;
45+
String queryName = null;
46+
47+
// Process positive query (required in proto)
48+
QueryContainer positiveContainer = boostingQueryProto.getPositive();
49+
positiveQuery = registry.fromProto(positiveContainer);
50+
if (positiveQuery == null) {
51+
throw new IllegalArgumentException(BoostingQueryBuilder.POSITIVE_QUERY_REQUIRED);
52+
}
53+
54+
// Process negative query (required in proto)
55+
QueryContainer negativeContainer = boostingQueryProto.getNegative();
56+
negativeQuery = registry.fromProto(negativeContainer);
57+
if (negativeQuery == null) {
58+
throw new IllegalArgumentException(BoostingQueryBuilder.NEGATIVE_QUERY_REQUIRED);
59+
}
60+
61+
// Process negative_boost (required in proto, but validate it's positive)
62+
negativeBoost = boostingQueryProto.getNegativeBoost();
63+
if (negativeBoost < 0) {
64+
throw new IllegalArgumentException(BoostingQueryBuilder.NEGATIVE_BOOST_POSITIVE_VALUE_REQUIRED);
65+
}
66+
67+
// Process boost (optional)
68+
if (boostingQueryProto.hasBoost()) {
69+
boost = boostingQueryProto.getBoost();
70+
}
71+
72+
// Process queryName (optional)
73+
if (boostingQueryProto.hasXName()) {
74+
queryName = boostingQueryProto.getXName();
75+
}
76+
77+
// Build the query in the same order as fromXContent
78+
BoostingQueryBuilder boostingQuery = new BoostingQueryBuilder(positiveQuery, negativeQuery);
79+
boostingQuery.negativeBoost(negativeBoost);
80+
boostingQuery.boost(boost);
81+
boostingQuery.queryName(queryName);
82+
83+
return boostingQuery;
84+
}
85+
}

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,8 @@ protected void registerBuiltInConverters() {
6969
delegate.registerConverter(new MatchBoolPrefixQueryBuilderProtoConverter());
7070
delegate.registerConverter(new MatchPhrasePrefixQueryBuilderProtoConverter());
7171
delegate.registerConverter(new FunctionScoreQueryBuilderProtoConverter());
72+
delegate.registerConverter(new BoostingQueryBuilderProtoConverter());
73+
delegate.registerConverter(new SimpleQueryStringBuilderProtoConverter());
7274

7375
// Set the registry on all converters so they can access each other
7476
delegate.setRegistryOnAllConverters(this);
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 SimpleQueryString queries.
17+
* This class implements the QueryBuilderProtoConverter interface to provide SimpleQueryString query support
18+
* for the gRPC transport module.
19+
*/
20+
public class SimpleQueryStringBuilderProtoConverter implements QueryBuilderProtoConverter {
21+
22+
/**
23+
* Default constructor for SimpleQueryStringBuilderProtoConverter.
24+
*/
25+
public SimpleQueryStringBuilderProtoConverter() {}
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.SIMPLE_QUERY_STRING;
37+
}
38+
39+
@Override
40+
public QueryBuilder fromProto(QueryContainer queryContainer) {
41+
if (queryContainer == null || queryContainer.getQueryContainerCase() != QueryContainer.QueryContainerCase.SIMPLE_QUERY_STRING) {
42+
throw new IllegalArgumentException("QueryContainer does not contain a SimpleQueryString query");
43+
}
44+
45+
return SimpleQueryStringBuilderProtoUtils.fromProto(queryContainer.getSimpleQueryString());
46+
}
47+
}

0 commit comments

Comments
 (0)