Skip to content

Commit d358a5a

Browse files
VachaShahdblock
andauthored
Fixing issue when tracktotalhits is disabled (#372) (#449)
* Fixing issue when tracktotalhits is disabled * Update Changelog --------- Signed-off-by: Vacha Shah <vachshah@amazon.com> Co-authored-by: Daniel (dB.) Doubrovkine <dblock@amazon.com>
1 parent 573ac7f commit d358a5a

3 files changed

Lines changed: 43 additions & 6 deletions

File tree

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ Inspired from [Keep a Changelog](https://keepachangelog.com/en/1.0.0/)
1515

1616
### Fixed
1717
- Fix missing Highlight and SourceConfig in the MultisearchBody ([#442](https://github.com/opensearch-project/opensearch-java/pull/442))
18+
- Fix search failure with missing required property HitsMetadata.total when trackTotalHits is disabled ([#372](https://github.com/opensearch-project/opensearch-java/pull/372))
1819

1920
### Security
2021

java-client/src/main/java/org/opensearch/client/opensearch/core/search/HitsMetadata.java

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@
5656

5757

5858
public class HitsMetadata<T> implements JsonpSerializable {
59+
@Nullable
5960
private final TotalHits total;
6061

6162
private final List<Hit<T>> hits;
@@ -70,7 +71,7 @@ public class HitsMetadata<T> implements JsonpSerializable {
7071

7172
private HitsMetadata(Builder<T> builder) {
7273

73-
this.total = ApiTypeHelper.requireNonNull(builder.total, this, "total");
74+
this.total = builder.total;
7475
this.hits = ApiTypeHelper.unmodifiableRequired(builder.hits, this, "hits");
7576
this.maxScore = builder.maxScore;
7677
this.tSerializer = builder.tSerializer;
@@ -82,7 +83,7 @@ public static <T> HitsMetadata<T> of(Function<Builder<T>, ObjectBuilder<HitsMeta
8283
}
8384

8485
/**
85-
* Required - API name: {@code total}
86+
* API name: {@code total}
8687
*/
8788
public final TotalHits total() {
8889
return this.total;
@@ -114,8 +115,10 @@ public void serialize(JsonGenerator generator, JsonpMapper mapper) {
114115

115116
protected void serializeInternal(JsonGenerator generator, JsonpMapper mapper) {
116117

117-
generator.writeKey("total");
118-
this.total.serialize(generator, mapper);
118+
if (this.total != null) {
119+
generator.writeKey("total");
120+
this.total.serialize(generator, mapper);
121+
}
119122

120123
if (ApiTypeHelper.isDefined(this.hits)) {
121124
generator.writeKey("hits");
@@ -142,6 +145,7 @@ protected void serializeInternal(JsonGenerator generator, JsonpMapper mapper) {
142145
*/
143146

144147
public static class Builder<T> extends ObjectBuilderBase implements ObjectBuilder<HitsMetadata<T>> {
148+
@Nullable
145149
private TotalHits total;
146150

147151
private List<Hit<T>> hits;
@@ -153,15 +157,15 @@ public static class Builder<T> extends ObjectBuilderBase implements ObjectBuilde
153157
private JsonpSerializer<T> tSerializer;
154158

155159
/**
156-
* Required - API name: {@code total}
160+
* API name: {@code total}
157161
*/
158162
public final Builder<T> total(TotalHits value) {
159163
this.total = value;
160164
return this;
161165
}
162166

163167
/**
164-
* Required - API name: {@code total}
168+
* API name: {@code total}
165169
*/
166170
public final Builder<T> total(Function<TotalHits.Builder, ObjectBuilder<TotalHits>> fn) {
167171
return this.total(fn.apply(new TotalHits.Builder()).build());

java-client/src/test/java/org/opensearch/client/opensearch/integTest/AbstractRequestIT.java

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@
5858
import org.opensearch.client.opensearch.core.search.CompletionSuggester;
5959
import org.opensearch.client.opensearch.core.search.FieldSuggester;
6060
import org.opensearch.client.opensearch.core.search.FieldSuggesterBuilders;
61+
import org.opensearch.client.opensearch.core.search.Hit;
6162
import org.opensearch.client.opensearch.core.search.Suggester;
6263
import org.opensearch.client.opensearch.indices.CreateIndexResponse;
6364
import org.opensearch.client.opensearch.indices.GetIndexResponse;
@@ -314,6 +315,37 @@ public void testBulkRequest() throws IOException {
314315
assertEquals(42, javaClient().get(b -> b.index("foo").id("abc"), AppData.class).source().getIntValue());
315316
}
316317

318+
@Test
319+
public void testTrackTotalHitsFalse() throws Exception {
320+
// https://github.com/opensearch-project/opensearch-java/issues/354
321+
String index = "ingest-test";
322+
323+
javaClient().indices().create(b -> b.index(index));
324+
325+
AppData appData = new AppData();
326+
appData.setIntValue(1337);
327+
appData.setMsg("foo");
328+
329+
javaClient().index(b -> b
330+
.index(index)
331+
.id("myId")
332+
.document(appData)
333+
.refresh(Refresh.True) // Make it visible for search
334+
).id();
335+
336+
// Search
337+
SearchResponse<AppData> search = javaClient().search(b -> b
338+
.index(index)
339+
.trackTotalHits(t -> t.enabled(false))
340+
, AppData.class
341+
);
342+
343+
List<Hit<AppData>> hits = search.hits().hits();
344+
AppData appDataResult = search.hits().hits().get(0).source();
345+
assertEquals(1337, appDataResult.getIntValue());
346+
assertEquals("foo", appDataResult.getMsg());
347+
}
348+
317349
@Test
318350
public void testRefresh() throws IOException {
319351
AppData appData = new AppData();

0 commit comments

Comments
 (0)