Make PercolateResponse a ToXContentObject

This commit is contained in:
javanna 2017-01-05 16:45:00 +01:00 committed by Luca Cavanna
parent 8edf59c9e7
commit 4e49860f68
2 changed files with 15 additions and 6 deletions

View File

@ -24,7 +24,7 @@ import org.elasticsearch.common.Nullable;
import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamInput;
import org.elasticsearch.common.io.stream.StreamOutput; import org.elasticsearch.common.io.stream.StreamOutput;
import org.elasticsearch.common.io.stream.Streamable; import org.elasticsearch.common.io.stream.Streamable;
import org.elasticsearch.common.xcontent.ToXContent; import org.elasticsearch.common.xcontent.ToXContentObject;
import org.elasticsearch.common.xcontent.XContentBuilder; import org.elasticsearch.common.xcontent.XContentBuilder;
import java.io.IOException; import java.io.IOException;
@ -40,7 +40,7 @@ import java.util.Iterator;
* @deprecated Instead use multi search API with {@link PercolateQueryBuilder} * @deprecated Instead use multi search API with {@link PercolateQueryBuilder}
*/ */
@Deprecated @Deprecated
public class MultiPercolateResponse extends ActionResponse implements Iterable<MultiPercolateResponse.Item>, ToXContent { public class MultiPercolateResponse extends ActionResponse implements Iterable<MultiPercolateResponse.Item>, ToXContentObject {
private Item[] items; private Item[] items;
@ -73,17 +73,19 @@ public class MultiPercolateResponse extends ActionResponse implements Iterable<M
@Override @Override
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException { public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
builder.startObject();
builder.startArray(Fields.RESPONSES); builder.startArray(Fields.RESPONSES);
for (MultiPercolateResponse.Item item : items) { for (MultiPercolateResponse.Item item : items) {
builder.startObject();
if (item.isFailure()) { if (item.isFailure()) {
builder.startObject();
ElasticsearchException.renderException(builder, params, item.getFailure()); ElasticsearchException.renderException(builder, params, item.getFailure());
builder.endObject();
} else { } else {
item.getResponse().toXContent(builder, params); item.getResponse().toXContent(builder, params);
} }
builder.endObject();
} }
builder.endArray(); builder.endArray();
builder.endObject();
return builder; return builder;
} }

View File

@ -26,7 +26,7 @@ import org.elasticsearch.common.io.stream.StreamOutput;
import org.elasticsearch.common.io.stream.Streamable; import org.elasticsearch.common.io.stream.Streamable;
import org.elasticsearch.common.text.Text; import org.elasticsearch.common.text.Text;
import org.elasticsearch.common.unit.TimeValue; import org.elasticsearch.common.unit.TimeValue;
import org.elasticsearch.common.xcontent.ToXContent; import org.elasticsearch.common.xcontent.ToXContentObject;
import org.elasticsearch.common.xcontent.XContentBuilder; import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.rest.action.RestActions; import org.elasticsearch.rest.action.RestActions;
import org.elasticsearch.search.aggregations.InternalAggregations; import org.elasticsearch.search.aggregations.InternalAggregations;
@ -45,7 +45,7 @@ import java.util.Map;
* @deprecated Instead use search API with {@link PercolateQueryBuilder} * @deprecated Instead use search API with {@link PercolateQueryBuilder}
*/ */
@Deprecated @Deprecated
public class PercolateResponse extends BroadcastResponse implements Iterable<PercolateResponse.Match>, ToXContent { public class PercolateResponse extends BroadcastResponse implements Iterable<PercolateResponse.Match>, ToXContentObject {
public static final Match[] EMPTY = new Match[0]; public static final Match[] EMPTY = new Match[0];
// PercolateQuery emits this score if no 'query' is defined in the percolate request // PercolateQuery emits this score if no 'query' is defined in the percolate request
@ -113,6 +113,13 @@ public class PercolateResponse extends BroadcastResponse implements Iterable<Per
@Override @Override
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException { 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 {
builder.field(Fields.TOOK, tookInMillis); builder.field(Fields.TOOK, tookInMillis);
RestActions.buildBroadcastShardsHeader(builder, params, this); RestActions.buildBroadcastShardsHeader(builder, params, this);