Skip to content

Commit

Permalink
Move query definitions to Query.proto. Add basic term query.
Browse files Browse the repository at this point in the history
Signed-off-by: Finn Carroll <[email protected]>
  • Loading branch information
finnegancarroll committed Nov 27, 2024
1 parent 609e675 commit 0c69cd2
Show file tree
Hide file tree
Showing 3 changed files with 107 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
55 changes: 55 additions & 0 deletions server/src/main/proto/org/opensearch/action/search/Query.proto
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.
*
* 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;
}
14 changes: 1 addition & 13 deletions server/src/main/proto/org/opensearch/action/search/Search.proto
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -79,26 +80,13 @@ message SearchRequest {
optional int32 size = 33;
// [optional] Search Request body
optional SearchRequestBody request_body = 47;

}

message SearchRequestBody {
// [optional] The DSL query to use in the request.
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;
Expand Down

0 comments on commit 0c69cd2

Please sign in to comment.