From 0bf18102846a8fd39ba9a229dfd65bccb3c7976a Mon Sep 17 00:00:00 2001 From: Andriy Redko Date: Thu, 4 Apr 2024 12:20:13 -0400 Subject: [PATCH] Add missed fields to MultisearchBody: collapse, version, timeout (#916) Signed-off-by: Andriy Redko --- CHANGELOG.md | 1 + .../core/msearch/MultisearchBody.java | 107 ++++++++++++++++++ .../AbstractMultiSearchRequestIT.java | 14 +++ 3 files changed, 122 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 21f24bb957..09bf98d9e8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -35,6 +35,7 @@ This section is for maintaining a changelog for all breaking changes for the cli ### Added - Add missed fields to MultisearchBody: seqNoPrimaryTerm, storedFields, explain, fields, indicesBoost ([#914](https://github.com/opensearch-project/opensearch-java/pull/914)) +- Add missed fields to MultisearchBody: collapse, version, timeout ([#916](https://github.com/opensearch-project/opensearch-java/pull/916) ### Dependencies - Bumps `io.github.classgraph:classgraph` from 4.8.161 to 4.8.165 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 dde14f797e..3d8d09629f 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 @@ -49,6 +49,7 @@ import org.opensearch.client.opensearch._types.query_dsl.FieldAndFormat; import org.opensearch.client.opensearch._types.query_dsl.Query; import org.opensearch.client.opensearch.core.SearchRequest.Builder; +import org.opensearch.client.opensearch.core.search.FieldCollapse; import org.opensearch.client.opensearch.core.search.Highlight; import org.opensearch.client.opensearch.core.search.SourceConfig; import org.opensearch.client.opensearch.core.search.Suggester; @@ -111,6 +112,15 @@ public class MultisearchBody implements JsonpSerializable { private final List> indicesBoost; + @Nullable + private final FieldCollapse collapse; + + @Nullable + private final Boolean version; + + @Nullable + private final String timeout; + // --------------------------------------------------------------------------------------------- private MultisearchBody(Builder builder) { @@ -134,6 +144,9 @@ private MultisearchBody(Builder builder) { this.explain = builder.explain; this.fields = ApiTypeHelper.unmodifiable(builder.fields); this.indicesBoost = ApiTypeHelper.unmodifiable(builder.indicesBoost); + this.collapse = builder.collapse; + this.version = builder.version; + this.timeout = builder.timeout; } public static MultisearchBody of(Function> fn) { @@ -298,6 +311,36 @@ public final List> indicesBoost() { return this.indicesBoost; } + /** + * API name: {@code collapse} + */ + @Nullable + public final FieldCollapse collapse() { + return this.collapse; + } + + /** + * If true, returns document version as part of a hit. + *

+ * API name: {@code version} + */ + @Nullable + public final Boolean version() { + return this.version; + } + + /** + * Specifies the period of time to wait for a response from each shard. If no + * response is received before the timeout expires, the request fails and + * returns an error. Defaults to no timeout. + *

+ * API name: {@code timeout} + */ + @Nullable + public final String timeout() { + return this.timeout; + } + /** * Serialize this object to JSON. */ @@ -453,6 +496,21 @@ protected void serializeInternal(JsonGenerator generator, JsonpMapper mapper) { } generator.writeEnd(); } + + if (this.collapse != null) { + generator.writeKey("collapse"); + this.collapse.serialize(generator, mapper); + } + + if (this.version != null) { + generator.writeKey("version"); + generator.write(this.version); + } + + if (this.timeout != null) { + generator.writeKey("timeout"); + generator.write(this.timeout); + } } // --------------------------------------------------------------------------------------------- @@ -518,6 +576,15 @@ public static class Builder extends ObjectBuilderBase implements ObjectBuilder> indicesBoost; + @Nullable + private FieldCollapse collapse; + + @Nullable + private Boolean version; + + @Nullable + private String timeout; + /** * API name: {@code aggregations} *

@@ -861,6 +928,43 @@ public final Builder indicesBoost(Map value, Map return this; } + /** + * API name: {@code collapse} + */ + public final Builder collapse(@Nullable FieldCollapse value) { + this.collapse = value; + return this; + } + + /** + * API name: {@code collapse} + */ + public final Builder collapse(Function> fn) { + return this.collapse(fn.apply(new FieldCollapse.Builder()).build()); + } + + /** + * If true, returns document version as part of a hit. + *

+ * API name: {@code version} + */ + public final Builder version(@Nullable Boolean value) { + this.version = value; + return this; + } + + /** + * Specifies the period of time to wait for a response from each shard. If no + * response is received before the timeout expires, the request fails and + * returns an error. Defaults to no timeout. + *

+ * API name: {@code timeout} + */ + public final Builder timeout(@Nullable String value) { + this.timeout = value; + return this; + } + /** * Builds a {@link MultisearchBody}. * @@ -909,6 +1013,9 @@ protected static void setupMultisearchBodyDeserializer(ObjectDeserializer b.collapse(FieldCollapse.of(f -> f.field("name"))).version(true).timeout("5s") + ); + + MsearchResponse response = sendMSearchRequest(index, List.of(sortedItemsQuery)); + assertEquals(1, response.responses().size()); + assertEquals(3, response.responses().get(0).result().hits().hits().size()); + } + private void assertResponseSources(MultiSearchResponseItem response) { List> hitsWithHighlights = response.result().hits().hits(); assertEquals(2, hitsWithHighlights.size());