From 99bb07a0eefa4e1aededccf016f8b4c3f64e5c3d Mon Sep 17 00:00:00 2001 From: Vacha Shah Date: Mon, 6 Nov 2023 13:28:54 -0800 Subject: [PATCH] Fixing partial success results for msearch template (#709) * Fixing partial success results for msearch template Signed-off-by: Vacha Shah * Adding changelog Signed-off-by: Vacha Shah * Spotless apply Signed-off-by: Vacha Shah --------- Signed-off-by: Vacha Shah --- CHANGELOG.md | 1 + .../opensearch/_types/ErrorResponse.java | 12 +++--- .../integTest/AbstractClusterClientIT.java | 2 +- .../integTest/AbstractRequestIT.java | 2 +- .../AbstractSearchTemplateRequestIT.java | 38 ++++++++++++++----- .../json/jackson/JacksonJsonpParserTest.java | 2 +- 6 files changed, 39 insertions(+), 18 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index adf5573800..4e91c2d05c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -38,6 +38,7 @@ This section is for maintaining a changelog for all breaking changes for the cli ### Removed ### Fixed +- Fix partial success results for msearch_template ([#709](https://github.com/opensearch-project/opensearch-java/pull/709)) ### Security diff --git a/java-client/src/main/java/org/opensearch/client/opensearch/_types/ErrorResponse.java b/java-client/src/main/java/org/opensearch/client/opensearch/_types/ErrorResponse.java index 029d6a20c0..0fe5d5ae2c 100644 --- a/java-client/src/main/java/org/opensearch/client/opensearch/_types/ErrorResponse.java +++ b/java-client/src/main/java/org/opensearch/client/opensearch/_types/ErrorResponse.java @@ -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; } @@ -87,7 +87,7 @@ public final ErrorCause error() { /** * Required - API name: {@code status} */ - public final int status() { + public final Integer status() { return this.status; } @@ -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); + } } diff --git a/java-client/src/test/java/org/opensearch/client/opensearch/integTest/AbstractClusterClientIT.java b/java-client/src/test/java/org/opensearch/client/opensearch/integTest/AbstractClusterClientIT.java index 02c24a94d6..4aac7cb5ca 100644 --- a/java-client/src/test/java/org/opensearch/client/opensearch/integTest/AbstractClusterClientIT.java +++ b/java-client/src/test/java/org/opensearch/client/opensearch/integTest/AbstractClusterClientIT.java @@ -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")); } } diff --git a/java-client/src/test/java/org/opensearch/client/opensearch/integTest/AbstractRequestIT.java b/java-client/src/test/java/org/opensearch/client/opensearch/integTest/AbstractRequestIT.java index ecfd9ff3aa..716cc4b25d 100644 --- a/java-client/src/test/java/org/opensearch/client/opensearch/integTest/AbstractRequestIT.java +++ b/java-client/src/test/java/org/opensearch/client/opensearch/integTest/AbstractRequestIT.java @@ -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 diff --git a/java-client/src/test/java/org/opensearch/client/opensearch/integTest/AbstractSearchTemplateRequestIT.java b/java-client/src/test/java/org/opensearch/client/opensearch/integTest/AbstractSearchTemplateRequestIT.java index 7b30b77786..4489ef8a68 100644 --- a/java-client/src/test/java/org/opensearch/client/opensearch/integTest/AbstractSearchTemplateRequestIT.java +++ b/java-client/src/test/java/org/opensearch/client/opensearch/integTest/AbstractSearchTemplateRequestIT.java @@ -9,6 +9,7 @@ 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; @@ -16,6 +17,7 @@ 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 { @@ -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); + 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( - 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)) - ) - ), + 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 sendTemplateRequest(String index, String title, boolean suggs, boolean aggs) diff --git a/java-client/src/test/java/org/opensearch/client/opensearch/json/jackson/JacksonJsonpParserTest.java b/java-client/src/test/java/org/opensearch/client/opensearch/json/jackson/JacksonJsonpParserTest.java index cc8426562d..dc0d04b1b1 100644 --- a/java-client/src/test/java/org/opensearch/client/opensearch/json/jackson/JacksonJsonpParserTest.java +++ b/java-client/src/test/java/org/opensearch/client/opensearch/json/jackson/JacksonJsonpParserTest.java @@ -185,7 +185,7 @@ public void testMultiSearchResponse() { MsearchResponse 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()); }