From ba243e7a9daeb16ab62e6bdbc0c3d971d15ad8ef Mon Sep 17 00:00:00 2001 From: javanna Date: Fri, 8 May 2015 18:26:45 +0200 Subject: [PATCH] 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 --- .../action/search/SearchResponse.java | 38 +------------------ .../rest/action/support/RestActions.java | 16 +++++--- 2 files changed, 13 insertions(+), 41 deletions(-) diff --git a/src/main/java/org/elasticsearch/action/search/SearchResponse.java b/src/main/java/org/elasticsearch/action/search/SearchResponse.java index f4f05366c6b..b0e2f213191 100644 --- a/src/main/java/org/elasticsearch/action/search/SearchResponse.java +++ b/src/main/java/org/elasticsearch/action/search/SearchResponse.java @@ -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); diff --git a/src/main/java/org/elasticsearch/rest/action/support/RestActions.java b/src/main/java/org/elasticsearch/rest/action/support/RestActions.java index 8bd59f1e8a9..6361af6895b 100644 --- a/src/main/java/org/elasticsearch/rest/action/support/RestActions.java +++ b/src/main/java/org/elasticsearch/rest/action/support/RestActions.java @@ -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(); }