Skip to content
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
- Add all-active ingestion as docrep equivalent in pull-based ingestion ([#19316](https://github.com/opensearch-project/OpenSearch/pull/19316))
- Adding logic for histogram aggregation using skiplist ([#19130](https://github.com/opensearch-project/OpenSearch/pull/19130))
- Add skip_list param for date, scaled float and token count fields ([#19142](https://github.com/opensearch-project/OpenSearch/pull/19142))
- Add GRPC Exists, Regexp, and Wildcard queries ([#19392](https://github.com/opensearch-project/OpenSearch/pull/19392))

### Changed
- Refactor `if-else` chains to use `Java 17 pattern matching switch expressions`(([#18965](https://github.com/opensearch-project/OpenSearch/pull/18965))
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*
* SPDX-License-Identifier: Apache-2.0
*
* The OpenSearch Contributors require contributions made to
* this file be licensed under the Apache-2.0 license or a
* compatible open source license.
*/
package org.opensearch.transport.grpc.proto.request.search.query;

import org.opensearch.index.query.QueryBuilder;
import org.opensearch.protobufs.QueryContainer;
import org.opensearch.transport.grpc.spi.QueryBuilderProtoConverter;

/**
* Converter for Exists queries.
* This class implements the QueryBuilderProtoConverter interface to provide Exists query support
* for the gRPC transport module.
*/
public class ExistsQueryBuilderProtoConverter implements QueryBuilderProtoConverter {

/**
* Constructs a new ExistsQueryBuilderProtoConverter.
*/
public ExistsQueryBuilderProtoConverter() {
// Default constructor
}

@Override
public QueryContainer.QueryContainerCase getHandledQueryCase() {
return QueryContainer.QueryContainerCase.EXISTS;
}

@Override
public QueryBuilder fromProto(QueryContainer queryContainer) {
if (queryContainer == null || !queryContainer.hasExists()) {
throw new IllegalArgumentException("QueryContainer does not contain an Exists query");
}

return ExistsQueryBuilderProtoUtils.fromProto(queryContainer.getExists());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/*
* SPDX-License-Identifier: Apache-2.0
*
* The OpenSearch Contributors require contributions made to
* this file be licensed under the Apache-2.0 license or a
* compatible open source license.
*/
package org.opensearch.transport.grpc.proto.request.search.query;

import org.opensearch.core.xcontent.XContentParser;
import org.opensearch.index.query.ExistsQueryBuilder;
import org.opensearch.protobufs.ExistsQuery;

/**
* Utility class for converting ExistsQuery Protocol Buffers to OpenSearch objects.
* This class provides methods to transform Protocol Buffer representations of exists queries
* into their corresponding OpenSearch ExistsQueryBuilder implementations for search operations.
*/
class ExistsQueryBuilderProtoUtils {

private ExistsQueryBuilderProtoUtils() {
// Utility class, no instances
}

/**
* Converts a Protocol Buffer ExistsQuery to an OpenSearch ExistsQueryBuilder.
* Similar to {@link ExistsQueryBuilder#fromXContent(XContentParser)}, this method
* parses the Protocol Buffer representation and creates a properly configured
* ExistsQueryBuilder with the appropriate field name, boost, and query name.
*
* @param existsQueryProto The Protocol Buffer ExistsQuery object
* @return A configured ExistsQueryBuilder instance
* @throws IllegalArgumentException if the exists query is null or missing required fields
*/
static ExistsQueryBuilder fromProto(ExistsQuery existsQueryProto) {
if (existsQueryProto == null) {
throw new IllegalArgumentException("ExistsQuery cannot be null");
}

String field = existsQueryProto.getField();

ExistsQueryBuilder existsQueryBuilder = new ExistsQueryBuilder(field);

// Set optional parameters
if (existsQueryProto.hasBoost()) {
existsQueryBuilder.boost(existsQueryProto.getBoost());
}

if (existsQueryProto.hasXName()) {
existsQueryBuilder.queryName(existsQueryProto.getXName());
}

return existsQueryBuilder;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
/**
* Utility class for converting MatchAllQuery Protocol Buffers to OpenSearch query objects.
*/
public class MatchAllQueryBuilderProtoUtils {
class MatchAllQueryBuilderProtoUtils {

private MatchAllQueryBuilderProtoUtils() {
// Utility class, no instances
Expand All @@ -29,7 +29,7 @@ private MatchAllQueryBuilderProtoUtils() {
* @param matchAllQueryProto The Protocol Buffer MatchAllQuery to convert
* @return A configured MatchAllQueryBuilder instance
*/
protected static MatchAllQueryBuilder fromProto(MatchAllQuery matchAllQueryProto) {
static MatchAllQueryBuilder fromProto(MatchAllQuery matchAllQueryProto) {
MatchAllQueryBuilder matchAllQueryBuilder = new MatchAllQueryBuilder();

if (matchAllQueryProto.hasBoost()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
* This class provides methods to transform Protocol Buffer representations of match_none queries
* into their corresponding OpenSearch MatchNoneQueryBuilder implementations for search operations.
*/
public class MatchNoneQueryBuilderProtoUtils {
class MatchNoneQueryBuilderProtoUtils {

private MatchNoneQueryBuilderProtoUtils() {
// Utility class, no instances
Expand All @@ -31,7 +31,7 @@ private MatchNoneQueryBuilderProtoUtils() {
* @param matchNoneQueryProto The Protocol Buffer MatchNoneQuery to convert
* @return A configured MatchNoneQueryBuilder instance
*/
protected static MatchNoneQueryBuilder fromProto(MatchNoneQuery matchNoneQueryProto) {
static MatchNoneQueryBuilder fromProto(MatchNoneQuery matchNoneQueryProto) {
MatchNoneQueryBuilder matchNoneQueryBuilder = new MatchNoneQueryBuilder();

if (matchNoneQueryProto.hasBoost()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,9 @@ protected void registerBuiltInConverters() {
delegate.registerConverter(new MatchNoneQueryBuilderProtoConverter());
delegate.registerConverter(new TermQueryBuilderProtoConverter());
delegate.registerConverter(new TermsQueryBuilderProtoConverter());
delegate.registerConverter(new ExistsQueryBuilderProtoConverter());
delegate.registerConverter(new RegexpQueryBuilderProtoConverter());
delegate.registerConverter(new WildcardQueryBuilderProtoConverter());

logger.info("Registered {} built-in query converters", delegate.size());
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*
* SPDX-License-Identifier: Apache-2.0
*
* The OpenSearch Contributors require contributions made to
* this file be licensed under the Apache-2.0 license or a
* compatible open source license.
*/
package org.opensearch.transport.grpc.proto.request.search.query;

import org.opensearch.index.query.QueryBuilder;
import org.opensearch.protobufs.QueryContainer;
import org.opensearch.transport.grpc.spi.QueryBuilderProtoConverter;

/**
* Converter for Regexp queries.
* This class implements the QueryBuilderProtoConverter interface to provide Regexp query support
* for the gRPC transport module.
*/
public class RegexpQueryBuilderProtoConverter implements QueryBuilderProtoConverter {

/**
* Constructs a new RegexpQueryBuilderProtoConverter.
*/
public RegexpQueryBuilderProtoConverter() {
// Default constructor
}

@Override
public QueryContainer.QueryContainerCase getHandledQueryCase() {
return QueryContainer.QueryContainerCase.REGEXP;
}

@Override
public QueryBuilder fromProto(QueryContainer queryContainer) {
if (queryContainer == null || !queryContainer.hasRegexp()) {
throw new IllegalArgumentException("QueryContainer does not contain a Regexp query");
}

return RegexpQueryBuilderProtoUtils.fromProto(queryContainer.getRegexp());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
/*
* SPDX-License-Identifier: Apache-2.0
*
* The OpenSearch Contributors require contributions made to
* this file be licensed under the Apache-2.0 license or a
* compatible open source license.
*/
package org.opensearch.transport.grpc.proto.request.search.query;

import org.opensearch.core.xcontent.XContentParser;
import org.opensearch.index.query.RegexpQueryBuilder;
import org.opensearch.protobufs.RegexpQuery;

import java.util.Locale;

/**
* Utility class for converting RegexpQuery Protocol Buffers to OpenSearch objects.
* This class provides methods to transform Protocol Buffer representations of regexp queries
* into their corresponding OpenSearch RegexpQueryBuilder implementations for search operations.
*/
class RegexpQueryBuilderProtoUtils {

private RegexpQueryBuilderProtoUtils() {
// Utility class, no instances
}

/**
* Converts a Protocol Buffer RegexpQuery to an OpenSearch RegexpQueryBuilder.
* Similar to {@link RegexpQueryBuilder#fromXContent(XContentParser)}, this method
* parses the Protocol Buffer representation and creates a properly configured
* RegexpQueryBuilder with the appropriate field name, value, boost, query name,
* flags, case sensitivity, max determinized states, and rewrite method.
*
* @param regexpQueryProto The Protocol Buffer RegexpQuery object
* @return A configured RegexpQueryBuilder instance
* @throws IllegalArgumentException if the regexp query is null or missing required fields
*/
static RegexpQueryBuilder fromProto(RegexpQuery regexpQueryProto) {
String field = regexpQueryProto.getField();
String value = regexpQueryProto.getValue();

RegexpQueryBuilder regexpQueryBuilder = new RegexpQueryBuilder(field, value);

// Set optional parameters
if (regexpQueryProto.hasBoost()) {
regexpQueryBuilder.boost(regexpQueryProto.getBoost());
}

if (regexpQueryProto.hasXName()) {
regexpQueryBuilder.queryName(regexpQueryProto.getXName());
}

if (regexpQueryProto.hasFlags()) {
// Convert string flags to integer value using RegexpFlag.resolveValue
regexpQueryBuilder.flags(org.opensearch.index.query.RegexpFlag.resolveValue(regexpQueryProto.getFlags()));
}

if (regexpQueryProto.hasCaseInsensitive()) {
regexpQueryBuilder.caseInsensitive(regexpQueryProto.getCaseInsensitive());
}

if (regexpQueryProto.hasMaxDeterminizedStates()) {
regexpQueryBuilder.maxDeterminizedStates(regexpQueryProto.getMaxDeterminizedStates());
}

if (regexpQueryProto.hasRewrite()) {
RegexpQuery.MultiTermQueryRewrite rewriteEnum = regexpQueryProto.getRewrite();

// Skip setting rewrite method if it's UNSPECIFIED
if (rewriteEnum != RegexpQuery.MultiTermQueryRewrite.MULTI_TERM_QUERY_REWRITE_UNSPECIFIED) {
String rewriteMethod = rewriteEnum.name();
// Remove the prefix and convert to lowercase to match expected format
rewriteMethod = rewriteMethod.replace("MULTI_TERM_QUERY_REWRITE_", "").toLowerCase(Locale.ROOT);
regexpQueryBuilder.rewrite(rewriteMethod);
}
}

return regexpQueryBuilder;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
* This class provides methods to transform Protocol Buffer representations of term queries
* into their corresponding OpenSearch TermQueryBuilder implementations for search operations.
*/
public class TermQueryBuilderProtoUtils {
class TermQueryBuilderProtoUtils {

private TermQueryBuilderProtoUtils() {
// Utility class, no instances
Expand All @@ -36,7 +36,7 @@ private TermQueryBuilderProtoUtils() {
* @return A configured TermQueryBuilder instance
* @throws IllegalArgumentException if the field value type is not supported, or if the term query field value is not recognized
*/
protected static TermQueryBuilder fromProto(TermQuery termQueryProto) {
static TermQueryBuilder fromProto(TermQuery termQueryProto) {
String queryName = null;
String fieldName = termQueryProto.getField();
Object value = null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
* This class provides methods to transform Protocol Buffer representations of terms queries
* into their corresponding OpenSearch TermsQueryBuilder implementations for search operations.
*/
public class TermsQueryBuilderProtoUtils {
class TermsQueryBuilderProtoUtils {

private TermsQueryBuilderProtoUtils() {
// Utility class, no instances
Expand All @@ -42,7 +42,7 @@ private TermsQueryBuilderProtoUtils() {
* @return A configured TermQueryBuilder instance
* @throws IllegalArgumentException if the terms query is invalid or missing required fields
*/
public static TermsQueryBuilder fromProto(org.opensearch.protobufs.TermsQuery termsQueryProto) {
static TermsQueryBuilder fromProto(org.opensearch.protobufs.TermsQuery termsQueryProto) {
if (termsQueryProto == null) {
throw new IllegalArgumentException("TermsQuery must not be null");
}
Expand Down Expand Up @@ -83,7 +83,7 @@ public static TermsQueryBuilder fromProto(org.opensearch.protobufs.TermsQuery te
* @return A configured TermQueryBuilder instance
* @throws IllegalArgumentException if the term query field value is not recognized
*/
protected static TermsQueryBuilder fromProto(TermsQueryField termsQueryProto) {
static TermsQueryBuilder fromProto(TermsQueryField termsQueryProto) {
String fieldName = null;
List<Object> values = null;
TermsLookup termsLookup = null;
Expand Down Expand Up @@ -124,7 +124,7 @@ protected static TermsQueryBuilder fromProto(TermsQueryField termsQueryProto) {
* @return configured TermsQueryBuilder
* @throws IllegalArgumentException if neither values nor lookup is set, or if bitmap validation fails
*/
protected static TermsQueryBuilder fromProto(
static TermsQueryBuilder fromProto(
String fieldName,
org.opensearch.protobufs.TermsQueryField termsQueryField,
org.opensearch.protobufs.TermsQueryValueType valueTypeProto
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*
* SPDX-License-Identifier: Apache-2.0
*
* The OpenSearch Contributors require contributions made to
* this file be licensed under the Apache-2.0 license or a
* compatible open source license.
*/
package org.opensearch.transport.grpc.proto.request.search.query;

import org.opensearch.index.query.QueryBuilder;
import org.opensearch.protobufs.QueryContainer;
import org.opensearch.transport.grpc.spi.QueryBuilderProtoConverter;

/**
* Converter for Wildcard queries.
* This class implements the QueryBuilderProtoConverter interface to provide Wildcard query support
* for the gRPC transport module.
*/
public class WildcardQueryBuilderProtoConverter implements QueryBuilderProtoConverter {

/**
* Constructs a new WildcardQueryBuilderProtoConverter.
*/
public WildcardQueryBuilderProtoConverter() {
// Default constructor
}

@Override
public QueryContainer.QueryContainerCase getHandledQueryCase() {
return QueryContainer.QueryContainerCase.WILDCARD;
}

@Override
public QueryBuilder fromProto(QueryContainer queryContainer) {
if (queryContainer == null || !queryContainer.hasWildcard()) {
throw new IllegalArgumentException("QueryContainer does not contain a Wildcard query");
}

return WildcardQueryBuilderProtoUtils.fromProto(queryContainer.getWildcard());
}
}
Loading
Loading