diff --git a/server/src/main/java/org/opensearch/transport/protobuf/FetchSearchResultProtobuf.java b/server/src/main/java/org/opensearch/transport/protobuf/FetchSearchResultProtobuf.java index f5533a5dceac7..a2b57b0dc6997 100644 --- a/server/src/main/java/org/opensearch/transport/protobuf/FetchSearchResultProtobuf.java +++ b/server/src/main/java/org/opensearch/transport/protobuf/FetchSearchResultProtobuf.java @@ -10,8 +10,8 @@ import org.opensearch.core.common.io.stream.StreamInput; import org.opensearch.core.common.io.stream.StreamOutput; -import org.opensearch.search.fetch.FetchSearchResult; import org.opensearch.proto.search.FetchSearchResultProtoDef.FetchSearchResultProto; +import org.opensearch.search.fetch.FetchSearchResult; import java.io.IOException; diff --git a/server/src/main/java/org/opensearch/transport/protobuf/ProtoSerDeHelpers.java b/server/src/main/java/org/opensearch/transport/protobuf/ProtoSerDeHelpers.java index b5c7088fa9374..dca3b3693ba2a 100644 --- a/server/src/main/java/org/opensearch/transport/protobuf/ProtoSerDeHelpers.java +++ b/server/src/main/java/org/opensearch/transport/protobuf/ProtoSerDeHelpers.java @@ -19,18 +19,18 @@ import org.opensearch.core.common.text.Text; import org.opensearch.core.index.Index; import org.opensearch.core.index.shard.ShardId; -import org.opensearch.search.SearchShardTarget; -import org.opensearch.search.fetch.subphase.highlight.HighlightField; import org.opensearch.proto.search.SearchHitsProtoDef.DocumentFieldProto; import org.opensearch.proto.search.SearchHitsProtoDef.ExplanationProto; +import org.opensearch.proto.search.SearchHitsProtoDef.GenericObjectProto; import org.opensearch.proto.search.SearchHitsProtoDef.HighlightFieldProto; import org.opensearch.proto.search.SearchHitsProtoDef.IndexProto; +import org.opensearch.proto.search.SearchHitsProtoDef.MissingValueProto; import org.opensearch.proto.search.SearchHitsProtoDef.SearchShardTargetProto; import org.opensearch.proto.search.SearchHitsProtoDef.ShardIdProto; import org.opensearch.proto.search.SearchHitsProtoDef.SortFieldProto; import org.opensearch.proto.search.SearchHitsProtoDef.SortTypeProto; -import org.opensearch.proto.search.SearchHitsProtoDef.GenericObjectProto; -import org.opensearch.proto.search.SearchHitsProtoDef.MissingValueProto; +import org.opensearch.search.SearchShardTarget; +import org.opensearch.search.fetch.subphase.highlight.HighlightField; import org.opensearch.transport.TransportSerializationException; import java.io.IOException; @@ -198,10 +198,7 @@ public static SortField sortFieldFromProto(SortFieldProto proto) { field = proto.getField(); } - SortField sortField = new SortField( - field, - sortTypeFromProto(proto.getType()), - proto.getReverse()); + SortField sortField = new SortField(field, sortTypeFromProto(proto.getType()), proto.getReverse()); if (proto.hasMissingValue()) { sortField.setMissingValue(missingValueFromProto(proto.getMissingValue())); @@ -235,8 +232,12 @@ public static MissingValueProto missingValueToProto(Object missingValue) { public static Object missingValueFromProto(MissingValueProto proto) { switch (proto.getValueCase()) { case INT_VAL: - if (proto.getIntVal() == 1) { return SortField.STRING_FIRST; } - if (proto.getIntVal() == 2) { return SortField.STRING_LAST; } + if (proto.getIntVal() == 1) { + return SortField.STRING_FIRST; + } + if (proto.getIntVal() == 2) { + return SortField.STRING_LAST; + } throw new TransportSerializationException("Unexpected sortField missingValue (INT_VAL): " + proto.getIntVal()); case OBJ_VAL: return genericObjectFromProto(proto.getObjVal()); diff --git a/server/src/main/java/org/opensearch/transport/protobuf/SearchHitProtobuf.java b/server/src/main/java/org/opensearch/transport/protobuf/SearchHitProtobuf.java index d77b450331d28..5e4253b6db83e 100644 --- a/server/src/main/java/org/opensearch/transport/protobuf/SearchHitProtobuf.java +++ b/server/src/main/java/org/opensearch/transport/protobuf/SearchHitProtobuf.java @@ -15,9 +15,9 @@ import org.opensearch.core.common.io.stream.StreamOutput; import org.opensearch.core.common.text.Text; import org.opensearch.proto.search.SearchHitsProtoDef; -import org.opensearch.search.SearchHit; -import org.opensearch.proto.search.SearchHitsProtoDef.SearchHitProto; import org.opensearch.proto.search.SearchHitsProtoDef.NestedIdentityProto; +import org.opensearch.proto.search.SearchHitsProtoDef.SearchHitProto; +import org.opensearch.search.SearchHit; import org.opensearch.search.SearchSortValues; import org.opensearch.transport.TransportSerializationException; @@ -77,14 +77,28 @@ SearchHitProto toProto() { metaFields.forEach((key, value) -> builder.putMetaFields(key, documentFieldToProto(value))); matchedQueries.forEach(builder::putMatchedQueries); - if (highlightFields != null) { highlightFields.forEach((key, value) -> builder.putHighlightFields(key, highlightFieldToProto(value))); } - if (innerHits != null) { innerHits.forEach((key, value) -> builder.putInnerHits(key, new SearchHitsProtobuf(value).toProto())); } + if (highlightFields != null) { + highlightFields.forEach((key, value) -> builder.putHighlightFields(key, highlightFieldToProto(value))); + } + if (innerHits != null) { + innerHits.forEach((key, value) -> builder.putInnerHits(key, new SearchHitsProtobuf(value).toProto())); + } - if (source != null) { builder.setSource(ByteString.copyFrom(source.toBytesRef().bytes)); } - if (id != null) { builder.setId(id.string()); } - if (nestedIdentity != null) { builder.setNestedIdentity(nestedIdentityToProto(nestedIdentity)); } - if (shard != null) { builder.setShard(searchShardTargetToProto(shard)); } - if (explanation != null) { builder.setExplanation(explanationToProto(explanation)); } + if (source != null) { + builder.setSource(ByteString.copyFrom(source.toBytesRef().bytes)); + } + if (id != null) { + builder.setId(id.string()); + } + if (nestedIdentity != null) { + builder.setNestedIdentity(nestedIdentityToProto(nestedIdentity)); + } + if (shard != null) { + builder.setShard(searchShardTargetToProto(shard)); + } + if (explanation != null) { + builder.setExplanation(explanationToProto(explanation)); + } return builder.build(); } @@ -116,10 +130,10 @@ void fromProto(SearchHitProto proto) { proto.getInnerHitsMap().forEach((key, value) -> innerHits.put(key, new SearchHitsProtobuf(value))); } - source = proto.hasSource()? BytesReference.fromByteBuffer(proto.getSource().asReadOnlyByteBuffer()) : null; - id = proto.hasId()? new Text(proto.getId()) : null; - nestedIdentity = proto.hasNestedIdentity()? nestedIdentityFromProto(proto.getNestedIdentity()) : null; - explanation = proto.hasExplanation()? explanationFromProto(proto.getExplanation()) : null; + source = proto.hasSource() ? BytesReference.fromByteBuffer(proto.getSource().asReadOnlyByteBuffer()) : null; + id = proto.hasId() ? new Text(proto.getId()) : null; + nestedIdentity = proto.hasNestedIdentity() ? nestedIdentityFromProto(proto.getNestedIdentity()) : null; + explanation = proto.hasExplanation() ? explanationFromProto(proto.getExplanation()) : null; if (proto.hasShard()) { shard = searchShardTargetFromProto(proto.getShard()); @@ -170,7 +184,8 @@ public static SearchHitsProtoDef.SearchSortValuesProto searchSortValuesToProto(S return builder.build(); } - public static SearchSortValues searchSortValuesFromProto(SearchHitsProtoDef.SearchSortValuesProto proto) throws TransportSerializationException { + public static SearchSortValues searchSortValuesFromProto(SearchHitsProtoDef.SearchSortValuesProto proto) + throws TransportSerializationException { Object[] formattedSortValues = new Object[proto.getFormattedSortValuesCount()]; Object[] rawSortValues = new Object[proto.getRawSortValuesCount()]; @@ -209,10 +224,9 @@ public static SearchHitsProtoDef.SortValueProto sortValueToProto(Object sortValu } else if (sortValue.getClass().equals(Boolean.class)) { builder.setBoolValue((Boolean) sortValue); } else if (sortValue.getClass().equals(BytesRef.class)) { - builder.setBytesValue(ByteString.copyFrom( - ((BytesRef) sortValue).bytes, - ((BytesRef) sortValue).offset, - ((BytesRef) sortValue).length)); + builder.setBytesValue( + ByteString.copyFrom(((BytesRef) sortValue).bytes, ((BytesRef) sortValue).offset, ((BytesRef) sortValue).length) + ); } else if (sortValue.getClass().equals(BigInteger.class)) { builder.setBigIntegerValue(sortValue.toString()); } else { diff --git a/server/src/main/java/org/opensearch/transport/protobuf/SearchHitsProtobuf.java b/server/src/main/java/org/opensearch/transport/protobuf/SearchHitsProtobuf.java index 499c8799fa184..413801c732922 100644 --- a/server/src/main/java/org/opensearch/transport/protobuf/SearchHitsProtobuf.java +++ b/server/src/main/java/org/opensearch/transport/protobuf/SearchHitsProtobuf.java @@ -12,19 +12,19 @@ import org.apache.lucene.search.TotalHits; import org.opensearch.core.common.io.stream.StreamInput; import org.opensearch.core.common.io.stream.StreamOutput; -import org.opensearch.search.SearchHit; -import org.opensearch.search.SearchHits; import org.opensearch.proto.search.SearchHitsProtoDef.SearchHitsProto; -import org.opensearch.proto.search.SearchHitsProtoDef.TotalHitsProto; import org.opensearch.proto.search.SearchHitsProtoDef.SortFieldProto; import org.opensearch.proto.search.SearchHitsProtoDef.SortValueProto; +import org.opensearch.proto.search.SearchHitsProtoDef.TotalHitsProto; +import org.opensearch.search.SearchHit; +import org.opensearch.search.SearchHits; import org.opensearch.transport.TransportSerializationException; import java.io.IOException; import java.util.List; -import static org.opensearch.transport.protobuf.ProtoSerDeHelpers.sortFieldToProto; import static org.opensearch.transport.protobuf.ProtoSerDeHelpers.sortFieldFromProto; +import static org.opensearch.transport.protobuf.ProtoSerDeHelpers.sortFieldToProto; import static org.opensearch.transport.protobuf.SearchHitProtobuf.sortValueFromProto; import static org.opensearch.transport.protobuf.SearchHitProtobuf.sortValueToProto; @@ -100,10 +100,10 @@ void fromProto(SearchHitsProto proto) throws TransportSerializationException { hits[i] = new SearchHitProtobuf(proto.getHits(i)); } - collapseField = proto.hasCollapseField()? proto.getCollapseField() : null; - totalHits = proto.hasTotalHits()? totalHitsFromProto(proto.getTotalHits()) : null; - sortFields = proto.getSortFieldsCount() > 0? sortFieldsFromProto(proto.getSortFieldsList()) : null; - collapseValues = proto.getCollapseValuesCount() > 0? collapseValuesFromProto(proto.getCollapseValuesList()) : null; + collapseField = proto.hasCollapseField() ? proto.getCollapseField() : null; + totalHits = proto.hasTotalHits() ? totalHitsFromProto(proto.getTotalHits()) : null; + sortFields = proto.getSortFieldsCount() > 0 ? sortFieldsFromProto(proto.getSortFieldsList()) : null; + collapseValues = proto.getCollapseValuesCount() > 0 ? collapseValuesFromProto(proto.getCollapseValuesList()) : null; } private TotalHits totalHitsFromProto(TotalHitsProto proto) { diff --git a/server/src/test/java/org/opensearch/search/SearchHitProtobufTests.java b/server/src/test/java/org/opensearch/search/SearchHitProtobufTests.java index 4b4a5169ce243..9d043efcdc4ea 100644 --- a/server/src/test/java/org/opensearch/search/SearchHitProtobufTests.java +++ b/server/src/test/java/org/opensearch/search/SearchHitProtobufTests.java @@ -59,7 +59,7 @@ import static org.opensearch.transport.protobuf.SearchHitProtobuf.searchSortValuesToProto; public class SearchHitProtobufTests extends AbstractWireSerializingTestCase { - public void testDocumentFieldProtoSerialization () { + public void testDocumentFieldProtoSerialization() { DocumentField orig = randomDocumentField(randomFrom(XContentType.values()), randomBoolean(), fieldName -> false).v1(); SearchHitsProtoDef.DocumentFieldProto proto = documentFieldToProto(orig); DocumentField cpy = documentFieldFromProto(proto); @@ -68,7 +68,7 @@ public void testDocumentFieldProtoSerialization () { assertNotSame(orig, cpy); } - public void testHighlightFieldProtoSerialization () { + public void testHighlightFieldProtoSerialization() { HighlightField orig = HighlightFieldTests.createTestItem(); SearchHitsProtoDef.HighlightFieldProto proto = highlightFieldToProto(orig); HighlightField cpy = highlightFieldFromProto(proto); @@ -77,7 +77,7 @@ public void testHighlightFieldProtoSerialization () { assertNotSame(orig, cpy); } - public void testSearchSortValuesProtoSerialization () { + public void testSearchSortValuesProtoSerialization() { SearchSortValues orig = SearchSortValuesTests.createTestItem(randomFrom(XContentType.values()), true); SearchHitsProtoDef.SearchSortValuesProto proto = searchSortValuesToProto(orig); SearchSortValues cpy = searchSortValuesFromProto(proto); @@ -86,7 +86,7 @@ public void testSearchSortValuesProtoSerialization () { assertNotSame(orig, cpy); } - public void testNestedIdentityProtoSerialization () { + public void testNestedIdentityProtoSerialization() { SearchHit.NestedIdentity orig = NestedIdentityTests.createTestItem(randomIntBetween(0, 2)); SearchHitsProtoDef.NestedIdentityProto proto = SearchHitProtobuf.nestedIdentityToProto(orig); SearchHit.NestedIdentity cpy = SearchHitProtobuf.nestedIdentityFromProto(proto); @@ -95,7 +95,7 @@ public void testNestedIdentityProtoSerialization () { assertNotSame(orig, cpy); } - public void testSearchShardTargetProtoSerialization () { + public void testSearchShardTargetProtoSerialization() { String index = randomAlphaOfLengthBetween(5, 10); String clusterAlias = randomBoolean() ? null : randomAlphaOfLengthBetween(5, 10); SearchShardTarget orig = new SearchShardTarget( @@ -111,7 +111,7 @@ public void testSearchShardTargetProtoSerialization () { assertNotSame(orig, cpy); } - public void testExplanationProtoSerialization () { + public void testExplanationProtoSerialization() { Explanation orig = createExplanation(randomIntBetween(0, 5)); SearchHitsProtoDef.ExplanationProto proto = explanationToProto(orig); Explanation cpy = explanationFromProto(proto); diff --git a/server/src/test/java/org/opensearch/search/SearchHitsProtobufTests.java b/server/src/test/java/org/opensearch/search/SearchHitsProtobufTests.java index e6789ab459e78..b2f52e87aadd2 100644 --- a/server/src/test/java/org/opensearch/search/SearchHitsProtobufTests.java +++ b/server/src/test/java/org/opensearch/search/SearchHitsProtobufTests.java @@ -45,7 +45,7 @@ import static org.opensearch.transport.protobuf.SearchHitProtobuf.sortValueToProto; public class SearchHitsProtobufTests extends AbstractWireSerializingTestCase { - public void testSortFieldProtoSerialization () { + public void testSortFieldProtoSerialization() { SortField[] fields = SearchHitsTests.createSortFields(randomIntBetween(1, 5)); for (SortField orig : fields) { SearchHitsProtoDef.SortFieldProto proto = sortFieldToProto(orig); @@ -56,7 +56,7 @@ public void testSortFieldProtoSerialization () { } } - public void testSortValueProtoSerialization () { + public void testSortValueProtoSerialization() { Object[] values = SearchHitsTests.createCollapseValues(randomIntBetween(1, 10)); for (Object orig : values) { SearchHitsProtoDef.SortValueProto proto = sortValueToProto(orig);