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

[Backport 2.x] Make MultiSearchItem.status optional (#660) #662

Merged
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ Inspired from [Keep a Changelog](https://keepachangelog.com/en/1.0.0/)
- Fixed CVE-2976 + added CVE checker ([#624](https://github.com/opensearch-project/opensearch-java/pull/624))
- Fix parsing of GetFieldMappingResponse ([#641](https://github.com/opensearch-project/opensearch-java/pull/641))
- Fix TermvectorsResponse for optional fields ([#642](https://github.com/opensearch-project/opensearch-java/pull/642))
- Fix deserialization of MsearchTemplateResponse ([#660](https://github.com/opensearch-project/opensearch-java/pull/660))

### Security

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,25 +35,26 @@
import jakarta.json.stream.JsonGenerator;
import java.util.function.Function;
import java.util.function.Supplier;
import javax.annotation.Nullable;
import org.opensearch.client.json.JsonpDeserializer;
import org.opensearch.client.json.JsonpMapper;
import org.opensearch.client.json.ObjectBuilderDeserializer;
import org.opensearch.client.json.ObjectDeserializer;
import org.opensearch.client.opensearch.core.SearchResponse;
import org.opensearch.client.util.ApiTypeHelper;
import org.opensearch.client.util.ObjectBuilder;

// typedef: _global.msearch.MultiSearchItem

public class MultiSearchItem<TDocument> extends SearchResponse<TDocument> {
private final int status;
@Nullable
private final Integer status;

// ---------------------------------------------------------------------------------------------

private MultiSearchItem(Builder<TDocument> builder) {
super(builder);

this.status = ApiTypeHelper.requireNonNull(builder.status, this, "status");
this.status = builder.status;

}

Expand All @@ -62,17 +63,21 @@ public static <TDocument> MultiSearchItem<TDocument> of(Function<Builder<TDocume
}

/**
* Required - API name: {@code status}
* API name: {@code status}
*/
public final int status() {
@Nullable
public final Integer status() {
return this.status;
}

protected void serializeInternal(JsonGenerator generator, JsonpMapper mapper) {

super.serializeInternal(generator, mapper);
generator.writeKey("status");
generator.write(this.status);

if (this.status != null) {
generator.writeKey("status");
generator.write(this.status);
}

}

Expand All @@ -85,12 +90,13 @@ protected void serializeInternal(JsonGenerator generator, JsonpMapper mapper) {
public static class Builder<TDocument> extends SearchResponse.AbstractBuilder<TDocument, Builder<TDocument>>
implements
ObjectBuilder<MultiSearchItem<TDocument>> {
@Nullable
private Integer status;

/**
* Required - API name: {@code status}
* API name: {@code status}
*/
public final Builder<TDocument> status(int value) {
public final Builder<TDocument> status(@Nullable Integer value) {
this.status = value;
return this;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,31 @@ public void testTemplateSearchAggregations() throws Exception {

}

@Test
public void testMultiSearchTemplate() throws Exception {
var index = "test-msearch-template";
createDocuments(index);

var searchResponse = javaClient().msearchTemplate(
request -> request.searchTemplates(
r -> r.header(h -> h.index(index))
.body(
t -> t.id(TEST_SEARCH_TEMPLATE)
.params("title", JsonData.of("Document"))
.params("suggs", JsonData.of(false))
.params("aggs", JsonData.of(false))
)
),
SimpleDoc.class
);

assertEquals(1, searchResponse.responses().size());
var response = searchResponse.responses().get(0);
assertTrue(response.isResult());
assertNull(response.result().status());
assertEquals(4, response.result().hits().hits().size());
}

private SearchTemplateResponse<SimpleDoc> sendTemplateRequest(String index, String title, boolean suggs, boolean aggs)
throws IOException {
return javaClient().searchTemplate(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ public void testMultiSearchResponse() {

assertEquals(2, response.responses().size());
assertEquals(404, response.responses().get(0).failure().status());
assertEquals(200, response.responses().get(1).result().status());
assertEquals((Integer) 200, response.responses().get(1).result().status());
}

public static class AttributedJacksonJsonpMapper extends JacksonJsonpMapper {
Expand Down
Loading