Skip to content

Commit

Permalink
Fixing partial success results for msearch template
Browse files Browse the repository at this point in the history
Signed-off-by: Vacha Shah <vachshah@amazon.com>
  • Loading branch information
VachaShah committed Nov 5, 2023
1 parent 648cfe2 commit 3ddab57
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -62,14 +62,14 @@ private enum Kind {

private final ErrorCause error;

private final int status;
private final Integer status;

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

private ErrorResponse(Builder builder) {

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

}

Expand All @@ -87,7 +87,7 @@ public final ErrorCause error() {
/**
* Required - API name: {@code status}
*/
public final int status() {
public final Integer status() {
return this.status;
}

Expand All @@ -105,8 +105,10 @@ protected void serializeInternal(JsonGenerator generator, JsonpMapper mapper) {
generator.writeKey("error");
this.error.serialize(generator, mapper);

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

}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ public void testClusterUpdateSettingNonExistent() throws IOException {
fail();
} catch (OpenSearchException e) {
assertNotNull(e);
assertEquals(e.response().status(), 400);
assertEquals(e.response().status().intValue(), 400);
assertTrue(e.getMessage().contains("transient setting [no_idea_what_you_are_talking_about], not recognized"));
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -335,7 +335,7 @@ public void testDataIngestion() throws Exception {
assertTrue(msearch.responses().get(0).isResult());
assertEquals(1, msearch.responses().get(0).result().hits().hits().size());
assertTrue(msearch.responses().get(1).isFailure());
assertEquals(404, msearch.responses().get(1).failure().status());
assertEquals(404, msearch.responses().get(1).failure().status().intValue());
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,15 @@
package org.opensearch.client.opensearch.integTest;

import java.io.IOException;
import java.util.List;
import java.util.Map;
import org.junit.Test;
import org.opensearch.client.json.JsonData;
import org.opensearch.client.opensearch._types.Refresh;
import org.opensearch.client.opensearch._types.mapping.Property;
import org.opensearch.client.opensearch.core.PutScriptRequest;
import org.opensearch.client.opensearch.core.SearchTemplateResponse;
import org.opensearch.client.opensearch.core.msearch_template.RequestItem;

public abstract class AbstractSearchTemplateRequestIT extends OpenSearchJavaClientTestCase {

Expand Down Expand Up @@ -87,27 +89,43 @@ public void testTemplateSearchAggregations() throws Exception {

@Test
public void testMultiSearchTemplate() throws Exception {
System.out.println("Multi search template test");
var index = "test-msearch-template";
createDocuments(index);

var searchResponse = javaClient().msearchTemplate(
request -> request.searchTemplates(
r -> r.header(h -> h.index(index))
RequestItem requestItem = RequestItem.of(
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))
)
),
);
// adding a request to non existing template to test partial results
RequestItem requestItem2 = RequestItem.of(
r -> r.header(h -> h.index(index))
.body(
t -> t.id("my-other-search-template")
.params("title", JsonData.of("Document"))
.params("suggs", JsonData.of(false))
.params("aggs", JsonData.of(false))
)
);

var searchResponse = javaClient().msearchTemplate(
request -> request.searchTemplates(List.of(requestItem, requestItem2)),
SimpleDoc.class
);

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

private SearchTemplateResponse<SimpleDoc> sendTemplateRequest(String index, String title, boolean suggs, boolean aggs)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ public void testMultiSearchResponse() {
MsearchResponse<Foo> response = fromJson(json, MsearchResponse.class, mapper);

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

Expand Down

0 comments on commit 3ddab57

Please sign in to comment.