From 8f4a56d1ec71620d9cb8c3a5cc5adbd1de5aa197 Mon Sep 17 00:00:00 2001 From: Vacha Shah Date: Fri, 3 Mar 2023 05:58:57 -0800 Subject: [PATCH] Fixing issue when tracktotalhits is disabled (#372) * Fixing issue when tracktotalhits is disabled Signed-off-by: Vacha Shah * Update Changelog Signed-off-by: Vacha Shah --------- Signed-off-by: Vacha Shah Co-authored-by: Daniel (dB.) Doubrovkine --- CHANGELOG.md | 1 + .../opensearch/core/search/HitsMetadata.java | 16 ++++++---- .../integTest/AbstractRequestIT.java | 32 +++++++++++++++++++ 3 files changed, 43 insertions(+), 6 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index d6935e56a4..161be7d318 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -15,6 +15,7 @@ Inspired from [Keep a Changelog](https://keepachangelog.com/en/1.0.0/) ### Fixed - Fix missing Highlight and SourceConfig in the MultisearchBody ([#442](https://github.com/opensearch-project/opensearch-java/pull/442)) +- Fix search failure with missing required property HitsMetadata.total when trackTotalHits is disabled ([#372](https://github.com/opensearch-project/opensearch-java/pull/372)) ### Security diff --git a/java-client/src/main/java/org/opensearch/client/opensearch/core/search/HitsMetadata.java b/java-client/src/main/java/org/opensearch/client/opensearch/core/search/HitsMetadata.java index caf3a255a5..86bd9dce6f 100644 --- a/java-client/src/main/java/org/opensearch/client/opensearch/core/search/HitsMetadata.java +++ b/java-client/src/main/java/org/opensearch/client/opensearch/core/search/HitsMetadata.java @@ -56,6 +56,7 @@ public class HitsMetadata implements JsonpSerializable { + @Nullable private final TotalHits total; private final List> hits; @@ -70,7 +71,7 @@ public class HitsMetadata implements JsonpSerializable { private HitsMetadata(Builder builder) { - this.total = ApiTypeHelper.requireNonNull(builder.total, this, "total"); + this.total = builder.total; this.hits = ApiTypeHelper.unmodifiableRequired(builder.hits, this, "hits"); this.maxScore = builder.maxScore; this.tSerializer = builder.tSerializer; @@ -82,7 +83,7 @@ public static HitsMetadata of(Function, ObjectBuilder extends ObjectBuilderBase implements ObjectBuilder> { + @Nullable private TotalHits total; private List> hits; @@ -153,7 +157,7 @@ public static class Builder extends ObjectBuilderBase implements ObjectBuilde private JsonpSerializer tSerializer; /** - * Required - API name: {@code total} + * API name: {@code total} */ public final Builder total(TotalHits value) { this.total = value; @@ -161,7 +165,7 @@ public final Builder total(TotalHits value) { } /** - * Required - API name: {@code total} + * API name: {@code total} */ public final Builder total(Function> fn) { return this.total(fn.apply(new TotalHits.Builder()).build()); diff --git a/java-client/src/test/java/org/opensearch/client/opensearch/integTest/AbstractRequestIT.java b/java-client/src/test/java/org/opensearch/client/opensearch/integTest/AbstractRequestIT.java index 0ca4db2b4b..586abd49d9 100644 --- a/java-client/src/test/java/org/opensearch/client/opensearch/integTest/AbstractRequestIT.java +++ b/java-client/src/test/java/org/opensearch/client/opensearch/integTest/AbstractRequestIT.java @@ -58,6 +58,7 @@ import org.opensearch.client.opensearch.core.search.CompletionSuggester; import org.opensearch.client.opensearch.core.search.FieldSuggester; import org.opensearch.client.opensearch.core.search.FieldSuggesterBuilders; +import org.opensearch.client.opensearch.core.search.Hit; import org.opensearch.client.opensearch.core.search.Suggester; import org.opensearch.client.opensearch.indices.CreateIndexResponse; import org.opensearch.client.opensearch.indices.GetIndexResponse; @@ -314,6 +315,37 @@ public void testBulkRequest() throws IOException { assertEquals(42, javaClient().get(b -> b.index("foo").id("abc"), AppData.class).source().getIntValue()); } + @Test + public void testTrackTotalHitsFalse() throws Exception { + // https://github.com/opensearch-project/opensearch-java/issues/354 + String index = "ingest-test"; + + javaClient().indices().create(b -> b.index(index)); + + AppData appData = new AppData(); + appData.setIntValue(1337); + appData.setMsg("foo"); + + javaClient().index(b -> b + .index(index) + .id("myId") + .document(appData) + .refresh(Refresh.True) // Make it visible for search + ).id(); + + // Search + SearchResponse search = javaClient().search(b -> b + .index(index) + .trackTotalHits(t -> t.enabled(false)) + , AppData.class + ); + + List> hits = search.hits().hits(); + AppData appDataResult = search.hits().hits().get(0).source(); + assertEquals(1337, appDataResult.getIntValue()); + assertEquals("foo", appDataResult.getMsg()); + } + @Test public void testRefresh() throws IOException { AppData appData = new AppData();