Java api: unify SearchResponse and BroadcastOperationResponse code around shards header
Different responses hold the shards header, search, count, flush etc. The code was duplicated in two different places, centralized in RestActions. It turns out that only the search response printed out the status field before the reason, which was added to all other broadcast responses too. Closes #11064
This commit is contained in:
parent
d2844e0210
commit
ba243e7a9d
|
@ -28,6 +28,7 @@ import org.elasticsearch.common.xcontent.XContentBuilder;
|
|||
import org.elasticsearch.common.xcontent.XContentBuilderString;
|
||||
import org.elasticsearch.common.xcontent.XContentFactory;
|
||||
import org.elasticsearch.rest.RestStatus;
|
||||
import org.elasticsearch.rest.action.support.RestActions;
|
||||
import org.elasticsearch.search.SearchHits;
|
||||
import org.elasticsearch.search.aggregations.Aggregations;
|
||||
import org.elasticsearch.search.internal.InternalSearchResponse;
|
||||
|
@ -161,15 +162,6 @@ public class SearchResponse extends ActionResponse implements StatusToXContent {
|
|||
|
||||
static final class Fields {
|
||||
static final XContentBuilderString _SCROLL_ID = new XContentBuilderString("_scroll_id");
|
||||
static final XContentBuilderString _SHARDS = new XContentBuilderString("_shards");
|
||||
static final XContentBuilderString TOTAL = new XContentBuilderString("total");
|
||||
static final XContentBuilderString SUCCESSFUL = new XContentBuilderString("successful");
|
||||
static final XContentBuilderString FAILED = new XContentBuilderString("failed");
|
||||
static final XContentBuilderString FAILURES = new XContentBuilderString("failures");
|
||||
static final XContentBuilderString STATUS = new XContentBuilderString("status");
|
||||
static final XContentBuilderString INDEX = new XContentBuilderString("index");
|
||||
static final XContentBuilderString SHARD = new XContentBuilderString("shard");
|
||||
static final XContentBuilderString REASON = new XContentBuilderString("reason");
|
||||
static final XContentBuilderString TOOK = new XContentBuilderString("took");
|
||||
static final XContentBuilderString TIMED_OUT = new XContentBuilderString("timed_out");
|
||||
static final XContentBuilderString TERMINATED_EARLY = new XContentBuilderString("terminated_early");
|
||||
|
@ -185,37 +177,11 @@ public class SearchResponse extends ActionResponse implements StatusToXContent {
|
|||
if (isTerminatedEarly() != null) {
|
||||
builder.field(Fields.TERMINATED_EARLY, isTerminatedEarly());
|
||||
}
|
||||
builder.startObject(Fields._SHARDS);
|
||||
builder.field(Fields.TOTAL, getTotalShards());
|
||||
builder.field(Fields.SUCCESSFUL, getSuccessfulShards());
|
||||
builder.field(Fields.FAILED, getFailedShards());
|
||||
|
||||
if (shardFailures.length > 0) {
|
||||
builder.startArray(Fields.FAILURES);
|
||||
for (ShardSearchFailure shardFailure : shardFailures) {
|
||||
builder.startObject();
|
||||
if (shardFailure.shard() != null) {
|
||||
builder.field(Fields.INDEX, shardFailure.shard().index());
|
||||
builder.field(Fields.SHARD, shardFailure.shard().shardId());
|
||||
}
|
||||
builder.field(Fields.STATUS, shardFailure.status().getStatus());
|
||||
builder.field(Fields.REASON, shardFailure.reason());
|
||||
builder.endObject();
|
||||
}
|
||||
builder.endArray();
|
||||
}
|
||||
|
||||
builder.endObject();
|
||||
RestActions.buildBroadcastShardsHeader(builder, getTotalShards(), getSuccessfulShards(), getFailedShards(), getShardFailures());
|
||||
internalResponse.toXContent(builder, params);
|
||||
return builder;
|
||||
}
|
||||
|
||||
public static SearchResponse readSearchResponse(StreamInput in) throws IOException {
|
||||
SearchResponse response = new SearchResponse();
|
||||
response.readFrom(in);
|
||||
return response;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void readFrom(StreamInput in) throws IOException {
|
||||
super.readFrom(in);
|
||||
|
|
|
@ -64,17 +64,22 @@ public class RestActions {
|
|||
static final XContentBuilderString FAILURES = new XContentBuilderString("failures");
|
||||
static final XContentBuilderString INDEX = new XContentBuilderString("index");
|
||||
static final XContentBuilderString SHARD = new XContentBuilderString("shard");
|
||||
static final XContentBuilderString STATUS = new XContentBuilderString("status");
|
||||
static final XContentBuilderString REASON = new XContentBuilderString("reason");
|
||||
}
|
||||
|
||||
public static void buildBroadcastShardsHeader(XContentBuilder builder, BroadcastOperationResponse response) throws IOException {
|
||||
buildBroadcastShardsHeader(builder, response.getTotalShards(), response.getSuccessfulShards(), response.getFailedShards(), response.getShardFailures());
|
||||
}
|
||||
|
||||
public static void buildBroadcastShardsHeader(XContentBuilder builder, int total, int successful, int failed, ShardOperationFailedException[] shardFailures) throws IOException {
|
||||
builder.startObject(Fields._SHARDS);
|
||||
builder.field(Fields.TOTAL, response.getTotalShards());
|
||||
builder.field(Fields.SUCCESSFUL, response.getSuccessfulShards());
|
||||
builder.field(Fields.FAILED, response.getFailedShards());
|
||||
if (response.getShardFailures() != null && response.getShardFailures().length > 0) {
|
||||
builder.field(Fields.TOTAL, total);
|
||||
builder.field(Fields.SUCCESSFUL, successful);
|
||||
builder.field(Fields.FAILED, failed);
|
||||
if (shardFailures != null && shardFailures.length > 0) {
|
||||
builder.startArray(Fields.FAILURES);
|
||||
for (ShardOperationFailedException shardFailure : response.getShardFailures()) {
|
||||
for (ShardOperationFailedException shardFailure : shardFailures) {
|
||||
builder.startObject();
|
||||
if (shardFailure.index() != null) {
|
||||
builder.field(Fields.INDEX, shardFailure.index(), XContentBuilder.FieldCaseConversion.NONE);
|
||||
|
@ -82,6 +87,7 @@ public class RestActions {
|
|||
if (shardFailure.shardId() != -1) {
|
||||
builder.field(Fields.SHARD, shardFailure.shardId());
|
||||
}
|
||||
builder.field(Fields.STATUS, shardFailure.status().getStatus());
|
||||
builder.field(Fields.REASON, shardFailure.reason());
|
||||
builder.endObject();
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue