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

Add status field to MultiSearchTemplateResponse #85496

Merged
Merged
Show file tree
Hide file tree
Changes from 5 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
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 @@ -30,6 +30,9 @@ setup:

---
"Basic multi-search template":
- skip:
version: " - 8.2.99"
reason: "status field checks were added in 8.3"

- do:
msearch_template:
Expand Down Expand Up @@ -62,10 +65,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 +119,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 +150,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 +183,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 +195,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 +231,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 }