Skip to content

Commit 75fd8a6

Browse files
karenyrxkarenx
andauthored
[GRPC][Term-level queries] Add Exists, Regexp, and Wildcard queries (#19392)
* [GRPC] Implement Exists, Ids, Regexp, Wildcard queries Signed-off-by: karenx <[email protected]> * changelog Signed-off-by: karenx <[email protected]> * remove idsquery Signed-off-by: Karen X <[email protected]> * package private Signed-off-by: Karen X <[email protected]> * compile with 0.18.0 protos Signed-off-by: Karen X <[email protected]> * spotlessApply Signed-off-by: Karen X <[email protected]> * add tests Signed-off-by: Karen X <[email protected]> * spotless Signed-off-by: Karen X <[email protected]> --------- Signed-off-by: karenx <[email protected]> Signed-off-by: Karen X <[email protected]> Co-authored-by: karenx <[email protected]>
1 parent 39849f9 commit 75fd8a6

20 files changed

+1241
-10
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 Exists queries.
16+
* This class implements the QueryBuilderProtoConverter interface to provide Exists query support
17+
* for the gRPC transport module.
18+
*/
19+
public class ExistsQueryBuilderProtoConverter implements QueryBuilderProtoConverter {
20+
21+
/**
22+
* Constructs a new ExistsQueryBuilderProtoConverter.
23+
*/
24+
public ExistsQueryBuilderProtoConverter() {
25+
// Default constructor
26+
}
27+
28+
@Override
29+
public QueryContainer.QueryContainerCase getHandledQueryCase() {
30+
return QueryContainer.QueryContainerCase.EXISTS;
31+
}
32+
33+
@Override
34+
public QueryBuilder fromProto(QueryContainer queryContainer) {
35+
if (queryContainer == null || !queryContainer.hasExists()) {
36+
throw new IllegalArgumentException("QueryContainer does not contain an Exists query");
37+
}
38+
39+
return ExistsQueryBuilderProtoUtils.fromProto(queryContainer.getExists());
40+
}
41+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
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.core.xcontent.XContentParser;
11+
import org.opensearch.index.query.ExistsQueryBuilder;
12+
import org.opensearch.protobufs.ExistsQuery;
13+
14+
/**
15+
* Utility class for converting ExistsQuery Protocol Buffers to OpenSearch objects.
16+
* This class provides methods to transform Protocol Buffer representations of exists queries
17+
* into their corresponding OpenSearch ExistsQueryBuilder implementations for search operations.
18+
*/
19+
class ExistsQueryBuilderProtoUtils {
20+
21+
private ExistsQueryBuilderProtoUtils() {
22+
// Utility class, no instances
23+
}
24+
25+
/**
26+
* Converts a Protocol Buffer ExistsQuery to an OpenSearch ExistsQueryBuilder.
27+
* Similar to {@link ExistsQueryBuilder#fromXContent(XContentParser)}, this method
28+
* parses the Protocol Buffer representation and creates a properly configured
29+
* ExistsQueryBuilder with the appropriate field name, boost, and query name.
30+
*
31+
* @param existsQueryProto The Protocol Buffer ExistsQuery object
32+
* @return A configured ExistsQueryBuilder instance
33+
* @throws IllegalArgumentException if the exists query is null or missing required fields
34+
*/
35+
static ExistsQueryBuilder fromProto(ExistsQuery existsQueryProto) {
36+
if (existsQueryProto == null) {
37+
throw new IllegalArgumentException("ExistsQuery cannot be null");
38+
}
39+
40+
String field = existsQueryProto.getField();
41+
42+
ExistsQueryBuilder existsQueryBuilder = new ExistsQueryBuilder(field);
43+
44+
// Set optional parameters
45+
if (existsQueryProto.hasBoost()) {
46+
existsQueryBuilder.boost(existsQueryProto.getBoost());
47+
}
48+
49+
if (existsQueryProto.hasXName()) {
50+
existsQueryBuilder.queryName(existsQueryProto.getXName());
51+
}
52+
53+
return existsQueryBuilder;
54+
}
55+
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
/**
1515
* Utility class for converting MatchAllQuery Protocol Buffers to OpenSearch query objects.
1616
*/
17-
public class MatchAllQueryBuilderProtoUtils {
17+
class MatchAllQueryBuilderProtoUtils {
1818

1919
private MatchAllQueryBuilderProtoUtils() {
2020
// Utility class, no instances
@@ -29,7 +29,7 @@ private MatchAllQueryBuilderProtoUtils() {
2929
* @param matchAllQueryProto The Protocol Buffer MatchAllQuery to convert
3030
* @return A configured MatchAllQueryBuilder instance
3131
*/
32-
protected static MatchAllQueryBuilder fromProto(MatchAllQuery matchAllQueryProto) {
32+
static MatchAllQueryBuilder fromProto(MatchAllQuery matchAllQueryProto) {
3333
MatchAllQueryBuilder matchAllQueryBuilder = new MatchAllQueryBuilder();
3434

3535
if (matchAllQueryProto.hasBoost()) {

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
* This class provides methods to transform Protocol Buffer representations of match_none queries
1717
* into their corresponding OpenSearch MatchNoneQueryBuilder implementations for search operations.
1818
*/
19-
public class MatchNoneQueryBuilderProtoUtils {
19+
class MatchNoneQueryBuilderProtoUtils {
2020

2121
private MatchNoneQueryBuilderProtoUtils() {
2222
// Utility class, no instances
@@ -31,7 +31,7 @@ private MatchNoneQueryBuilderProtoUtils() {
3131
* @param matchNoneQueryProto The Protocol Buffer MatchNoneQuery to convert
3232
* @return A configured MatchNoneQueryBuilder instance
3333
*/
34-
protected static MatchNoneQueryBuilder fromProto(MatchNoneQuery matchNoneQueryProto) {
34+
static MatchNoneQueryBuilder fromProto(MatchNoneQuery matchNoneQueryProto) {
3535
MatchNoneQueryBuilder matchNoneQueryBuilder = new MatchNoneQueryBuilder();
3636

3737
if (matchNoneQueryProto.hasBoost()) {

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,9 @@ protected void registerBuiltInConverters() {
5252
delegate.registerConverter(new MultiMatchQueryBuilderProtoConverter());
5353
delegate.registerConverter(new BoolQueryBuilderProtoConverter());
5454
delegate.registerConverter(new ScriptQueryBuilderProtoConverter());
55+
delegate.registerConverter(new ExistsQueryBuilderProtoConverter());
56+
delegate.registerConverter(new RegexpQueryBuilderProtoConverter());
57+
delegate.registerConverter(new WildcardQueryBuilderProtoConverter());
5558

5659
// Set the registry on all converters so they can access each other
5760
delegate.setRegistryOnAllConverters(this);
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 Regexp queries.
16+
* This class implements the QueryBuilderProtoConverter interface to provide Regexp query support
17+
* for the gRPC transport module.
18+
*/
19+
public class RegexpQueryBuilderProtoConverter implements QueryBuilderProtoConverter {
20+
21+
/**
22+
* Constructs a new RegexpQueryBuilderProtoConverter.
23+
*/
24+
public RegexpQueryBuilderProtoConverter() {
25+
// Default constructor
26+
}
27+
28+
@Override
29+
public QueryContainer.QueryContainerCase getHandledQueryCase() {
30+
return QueryContainer.QueryContainerCase.REGEXP;
31+
}
32+
33+
@Override
34+
public QueryBuilder fromProto(QueryContainer queryContainer) {
35+
if (queryContainer == null || !queryContainer.hasRegexp()) {
36+
throw new IllegalArgumentException("QueryContainer does not contain a Regexp query");
37+
}
38+
39+
return RegexpQueryBuilderProtoUtils.fromProto(queryContainer.getRegexp());
40+
}
41+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
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.core.xcontent.XContentParser;
11+
import org.opensearch.index.query.AbstractQueryBuilder;
12+
import org.opensearch.index.query.RegexpQueryBuilder;
13+
import org.opensearch.protobufs.MultiTermQueryRewrite;
14+
import org.opensearch.protobufs.RegexpQuery;
15+
import org.opensearch.transport.grpc.util.ProtobufEnumUtils;
16+
17+
/**
18+
* Utility class for converting RegexpQuery Protocol Buffers to OpenSearch objects.
19+
* This class provides methods to transform Protocol Buffer representations of regexp queries
20+
* into their corresponding OpenSearch RegexpQueryBuilder implementations for search operations.
21+
*/
22+
class RegexpQueryBuilderProtoUtils {
23+
24+
private RegexpQueryBuilderProtoUtils() {
25+
// Utility class, no instances
26+
}
27+
28+
/**
29+
* Converts a Protocol Buffer RegexpQuery to an OpenSearch RegexpQueryBuilder.
30+
* Similar to {@link RegexpQueryBuilder#fromXContent(XContentParser)}, this method
31+
* parses the Protocol Buffer representation and creates a properly configured
32+
* RegexpQueryBuilder with the appropriate field name, value, boost, query name,
33+
* flags, case sensitivity, max determinized states, and rewrite method.
34+
*
35+
* @param regexpQueryProto The Protocol Buffer RegexpQuery object
36+
* @return A configured RegexpQueryBuilder instance
37+
* @throws IllegalArgumentException if the regexp query is null or missing required fields
38+
*/
39+
static RegexpQueryBuilder fromProto(RegexpQuery regexpQueryProto) {
40+
String fieldName = regexpQueryProto.getField();
41+
String rewrite = null;
42+
String value = regexpQueryProto.getValue();
43+
float boost = AbstractQueryBuilder.DEFAULT_BOOST;
44+
int flagsValue = RegexpQueryBuilder.DEFAULT_FLAGS_VALUE;
45+
boolean caseInsensitive = RegexpQueryBuilder.DEFAULT_CASE_INSENSITIVITY;
46+
int maxDeterminizedStates = RegexpQueryBuilder.DEFAULT_DETERMINIZE_WORK_LIMIT;
47+
String queryName = null;
48+
49+
if (regexpQueryProto.hasBoost()) {
50+
boost = regexpQueryProto.getBoost();
51+
}
52+
53+
if (regexpQueryProto.hasRewrite()) {
54+
MultiTermQueryRewrite rewriteEnum = regexpQueryProto.getRewrite();
55+
// Skip setting rewrite method if it's UNSPECIFIED
56+
if (rewriteEnum != MultiTermQueryRewrite.MULTI_TERM_QUERY_REWRITE_UNSPECIFIED) {
57+
rewrite = ProtobufEnumUtils.convertToString(rewriteEnum);
58+
}
59+
}
60+
61+
if (regexpQueryProto.hasFlags()) {
62+
// Convert string flags to integer value using RegexpFlag.resolveValue
63+
flagsValue = org.opensearch.index.query.RegexpFlag.resolveValue(regexpQueryProto.getFlags());
64+
}
65+
66+
if (regexpQueryProto.hasMaxDeterminizedStates()) {
67+
maxDeterminizedStates = regexpQueryProto.getMaxDeterminizedStates();
68+
}
69+
70+
if (regexpQueryProto.hasCaseInsensitive()) {
71+
caseInsensitive = regexpQueryProto.getCaseInsensitive();
72+
}
73+
74+
if (regexpQueryProto.hasXName()) {
75+
queryName = regexpQueryProto.getXName();
76+
}
77+
78+
RegexpQueryBuilder result = new RegexpQueryBuilder(fieldName, value).flags(flagsValue)
79+
.maxDeterminizedStates(maxDeterminizedStates)
80+
.rewrite(rewrite)
81+
.boost(boost)
82+
.queryName(queryName);
83+
result.caseInsensitive(caseInsensitive);
84+
return result;
85+
}
86+
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
* This class provides methods to transform Protocol Buffer representations of term queries
2020
* into their corresponding OpenSearch TermQueryBuilder implementations for search operations.
2121
*/
22-
public class TermQueryBuilderProtoUtils {
22+
class TermQueryBuilderProtoUtils {
2323

2424
private TermQueryBuilderProtoUtils() {
2525
// Utility class, no instances
@@ -36,7 +36,7 @@ private TermQueryBuilderProtoUtils() {
3636
* @return A configured TermQueryBuilder instance
3737
* @throws IllegalArgumentException if the field value type is not supported, or if the term query field value is not recognized
3838
*/
39-
protected static TermQueryBuilder fromProto(TermQuery termQueryProto) {
39+
static TermQueryBuilder fromProto(TermQuery termQueryProto) {
4040
String queryName = null;
4141
String fieldName = termQueryProto.getField();
4242
Object value = null;

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
* This class provides methods to transform Protocol Buffer representations of terms queries
2626
* into their corresponding OpenSearch TermsQueryBuilder implementations for search operations.
2727
*/
28-
public class TermsQueryBuilderProtoUtils {
28+
class TermsQueryBuilderProtoUtils {
2929

3030
private TermsQueryBuilderProtoUtils() {
3131
// Utility class, no instances
@@ -42,7 +42,7 @@ private TermsQueryBuilderProtoUtils() {
4242
* @return A configured TermQueryBuilder instance
4343
* @throws IllegalArgumentException if the terms query is invalid or missing required fields
4444
*/
45-
public static TermsQueryBuilder fromProto(org.opensearch.protobufs.TermsQuery termsQueryProto) {
45+
static TermsQueryBuilder fromProto(org.opensearch.protobufs.TermsQuery termsQueryProto) {
4646
if (termsQueryProto == null) {
4747
throw new IllegalArgumentException("TermsQuery must not be null");
4848
}
@@ -83,7 +83,7 @@ public static TermsQueryBuilder fromProto(org.opensearch.protobufs.TermsQuery te
8383
* @return A configured TermQueryBuilder instance
8484
* @throws IllegalArgumentException if the term query field value is not recognized
8585
*/
86-
protected static TermsQueryBuilder fromProto(TermsQueryField termsQueryProto) {
86+
static TermsQueryBuilder fromProto(TermsQueryField termsQueryProto) {
8787
String fieldName = null;
8888
List<Object> values = null;
8989
TermsLookup termsLookup = null;
@@ -124,7 +124,7 @@ protected static TermsQueryBuilder fromProto(TermsQueryField termsQueryProto) {
124124
* @return configured TermsQueryBuilder
125125
* @throws IllegalArgumentException if neither values nor lookup is set, or if bitmap validation fails
126126
*/
127-
protected static TermsQueryBuilder fromProto(
127+
static TermsQueryBuilder fromProto(
128128
String fieldName,
129129
org.opensearch.protobufs.TermsQueryField termsQueryField,
130130
org.opensearch.protobufs.TermsQueryValueType valueTypeProto
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 Wildcard queries.
16+
* This class implements the QueryBuilderProtoConverter interface to provide Wildcard query support
17+
* for the gRPC transport module.
18+
*/
19+
public class WildcardQueryBuilderProtoConverter implements QueryBuilderProtoConverter {
20+
21+
/**
22+
* Constructs a new WildcardQueryBuilderProtoConverter.
23+
*/
24+
public WildcardQueryBuilderProtoConverter() {
25+
// Default constructor
26+
}
27+
28+
@Override
29+
public QueryContainer.QueryContainerCase getHandledQueryCase() {
30+
return QueryContainer.QueryContainerCase.WILDCARD;
31+
}
32+
33+
@Override
34+
public QueryBuilder fromProto(QueryContainer queryContainer) {
35+
if (queryContainer == null || !queryContainer.hasWildcard()) {
36+
throw new IllegalArgumentException("QueryContainer does not contain a Wildcard query");
37+
}
38+
39+
return WildcardQueryBuilderProtoUtils.fromProto(queryContainer.getWildcard());
40+
}
41+
}

0 commit comments

Comments
 (0)