Browse Source

Merge pull request #18586 from a2lin/msearch_error_fix

Adding status field in _msearch error request bodies
Tanguy Leroux 9 years ago
parent
commit
3c9712794e

+ 5 - 0
core/src/main/java/org/elasticsearch/action/search/MultiSearchResponse.java

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

+ 1 - 1
core/src/test/java/org/elasticsearch/action/search/MultiSearchRequestTests.java

@@ -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"))});
         XContentBuilder builder = XContentFactory.jsonBuilder();
         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());
     }
 

+ 4 - 4
docs/reference/search/multi-search.asciidoc

@@ -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.
 
 The response returns a `responses` array, which includes the search
-response for each search request matching its order in the original
-multi search request. If there was a complete failure for that specific
-search request, an object with `error` message will be returned in place
-of the actual search response.
+response and status code for each search request matching its order in
+the original multi search request. If there was a complete failure for that
+specific search request, an object with `error` message and corresponding
+status code will be returned in place of the actual search response.
 
 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

+ 20 - 0
rest-api-spec/src/main/resources/rest-api-spec/test/msearch/11_status.yaml

@@ -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 }