Skip to content

Commit

Permalink
Fixing issue when tracktotalhits is disabled
Browse files Browse the repository at this point in the history
Signed-off-by: Vacha Shah <[email protected]>
  • Loading branch information
VachaShah committed Feb 21, 2023
1 parent fbef578 commit bdffc8c
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@


public class HitsMetadata<T> implements JsonpSerializable {
@Nullable
private final TotalHits total;

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

private HitsMetadata(Builder<T> 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;
Expand All @@ -82,7 +83,7 @@ public static <T> HitsMetadata<T> of(Function<Builder<T>, ObjectBuilder<HitsMeta
}

/**
* Required - API name: {@code total}
* API name: {@code total}
*/
public final TotalHits total() {
return this.total;
Expand Down Expand Up @@ -114,8 +115,10 @@ public void serialize(JsonGenerator generator, JsonpMapper mapper) {

protected void serializeInternal(JsonGenerator generator, JsonpMapper mapper) {

generator.writeKey("total");
this.total.serialize(generator, mapper);
if (this.total != null) {
generator.writeKey("total");
this.total.serialize(generator, mapper);
}

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

public static class Builder<T> extends ObjectBuilderBase implements ObjectBuilder<HitsMetadata<T>> {
@Nullable
private TotalHits total;

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

/**
* Required - API name: {@code total}
* API name: {@code total}
*/
public final Builder<T> total(TotalHits value) {
this.total = value;
return this;
}

/**
* Required - API name: {@code total}
* API name: {@code total}
*/
public final Builder<T> total(Function<TotalHits.Builder, ObjectBuilder<TotalHits>> fn) {
return this.total(fn.apply(new TotalHits.Builder()).build());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<AppData> search = javaClient().search(b -> b
.index(index)
.trackTotalHits(t -> t.enabled(false))
, AppData.class
);

List<Hit<AppData>> 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();
Expand Down

0 comments on commit bdffc8c

Please sign in to comment.