-
Notifications
You must be signed in to change notification settings - Fork 2.3k
[GRPC][Joining queries] Implement GRPC Nested query, fix InnerHits, and bump to 0.19.0 protobufs #19453
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
[GRPC][Joining queries] Implement GRPC Nested query, fix InnerHits, and bump to 0.19.0 protobufs #19453
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
28b984c
upgrade to protobufs 0.18.0
karenyrx fef4f49
add tests
karenyrx 75b6f38
[GRPC][Joining queries] Implement GRPC Nested query
karenyrx f21aaa9
package private, remove static registry and add tests
karenyrx 4d61048
Merge remote-tracking branch 'ossupstream/main' into 0.18.0_nested
karenyrx ddb9e81
spotless
karenyrx 4aa1ae3
add changelog
karenyrx 7709cd3
add tests
karenyrx 73d5d17
inner hits fix and update to 0.19.0
karenyrx bde6d68
Merge branch 'main' into 0.18.0_nested
karenyrx File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| 14b425a0cb280ccad20983daeaabf3d1c83c3670 |
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| 14b425a0cb280ccad20983daeaabf3d1c83c3670 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
47 changes: 47 additions & 0 deletions
47
...pensearch/transport/grpc/proto/request/search/query/NestedQueryBuilderProtoConverter.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,47 @@ | ||
| /* | ||
| * 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; | ||
| import org.opensearch.transport.grpc.spi.QueryBuilderProtoConverterRegistry; | ||
|
|
||
| /** | ||
| * Converter for NestedQuery protobuf messages to OpenSearch QueryBuilder objects. | ||
| * Handles the conversion of nested query protobuf messages to OpenSearch NestedQueryBuilder. | ||
| */ | ||
| public class NestedQueryBuilderProtoConverter implements QueryBuilderProtoConverter { | ||
|
|
||
| /** | ||
| * Default constructor for NestedQueryBuilderProtoConverter. | ||
| */ | ||
| public NestedQueryBuilderProtoConverter() {} | ||
|
|
||
| private QueryBuilderProtoConverterRegistry registry; | ||
|
|
||
| @Override | ||
| public void setRegistry(QueryBuilderProtoConverterRegistry registry) { | ||
| this.registry = registry; | ||
| // The utility class no longer stores the registry statically, it's passed directly to fromProto | ||
| } | ||
|
|
||
| @Override | ||
| public QueryContainer.QueryContainerCase getHandledQueryCase() { | ||
| return QueryContainer.QueryContainerCase.NESTED; | ||
| } | ||
|
|
||
| @Override | ||
| public QueryBuilder fromProto(QueryContainer queryContainer) { | ||
| if (queryContainer == null || queryContainer.getQueryContainerCase() != QueryContainer.QueryContainerCase.NESTED) { | ||
| throw new IllegalArgumentException("QueryContainer must contain a NestedQuery"); | ||
| } | ||
| return NestedQueryBuilderProtoUtils.fromProto(queryContainer.getNested(), registry); | ||
| } | ||
| } |
123 changes: 123 additions & 0 deletions
123
...rg/opensearch/transport/grpc/proto/request/search/query/NestedQueryBuilderProtoUtils.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,123 @@ | ||
| /* | ||
| * 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.apache.lucene.search.join.ScoreMode; | ||
| import org.opensearch.index.query.AbstractQueryBuilder; | ||
| import org.opensearch.index.query.InnerHitBuilder; | ||
| import org.opensearch.index.query.NestedQueryBuilder; | ||
| import org.opensearch.index.query.QueryBuilder; | ||
| import org.opensearch.protobufs.ChildScoreMode; | ||
| import org.opensearch.protobufs.NestedQuery; | ||
| import org.opensearch.protobufs.QueryContainer; | ||
| import org.opensearch.transport.grpc.proto.request.search.InnerHitsBuilderProtoUtils; | ||
| import org.opensearch.transport.grpc.spi.QueryBuilderProtoConverterRegistry; | ||
|
|
||
| /** | ||
| * Utility class for converting protobuf NestedQuery to OpenSearch NestedQueryBuilder. | ||
| * Handles the conversion of nested query protobuf messages to OpenSearch NestedQueryBuilder objects. | ||
| */ | ||
| class NestedQueryBuilderProtoUtils { | ||
|
|
||
| private NestedQueryBuilderProtoUtils() { | ||
| // Utility class, no instances | ||
| } | ||
|
|
||
| /** | ||
| * Converts a protobuf NestedQuery to an OpenSearch NestedQueryBuilder. | ||
| * Similar to {@link NestedQueryBuilder#fromXContent(org.opensearch.core.xcontent.XContentParser)}, this method | ||
| * parses the Protocol Buffer representation and creates a properly configured | ||
| * NestedQueryBuilder with the appropriate path, query, score mode, inner hits, boost, and query name. | ||
| * | ||
| * @param nestedQueryProto the protobuf NestedQuery to convert | ||
| * @param registry The registry to use for converting nested queries | ||
| * @return the converted OpenSearch NestedQueryBuilder | ||
| * @throws IllegalArgumentException if the protobuf query is invalid | ||
| */ | ||
| static NestedQueryBuilder fromProto(NestedQuery nestedQueryProto, QueryBuilderProtoConverterRegistry registry) { | ||
| if (nestedQueryProto == null) { | ||
| throw new IllegalArgumentException("NestedQuery cannot be null"); | ||
| } | ||
|
|
||
| float boost = AbstractQueryBuilder.DEFAULT_BOOST; | ||
| ScoreMode scoreMode = ScoreMode.Avg; | ||
| String queryName = null; | ||
| QueryBuilder query = null; | ||
| InnerHitBuilder innerHitBuilder = null; | ||
| boolean ignoreUnmapped = NestedQueryBuilder.DEFAULT_IGNORE_UNMAPPED; | ||
|
|
||
| String path = nestedQueryProto.getPath(); | ||
| if (path.isEmpty()) { | ||
| throw new IllegalArgumentException("Path is required for NestedQuery"); | ||
| } | ||
|
|
||
| if (!nestedQueryProto.hasQuery()) { | ||
| throw new IllegalArgumentException("Query is required for NestedQuery"); | ||
| } | ||
| try { | ||
| QueryContainer queryContainer = nestedQueryProto.getQuery(); | ||
| query = registry.fromProto(queryContainer); | ||
| } catch (Exception e) { | ||
| throw new IllegalArgumentException("Failed to convert inner query for NestedQuery: " + e.getMessage(), e); | ||
| } | ||
|
|
||
| if (nestedQueryProto.hasScoreMode()) { | ||
| scoreMode = parseScoreMode(nestedQueryProto.getScoreMode()); | ||
| } | ||
|
|
||
| if (nestedQueryProto.hasIgnoreUnmapped()) { | ||
| ignoreUnmapped = nestedQueryProto.getIgnoreUnmapped(); | ||
| } | ||
|
|
||
| if (nestedQueryProto.hasBoost()) { | ||
| boost = nestedQueryProto.getBoost(); | ||
| } | ||
|
|
||
| if (nestedQueryProto.hasXName()) { | ||
| queryName = nestedQueryProto.getXName(); | ||
| } | ||
|
|
||
| if (nestedQueryProto.hasInnerHits()) { | ||
| try { | ||
| innerHitBuilder = InnerHitsBuilderProtoUtils.fromProto(nestedQueryProto.getInnerHits()); | ||
| } catch (Exception e) { | ||
| throw new IllegalArgumentException("Failed to convert inner hits for NestedQuery: " + e.getMessage(), e); | ||
| } | ||
| } | ||
|
|
||
| NestedQueryBuilder queryBuilder = new NestedQueryBuilder(path, query, scoreMode, innerHitBuilder).ignoreUnmapped(ignoreUnmapped) | ||
| .queryName(queryName) | ||
| .boost(boost); | ||
|
|
||
| return queryBuilder; | ||
| } | ||
|
|
||
| /** | ||
| * Converts protobuf ChildScoreMode to Lucene ScoreMode. | ||
| * | ||
| * @param childScoreMode the protobuf ChildScoreMode | ||
| * @return the converted Lucene ScoreMode | ||
| */ | ||
| private static ScoreMode parseScoreMode(ChildScoreMode childScoreMode) { | ||
| switch (childScoreMode) { | ||
| case CHILD_SCORE_MODE_AVG: | ||
| return ScoreMode.Avg; | ||
| case CHILD_SCORE_MODE_MAX: | ||
| return ScoreMode.Max; | ||
| case CHILD_SCORE_MODE_MIN: | ||
| return ScoreMode.Min; | ||
| case CHILD_SCORE_MODE_NONE: | ||
| return ScoreMode.None; | ||
| case CHILD_SCORE_MODE_SUM: | ||
| return ScoreMode.Total; | ||
| default: | ||
| return ScoreMode.Avg; // Default value | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
79 changes: 79 additions & 0 deletions
79
...ava/org/opensearch/transport/grpc/proto/request/search/FieldAndFormatProtoUtilsTests.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,79 @@ | ||
| /* | ||
| * 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; | ||
|
|
||
| import org.opensearch.search.fetch.subphase.FieldAndFormat; | ||
| import org.opensearch.test.OpenSearchTestCase; | ||
|
|
||
| /** | ||
| * Unit tests for FieldAndFormatProtoUtils. | ||
| */ | ||
| public class FieldAndFormatProtoUtilsTests extends OpenSearchTestCase { | ||
|
|
||
| public void testFromProto_WithFieldOnly() { | ||
| org.opensearch.protobufs.FieldAndFormat fieldAndFormatProto = org.opensearch.protobufs.FieldAndFormat.newBuilder() | ||
| .setField("test_field") | ||
| .build(); | ||
|
|
||
| FieldAndFormat result = FieldAndFormatProtoUtils.fromProto(fieldAndFormatProto); | ||
|
|
||
| assertNotNull(result); | ||
| assertEquals("test_field", result.field); | ||
| assertNull(result.format); | ||
| } | ||
|
|
||
| public void testFromProto_WithFieldAndFormat() { | ||
| org.opensearch.protobufs.FieldAndFormat fieldAndFormatProto = org.opensearch.protobufs.FieldAndFormat.newBuilder() | ||
| .setField("date_field") | ||
| .setFormat("yyyy-MM-dd") | ||
| .build(); | ||
|
|
||
| FieldAndFormat result = FieldAndFormatProtoUtils.fromProto(fieldAndFormatProto); | ||
|
|
||
| assertNotNull(result); | ||
| assertEquals("date_field", result.field); | ||
| assertEquals("yyyy-MM-dd", result.format); | ||
| } | ||
|
|
||
| public void testFromProto_WithEmptyFieldName() { | ||
| org.opensearch.protobufs.FieldAndFormat fieldAndFormatProto = org.opensearch.protobufs.FieldAndFormat.newBuilder() | ||
| .setField("") | ||
| .setFormat("yyyy-MM-dd") | ||
| .build(); | ||
|
|
||
| try { | ||
| FieldAndFormatProtoUtils.fromProto(fieldAndFormatProto); | ||
| fail("Should have thrown IllegalArgumentException for empty field name"); | ||
| } catch (IllegalArgumentException e) { | ||
| assertEquals("Field name cannot be null or empty", e.getMessage()); | ||
| } | ||
| } | ||
|
|
||
| public void testFromProto_WithNullProtobuf() { | ||
| try { | ||
| FieldAndFormatProtoUtils.fromProto(null); | ||
| fail("Should have thrown IllegalArgumentException for null protobuf"); | ||
| } catch (IllegalArgumentException e) { | ||
| assertEquals("FieldAndFormat protobuf cannot be null", e.getMessage()); | ||
| } | ||
| } | ||
|
|
||
| public void testFromProto_WithComplexFormat() { | ||
| org.opensearch.protobufs.FieldAndFormat fieldAndFormatProto = org.opensearch.protobufs.FieldAndFormat.newBuilder() | ||
| .setField("timestamp") | ||
| .setFormat("epoch_millis") | ||
| .build(); | ||
|
|
||
| FieldAndFormat result = FieldAndFormatProtoUtils.fromProto(fieldAndFormatProto); | ||
|
|
||
| assertNotNull(result); | ||
| assertEquals("timestamp", result.field); | ||
| assertEquals("epoch_millis", result.format); | ||
| } | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.