Skip to content

Commit

Permalink
Fixing issue when tracktotalhits is disabled (#372)
Browse files Browse the repository at this point in the history
* Fixing issue when tracktotalhits is disabled

Signed-off-by: Vacha Shah <vachshah@amazon.com>

* Update Changelog

Signed-off-by: Vacha Shah <vachshah@amazon.com>

---------

Signed-off-by: Vacha Shah <vachshah@amazon.com>
Co-authored-by: Daniel (dB.) Doubrovkine <dblock@amazon.com>
  • Loading branch information
VachaShah and dblock authored Mar 3, 2023
1 parent 75c5e59 commit 7b9f8c7
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 8 deletions.
4 changes: 2 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ Inspired from [Keep a Changelog](https://keepachangelog.com/en/1.0.0/)
- Update Gradle to 7.6 ([#309](https://github.com/opensearch-project/opensearch-java/pull/309))
- Prevent SPI calls at runtime ([#293](https://github.com/opensearch-project/opensearch-java/pull/293))
- Add support for OpenSearch Serverless ([#339](https://github.com/opensearch-project/opensearch-java/pull/339))
- Fix issue where completion suggestions were failing, due to being parsed as term suggestions ([#107](https://github.com/opensearch-project/opensearch-java/issues/107))
- Fix integration tests ([#375](https://github.com/opensearch-project/opensearch-java/pull/375))
- Fix completion suggestions failure with missing required property TermSuggestOption.score ([#347](https://github.com/opensearch-project/opensearch-java/pull/347))
- Fix search failure with missing required property HitsMetadata.total when trackTotalHits is disabled ([#372](https://github.com/opensearch-project/opensearch-java/pull/372))

### Deprecated

Expand Down
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 7b9f8c7

Please sign in to comment.