diff --git a/docs/changelog/85496.yaml b/docs/changelog/85496.yaml new file mode 100644 index 0000000000000..574a7823f9427 --- /dev/null +++ b/docs/changelog/85496.yaml @@ -0,0 +1,6 @@ +pr: 85496 +summary: Add status field to Multi Search Template Responses +area: Search +type: bug +issues: + - 83029 diff --git a/modules/lang-mustache/src/main/java/org/elasticsearch/script/mustache/MultiSearchTemplateResponse.java b/modules/lang-mustache/src/main/java/org/elasticsearch/script/mustache/MultiSearchTemplateResponse.java index 86cba18da06c1..5aa962973b6f8 100644 --- a/modules/lang-mustache/src/main/java/org/elasticsearch/script/mustache/MultiSearchTemplateResponse.java +++ b/modules/lang-mustache/src/main/java/org/elasticsearch/script/mustache/MultiSearchTemplateResponse.java @@ -9,6 +9,7 @@ package org.elasticsearch.script.mustache; import org.elasticsearch.ElasticsearchException; +import org.elasticsearch.ExceptionsHelper; import org.elasticsearch.Version; import org.elasticsearch.action.ActionResponse; import org.elasticsearch.action.search.MultiSearchResponse; @@ -146,13 +147,15 @@ public XContentBuilder toXContent(XContentBuilder builder, ToXContent.Params par builder.field("took", tookInMillis); builder.startArray(Fields.RESPONSES); for (Item item : items) { + builder.startObject(); if (item.isFailure()) { - builder.startObject(); ElasticsearchException.generateFailureXContent(builder, params, item.getFailure(), true); - builder.endObject(); + builder.field(Fields.STATUS, ExceptionsHelper.status(item.getFailure()).getStatus()); } else { - item.getResponse().toXContent(builder, params); + item.getResponse().innerToXContent(builder, params); + builder.field(Fields.STATUS, item.getResponse().status().getStatus()); } + builder.endObject(); } builder.endArray(); builder.endObject(); @@ -161,6 +164,7 @@ public XContentBuilder toXContent(XContentBuilder builder, ToXContent.Params par static final class Fields { static final String RESPONSES = "responses"; + static final String STATUS = "status"; } public static MultiSearchTemplateResponse fromXContext(XContentParser parser) { diff --git a/modules/lang-mustache/src/main/java/org/elasticsearch/script/mustache/SearchTemplateResponse.java b/modules/lang-mustache/src/main/java/org/elasticsearch/script/mustache/SearchTemplateResponse.java index 2e8fd3d64b032..cbf6e3003ec3d 100644 --- a/modules/lang-mustache/src/main/java/org/elasticsearch/script/mustache/SearchTemplateResponse.java +++ b/modules/lang-mustache/src/main/java/org/elasticsearch/script/mustache/SearchTemplateResponse.java @@ -95,15 +95,20 @@ public static SearchTemplateResponse fromXContent(XContentParser parser) throws @Override public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException { + builder.startObject(); + innerToXContent(builder, params); + builder.endObject(); + return builder; + } + + public XContentBuilder innerToXContent(XContentBuilder builder, Params params) throws IOException { if (hasResponse()) { - response.toXContent(builder, params); + response.innerToXContent(builder, params); } else { - builder.startObject(); // we can assume the template is always json as we convert it before compiling it try (InputStream stream = source.streamInput()) { builder.rawField(TEMPLATE_OUTPUT_FIELD.getPreferredName(), stream, XContentType.JSON); } - builder.endObject(); } return builder; } diff --git a/modules/lang-mustache/src/yamlRestTest/resources/rest-api-spec/test/lang_mustache/50_multi_search_template.yml b/modules/lang-mustache/src/yamlRestTest/resources/rest-api-spec/test/lang_mustache/50_multi_search_template.yml index c6422b93d8090..9d20462900ab8 100644 --- a/modules/lang-mustache/src/yamlRestTest/resources/rest-api-spec/test/lang_mustache/50_multi_search_template.yml +++ b/modules/lang-mustache/src/yamlRestTest/resources/rest-api-spec/test/lang_mustache/50_multi_search_template.yml @@ -31,6 +31,10 @@ setup: --- "Basic multi-search template": + - skip: + version: " - 8.2.99" + reason: "status field checks were added in 8.3" + - do: msearch_template: rest_total_hits_as_int: true @@ -62,10 +66,18 @@ setup: - match: { responses.2.hits.total: 4 } - match: { responses.3.hits.total: 1 } - length: { responses.3.hits.hits: 0 } + - match: { responses.0.status: 200 } + - match: { responses.1.status: 200 } + - match: { responses.2.status: 200 } + - match: { responses.3.status: 200 } --- "Multi-search template with errors": + + - skip: + version: " - 8.2.99" + reason: "status field checks were added in 8.3" - do: msearch_template: @@ -108,6 +120,10 @@ setup: - match: { responses.3.error.root_cause.0.reason: "/unknown.query.\\[unknown\\]/" } - match: { responses.4.error.root_cause.0.type: illegal_argument_exception } - match: { responses.4.error.root_cause.0.reason: "[rest_total_hits_as_int] cannot be used if the tracking of total hits is not accurate, got 1" } + - match: { responses.0.status: 200 } + - match: { responses.1.status: 500 } + - match: { responses.2.status: 200 } + - match: { responses.3.status: 400 } --- @@ -135,6 +151,10 @@ setup: --- "Basic multi-search using stored template": + - skip: + version: " - 8.2.99" + reason: "status field checks were added in 8.3" + - do: put_script: id: stored_template_1 @@ -164,7 +184,9 @@ setup: - match: { responses.0.hits.total: 2 } - match: { responses.1.hits.total: 1 } - match: { responses.2.hits.total: 1 } - + - match: { responses.0.status: 200 } + - match: { responses.1.status: 200 } + - match: { responses.2.status: 200 } - do: put_script: @@ -174,6 +196,11 @@ setup: --- "Test with rest_total_hits_as_int": + + - skip: + version: " - 8.2.99" + reason: "status field checks were added in 8.3" + - do: put_script: id: stored_template_1 @@ -205,3 +232,6 @@ setup: - match: { responses.1.hits.total.relation: eq } - match: { responses.2.hits.total.value: 1 } - match: { responses.1.hits.total.relation: eq } + - match: { responses.0.status: 200 } + - match: { responses.1.status: 200 } + - match: { responses.2.status: 200 }