Merge pull request #18586 from a2lin/msearch_error_fix

Adding status field in _msearch error request bodies
This commit is contained in:
Tanguy Leroux 2016-06-16 14:31:39 +02:00 committed by GitHub
commit 3c9712794e
4 changed files with 30 additions and 5 deletions

View File

@ -20,6 +20,7 @@
package org.elasticsearch.action.search; package org.elasticsearch.action.search;
import org.elasticsearch.ElasticsearchException; import org.elasticsearch.ElasticsearchException;
import org.elasticsearch.ExceptionsHelper;
import org.elasticsearch.action.ActionResponse; import org.elasticsearch.action.ActionResponse;
import org.elasticsearch.common.Nullable; import org.elasticsearch.common.Nullable;
import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamInput;
@ -28,6 +29,7 @@ import org.elasticsearch.common.io.stream.Streamable;
import org.elasticsearch.common.xcontent.ToXContent; import org.elasticsearch.common.xcontent.ToXContent;
import org.elasticsearch.common.xcontent.XContentBuilder; import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentFactory; import org.elasticsearch.common.xcontent.XContentFactory;
import org.elasticsearch.rest.RestStatus;
import java.io.IOException; import java.io.IOException;
import java.util.Arrays; import java.util.Arrays;
@ -155,8 +157,10 @@ public class MultiSearchResponse extends ActionResponse implements Iterable<Mult
builder.startObject(); builder.startObject();
if (item.isFailure()) { if (item.isFailure()) {
ElasticsearchException.renderThrowable(builder, params, item.getFailure()); ElasticsearchException.renderThrowable(builder, params, item.getFailure());
builder.field(Fields.STATUS, ExceptionsHelper.status(item.getFailure()).getStatus());
} else { } else {
item.getResponse().toXContent(builder, params); item.getResponse().toXContent(builder, params);
builder.field(Fields.STATUS, item.getResponse().status().getStatus());
} }
builder.endObject(); builder.endObject();
} }
@ -166,6 +170,7 @@ public class MultiSearchResponse extends ActionResponse implements Iterable<Mult
static final class Fields { static final class Fields {
static final String RESPONSES = "responses"; static final String RESPONSES = "responses";
static final String STATUS = "status";
static final String ERROR = "error"; static final String ERROR = "error";
static final String ROOT_CAUSE = "root_cause"; static final String ROOT_CAUSE = "root_cause";
} }

View File

@ -160,7 +160,7 @@ public class MultiSearchRequestTests extends ESTestCase {
MultiSearchResponse response = new MultiSearchResponse(new MultiSearchResponse.Item[]{new MultiSearchResponse.Item(null, new IllegalStateException("foobar")), new MultiSearchResponse.Item(null, new IllegalStateException("baaaaaazzzz"))}); MultiSearchResponse response = new MultiSearchResponse(new MultiSearchResponse.Item[]{new MultiSearchResponse.Item(null, new IllegalStateException("foobar")), new MultiSearchResponse.Item(null, new IllegalStateException("baaaaaazzzz"))});
XContentBuilder builder = XContentFactory.jsonBuilder(); XContentBuilder builder = XContentFactory.jsonBuilder();
response.toXContent(builder, ToXContent.EMPTY_PARAMS); response.toXContent(builder, ToXContent.EMPTY_PARAMS);
assertEquals("\"responses\"[{\"error\":{\"root_cause\":[{\"type\":\"illegal_state_exception\",\"reason\":\"foobar\"}],\"type\":\"illegal_state_exception\",\"reason\":\"foobar\"}},{\"error\":{\"root_cause\":[{\"type\":\"illegal_state_exception\",\"reason\":\"baaaaaazzzz\"}],\"type\":\"illegal_state_exception\",\"reason\":\"baaaaaazzzz\"}}]", assertEquals("\"responses\"[{\"error\":{\"root_cause\":[{\"type\":\"illegal_state_exception\",\"reason\":\"foobar\"}],\"type\":\"illegal_state_exception\",\"reason\":\"foobar\"},\"status\":500},{\"error\":{\"root_cause\":[{\"type\":\"illegal_state_exception\",\"reason\":\"baaaaaazzzz\"}],\"type\":\"illegal_state_exception\",\"reason\":\"baaaaaazzzz\"},\"status\":500}]",
builder.string()); builder.string());
} }

View File

@ -42,10 +42,10 @@ Note, the above includes an example of an empty header (can also be just
without any content) which is supported as well. without any content) which is supported as well.
The response returns a `responses` array, which includes the search The response returns a `responses` array, which includes the search
response for each search request matching its order in the original response and status code for each search request matching its order in
multi search request. If there was a complete failure for that specific the original multi search request. If there was a complete failure for that
search request, an object with `error` message will be returned in place specific search request, an object with `error` message and corresponding
of the actual search response. status code will be returned in place of the actual search response.
The endpoint allows to also search against an index/indices and The endpoint allows to also search against an index/indices and
type/types in the URI itself, in which case it will be used as the type/types in the URI itself, in which case it will be used as the

View File

@ -0,0 +1,20 @@
---
setup:
- do:
indices.create:
index: test_1
---
"Check Status":
- do:
msearch:
body:
- index: test_2
- query:
match_all: {}
- index: test_1
- query:
match_all: {}
- match: { responses.0.status: 404 }
- match: { responses.1.status: 200 }