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 08ec63b009..835f292934 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; @@ -249,6 +250,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();