From 0c69cd22f8ffe749b81855854e3ba2403c7db8e3 Mon Sep 17 00:00:00 2001 From: Finn Carroll Date: Wed, 27 Nov 2024 12:32:19 -0800 Subject: [PATCH] Move query definitions to Query.proto. Add basic term query. Signed-off-by: Finn Carroll --- .../search/SearchRequestBodyProtoHelper.java | 63 +++++++++++++++---- .../org/opensearch/action/search/Query.proto | 55 ++++++++++++++++ .../org/opensearch/action/search/Search.proto | 14 +---- 3 files changed, 107 insertions(+), 25 deletions(-) create mode 100644 server/src/main/proto/org/opensearch/action/search/Query.proto diff --git a/server/src/main/java/org/opensearch/grpc/services/search/SearchRequestBodyProtoHelper.java b/server/src/main/java/org/opensearch/grpc/services/search/SearchRequestBodyProtoHelper.java index 26708fb70ec5a..cec810fcab480 100644 --- a/server/src/main/java/org/opensearch/grpc/services/search/SearchRequestBodyProtoHelper.java +++ b/server/src/main/java/org/opensearch/grpc/services/search/SearchRequestBodyProtoHelper.java @@ -8,28 +8,67 @@ package org.opensearch.grpc.services.search; +import org.opensearch.action.search.proto.TermQuery; import org.opensearch.index.query.MatchAllQueryBuilder; +import org.opensearch.index.query.TermQueryBuilder; public class SearchRequestBodyProtoHelper { public static org.opensearch.search.builder.SearchSourceBuilder searchSourceBuilderFromProto(org.opensearch.action.search.proto.SearchRequest searchRequestProto, org.opensearch.action.search.proto.SearchRequestBody searchRequestBodyProto) { org.opensearch.search.builder.SearchSourceBuilder sourceBuilder = new org.opensearch.search.builder.SearchSourceBuilder(); + org.opensearch.action.search.proto.QueryContainer queryContainer = searchRequestProto.getRequestBody().getQuery(); - /* - message SearchRequestBody { - optional QueryContainer query = 1; + switch (queryContainer.getQueryCase()) { + case MATCH_ALL: + sourceBuilder.query(new MatchAllQueryBuilder()); + break; + case TERM: + sourceBuilder.query(getTermQuery(queryContainer)); + break; + case QUERY_NOT_SET: + throw new IllegalArgumentException("SearchBody set but no query specified"); } - message QueryContainer { - optional MatchAllQuery match_all = 1; + + return sourceBuilder; + } + + private static TermQueryBuilder getTermQuery(org.opensearch.action.search.proto.QueryContainer queryContainer) { + org.opensearch.action.search.proto.TermQuery termQuery = queryContainer.getTerm(); + TermQuery.FieldValue termQueryVal = termQuery.getVal(); + String field = termQuery.getField(); + + TermQueryBuilder builder = null; + switch (termQueryVal.getTypedValCase()) { + case STRVAL: + builder = new TermQueryBuilder(field, termQueryVal.getStrVal()); + break; + case INTVAL: + builder = new TermQueryBuilder(field, termQueryVal.getIntVal()); + break; + case DOUBLEVAL: + builder = new TermQueryBuilder(field, termQueryVal.getDoubleVal()); + break; + case BOOLVAL: + builder = new TermQueryBuilder(field, termQueryVal.getBoolVal()); + break; + case BYTEVAL: + builder = new TermQueryBuilder(field, termQueryVal.getByteVal()); + break; + case TYPEDVAL_NOT_SET: + throw new IllegalArgumentException("TermQuery body field value type not set"); + default: + throw new IllegalArgumentException("TermQuery field value not recognized"); } - message MatchAllQuery { - optional float boost = 1; - optional string name = 2; + + if (termQuery.hasName()) { + builder.queryName(termQuery.getName()); } - */ - // TODO: Support more queries - sourceBuilder.query(new MatchAllQueryBuilder()); - return sourceBuilder; + if (termQuery.hasBoost()) { + builder.boost(termQuery.getBoost()); + } + + builder.caseInsensitive(termQuery.getCaseInsensitive()); + return builder; } } diff --git a/server/src/main/proto/org/opensearch/action/search/Query.proto b/server/src/main/proto/org/opensearch/action/search/Query.proto new file mode 100644 index 0000000000000..7419004bede37 --- /dev/null +++ b/server/src/main/proto/org/opensearch/action/search/Query.proto @@ -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. + * + * Modifications Copyright OpenSearch Contributors. See + * GitHub history for details. + */ + +syntax = "proto3"; + +package org.opensearch.action.search.proto; + +import "google/protobuf/struct.proto"; + +option java_multiple_files = true; +option java_outer_classname = "QueryProto"; + +message QueryContainer { + oneof query { + MatchAllQuery match_all = 1; + TermQuery term = 2; + } +} + +message MatchAllQuery { + // Query name for query tagging + optional string name = 1; + // Boosts the clause by the given multiplier. Default is 1. + optional float boost = 2; +} + +message TermQuery { + // Query name for query tagging + optional string name = 1; + // Boosts the clause by the given multiplier. Default is 1. + optional float boost = 2; + // Field to match on. + string field = 3; + // Value to match. + FieldValue val = 4; + message FieldValue { + oneof TypedVal { + string strVal = 1; + int64 intVal = 2; + double doubleVal = 3; + bool boolVal = 4; + bytes byteVal = 5; + } + } + // Defaults to false. + bool case_insensitive = 5; +} diff --git a/server/src/main/proto/org/opensearch/action/search/Search.proto b/server/src/main/proto/org/opensearch/action/search/Search.proto index 26f906a9a3e45..86f92b56e4379 100644 --- a/server/src/main/proto/org/opensearch/action/search/Search.proto +++ b/server/src/main/proto/org/opensearch/action/search/Search.proto @@ -14,6 +14,7 @@ syntax = "proto3"; package org.opensearch.action.search.proto; import "google/protobuf/struct.proto"; +import "org/opensearch/action/search/Query.proto"; option java_multiple_files = true; option java_outer_classname = "SearchProto"; @@ -79,7 +80,6 @@ message SearchRequest { optional int32 size = 33; // [optional] Search Request body optional SearchRequestBody request_body = 47; - } message SearchRequestBody { @@ -87,18 +87,6 @@ message SearchRequestBody { optional QueryContainer query = 1; } -message QueryContainer { - // The match all query returns all documents. This query can be useful in testing large document sets if you need to return the entire set. - optional MatchAllQuery match_all = 1; -} - -message MatchAllQuery { - // Boosts the clause by the given multiplier. Useful for weighing clauses in compound queries. Values in the [0, 1) range decrease relevance, and values greater than 1 increase relevance. Default is 1. - optional float boost = 1; - // Query name for query tagging - optional string name = 2; -} - message SearchResponse { // [required] Milliseconds it took Elasticsearch to execute the request. optional int64 took = 1;