From 74c3721e71d9d9dbbfc851f9f9238a5fed3c96fb Mon Sep 17 00:00:00 2001 From: Thomas Farr Date: Thu, 10 Oct 2024 10:28:21 +1300 Subject: [PATCH] Add bwc handling on non-string sort values on `Hit`s (#1224) * Fix handling on non-string sort values on `Hit`s Signed-off-by: Thomas Farr * Update CHANGELOG Signed-off-by: Thomas Farr * Fix MultisearchBody as well Signed-off-by: Thomas Farr --------- Signed-off-by: Thomas Farr --- CHANGELOG.md | 4 +- .../core/msearch/MultisearchBody.java | 53 ++++++++++++++++--- .../client/opensearch/core/search/Hit.java | 53 ++++++++++++++++--- 3 files changed, 97 insertions(+), 13 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 8946341fd0..1ba8e7dc07 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,7 +3,9 @@ Inspired from [Keep a Changelog](https://keepachangelog.com/en/1.0.0/) ## [Unreleased 2.x] ### Added -- Add `hashCode` and `equals` implementations ([#312](https://github.com/opensearch-project/opensearch-java/pull/312)). +- Added `hashCode` and `equals` implementations ([#312](https://github.com/opensearch-project/opensearch-java/pull/312)) +- Added `sortVals` to `Hit` to allow retrieving non-string sort values ([#1224](https://github.com/opensearch-project/opensearch-java/pull/1224)) +- Added `searchAfterVals` to `MultisearchBody` to allow passing arbitrary `FieldValue`s to `search_after` ([#1224](https://github.com/opensearch-project/opensearch-java/pull/1224)) ### Dependencies - Bumps `org.junit:junit-bom` from 5.10.3 to 5.11.1 diff --git a/java-client/src/main/java/org/opensearch/client/opensearch/core/msearch/MultisearchBody.java b/java-client/src/main/java/org/opensearch/client/opensearch/core/msearch/MultisearchBody.java index fb4ff1013d..4fc94119d5 100644 --- a/java-client/src/main/java/org/opensearch/client/opensearch/core/msearch/MultisearchBody.java +++ b/java-client/src/main/java/org/opensearch/client/opensearch/core/msearch/MultisearchBody.java @@ -36,6 +36,7 @@ import java.util.List; import java.util.Map; import java.util.function.Function; +import java.util.stream.Collectors; import javax.annotation.Nullable; import org.opensearch.client.json.JsonData; import org.opensearch.client.json.JsonpDeserializable; @@ -44,6 +45,7 @@ import org.opensearch.client.json.ObjectBuilderDeserializer; import org.opensearch.client.json.ObjectDeserializer; import org.opensearch.client.json.PlainJsonSerializable; +import org.opensearch.client.opensearch._types.FieldValue; import org.opensearch.client.opensearch._types.ScriptField; import org.opensearch.client.opensearch._types.SortOptions; import org.opensearch.client.opensearch._types.aggregations.Aggregation; @@ -77,7 +79,7 @@ public class MultisearchBody implements PlainJsonSerializable { @Nullable private final Query postFilter; - private final List searchAfter; + private final List searchAfter; @Nullable private final Integer size; @@ -201,8 +203,19 @@ public final Query postFilter() { /** * API name: {@code search_after} + * + *

NOTE: In version 3.0.0 of opensearch-java, this method will instead return a {@code List}.

*/ public final List searchAfter() { + return this.searchAfter.stream().map(FieldValue::_toJsonString).collect(Collectors.toList()); + } + + /** + * API name: {@code search_after} + * + *

NOTE: In version 3.0.0 of opensearch-java, this method will be renamed to replace {@link #searchAfter()}.

+ */ + public final List searchAfterVals() { return this.searchAfter; } @@ -407,8 +420,8 @@ protected void serializeInternal(JsonGenerator generator, JsonpMapper mapper) { if (ApiTypeHelper.isDefined(this.searchAfter)) { generator.writeKey("search_after"); generator.writeStartArray(); - for (String item0 : this.searchAfter) { - generator.write(item0); + for (FieldValue item0 : this.searchAfter) { + item0.serialize(generator, mapper); } generator.writeEnd(); @@ -578,7 +591,7 @@ public static class Builder extends ObjectBuilderBase implements ObjectBuilder searchAfter; + private List searchAfter; @Nullable private Integer size; @@ -715,9 +728,11 @@ public final Builder postFilter(Function> fn * API name: {@code search_after} *

* Adds all elements of list to searchAfter. + * + *

NOTE: In version 3.0.0 of opensearch-java, this method will instead accept a {@code List}.

*/ public final Builder searchAfter(List list) { - this.searchAfter = _listAddAll(this.searchAfter, list); + this.searchAfter = _listAddAll(this.searchAfter, FieldValue::of, list); return this; } @@ -725,8 +740,34 @@ public final Builder searchAfter(List list) { * API name: {@code search_after} *

* Adds one or more values to searchAfter. + * + *

NOTE: In version 3.0.0 of opensearch-java, this method will instead accept values of type {@code FieldValue}.

*/ public final Builder searchAfter(String value, String... values) { + this.searchAfter = _listAdd(this.searchAfter, FieldValue::of, value, values); + return this; + } + + /** + * API name: {@code search_after} + *

+ * Adds all elements of list to searchAfter. + * + *

NOTE: In version 3.0.0 of opensearch-java, this method will be renamed to replace {@link #searchAfter(List)}.

+ */ + public final Builder searchAfterVals(List list) { + this.searchAfter = _listAddAll(this.searchAfter, list); + return this; + } + + /** + * API name: {@code search_after} + *

+ * Adds one or more values to searchAfter. + * + *

NOTE: In version 3.0.0 of opensearch-java, this method will be renamed to replace {@link #searchAfter(String, String...)}.

+ */ + public final Builder searchAfterVals(FieldValue value, FieldValue... values) { this.searchAfter = _listAdd(this.searchAfter, value, values); return this; } @@ -1092,7 +1133,7 @@ protected static void setupMultisearchBodyDeserializer(ObjectDeserializer implements PlainJsonSerializable { @Nullable private final Long version; - private final List sort; + private final List sort; @Nullable private final JsonpSerializer tDocumentSerializer; @@ -265,8 +267,19 @@ public final Long version() { /** * API name: {@code sort} + * + *

NOTE: In version 3.0.0 of opensearch-java, this method will instead return a {@code List}.

*/ public final List sort() { + return this.sort.stream().map(FieldValue::_toJsonString).collect(Collectors.toList()); + } + + /** + * API name: {@code sort} + * + *

NOTE: In version 3.0.0 of opensearch-java, this method will be renamed to replace {@link #sort()}.

+ */ + public final List sortVals() { return this.sort; } @@ -404,8 +417,8 @@ protected void serializeInternal(JsonGenerator generator, JsonpMapper mapper) { if (ApiTypeHelper.isDefined(this.sort)) { generator.writeKey("sort"); generator.writeStartArray(); - for (String item0 : this.sort) { - generator.write(item0); + for (FieldValue item0 : this.sort) { + item0.serialize(generator, mapper); } generator.writeEnd(); @@ -474,7 +487,7 @@ public static class Builder extends ObjectBuilderBase implements Obje private Long version; @Nullable - private List sort; + private List sort; @Nullable private JsonpSerializer tDocumentSerializer; @@ -702,9 +715,11 @@ public final Builder version(@Nullable Long value) { * API name: {@code sort} *

* Adds all elements of list to sort. + * + *

NOTE: In version 3.0.0 of opensearch-java, this method will instead accept a {@code List}.

*/ public final Builder sort(List list) { - this.sort = _listAddAll(this.sort, list); + this.sort = _listAddAll(this.sort, FieldValue::of, list); return this; } @@ -712,8 +727,34 @@ public final Builder sort(List list) { * API name: {@code sort} *

* Adds one or more values to sort. + * + *

NOTE: In version 3.0.0 of opensearch-java, this method will instead accept a {@code List}.

*/ public final Builder sort(String value, String... values) { + this.sort = _listAdd(this.sort, FieldValue::of, value, values); + return this; + } + + /** + * API name: {@code sort} + *

+ * Adds all elements of list to sort. + * + *

NOTE: In version 3.0.0 of opensearch-java, this method will be renamed to replace {@link #sort(List)}.

+ */ + public final Builder sortVals(List list) { + this.sort = _listAddAll(this.sort, list); + return this; + } + + /** + * API name: {@code sort} + *

+ * Adds one or more values to sort. + * + *

NOTE: In version 3.0.0 of opensearch-java, this method will be renamed to replace {@link #sort(String, String...)}.

+ */ + public final Builder sortVals(FieldValue value, FieldValue... values) { this.sort = _listAdd(this.sort, value, values); return this; } @@ -778,7 +819,7 @@ protected static void setupHitDeserializer( op.add(Builder::seqNo, JsonpDeserializer.longDeserializer(), "_seq_no"); op.add(Builder::primaryTerm, JsonpDeserializer.longDeserializer(), "_primary_term"); op.add(Builder::version, JsonpDeserializer.longDeserializer(), "_version"); - op.add(Builder::sort, JsonpDeserializer.arrayDeserializer(JsonpDeserializer.stringOrNullDeserializer()), "sort"); + op.add(Builder::sortVals, JsonpDeserializer.arrayDeserializer(FieldValue._DESERIALIZER), "sort"); }