Skip to content

Commit

Permalink
Add status field to MultiSearchTemplateResponse (elastic#85496)
Browse files Browse the repository at this point in the history
This change adds a `status` field to MultiSearchTemplateResponse items, 
as is already the case for MultiSearchResponse, making the two response
formats consistent.

Closes elastic#83029
  • Loading branch information
AbeleMM authored May 20, 2022
1 parent b333895 commit 8c2f429
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 7 deletions.
6 changes: 6 additions & 0 deletions docs/changelog/85496.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
pr: 85496
summary: Add status field to Multi Search Template Responses
area: Search
type: bug
issues:
- 83029
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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();
Expand All @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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 }


---
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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:
Expand All @@ -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
Expand Down Expand Up @@ -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 }

0 comments on commit 8c2f429

Please sign in to comment.