Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixing issue when tracktotalhits is disabled #372

Merged
merged 3 commits into from
Mar 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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