Make SearchResponse a ToXContentObject

This commit is contained in:
javanna 2017-01-05 16:10:34 +01:00 committed by Luca Cavanna
parent 3393d1b409
commit d5510701a0
5 changed files with 17 additions and 7 deletions

View File

@ -159,7 +159,7 @@ public class MultiSearchResponse extends ActionResponse implements Iterable<Mult
ElasticsearchException.renderException(builder, params, item.getFailure());
builder.field(Fields.STATUS, ExceptionsHelper.status(item.getFailure()).getStatus());
} else {
item.getResponse().toXContent(builder, params);
item.getResponse().innerToXContent(builder, params);
builder.field(Fields.STATUS, item.getResponse().status().getStatus());
}
builder.endObject();

View File

@ -26,6 +26,7 @@ import org.elasticsearch.common.io.stream.StreamInput;
import org.elasticsearch.common.io.stream.StreamOutput;
import org.elasticsearch.common.unit.TimeValue;
import org.elasticsearch.common.xcontent.StatusToXContent;
import org.elasticsearch.common.xcontent.ToXContentObject;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.rest.RestStatus;
import org.elasticsearch.rest.action.RestActions;
@ -44,7 +45,7 @@ import static org.elasticsearch.search.internal.InternalSearchResponse.readInter
/**
* A response of a search request.
*/
public class SearchResponse extends ActionResponse implements StatusToXContent {
public class SearchResponse extends ActionResponse implements StatusToXContent, ToXContentObject {
private InternalSearchResponse internalResponse;
@ -181,6 +182,13 @@ public class SearchResponse extends ActionResponse implements StatusToXContent {
@Override
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 {
if (scrollId != null) {
builder.field(Fields._SCROLL_ID, scrollId);
}

View File

@ -182,9 +182,8 @@ public abstract class AbstractGeoTestCase extends ESIntegTestCase {
.order(SortOrder.ASC)).setSize(5000).get();
assertSearchResponse(response);
long totalHits = response.getHits().totalHits();
XContentBuilder builder = XContentFactory.jsonBuilder().startObject();
XContentBuilder builder = XContentFactory.jsonBuilder();
response.toXContent(builder, ToXContent.EMPTY_PARAMS);
builder.endObject();
logger.info("Full high_card_idx Response Content:\n{ {} }", builder.string());
for (int i = 0; i < totalHits; i++) {
SearchHit searchHit = response.getHits().getAt(i);

View File

@ -150,13 +150,13 @@ public class MultiSearchTemplateResponse extends ActionResponse implements Itera
builder.startObject();
builder.startArray(Fields.RESPONSES);
for (Item item : items) {
builder.startObject();
if (item.isFailure()) {
builder.startObject();
ElasticsearchException.renderException(builder, params, item.getFailure());
builder.endObject();
} else {
item.getResponse().toXContent(builder, params);
}
builder.endObject();
}
builder.endArray();
builder.endObject();

View File

@ -25,12 +25,13 @@ import org.elasticsearch.common.bytes.BytesReference;
import org.elasticsearch.common.io.stream.StreamInput;
import org.elasticsearch.common.io.stream.StreamOutput;
import org.elasticsearch.common.xcontent.StatusToXContent;
import org.elasticsearch.common.xcontent.ToXContentObject;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.rest.RestStatus;
import java.io.IOException;
public class SearchTemplateResponse extends ActionResponse implements StatusToXContent {
public class SearchTemplateResponse extends ActionResponse implements StatusToXContent, ToXContentObject {
/** Contains the source of the rendered template **/
private BytesReference source;
@ -80,7 +81,9 @@ public class SearchTemplateResponse extends ActionResponse implements StatusToX
if (hasResponse()) {
response.toXContent(builder, params);
} else {
builder.startObject();
builder.rawField("template_output", source);
builder.endObject();
}
return builder;
}