Make DocWriteResponse a ToXContentObject

This involved changing also index, delete, update so that bulk can print them out in its own format.
This commit is contained in:
javanna 2017-01-05 16:34:35 +01:00 committed by Luca Cavanna
parent d5510701a0
commit 8edf59c9e7
6 changed files with 22 additions and 18 deletions

View File

@ -28,6 +28,7 @@ import org.elasticsearch.common.io.stream.StreamInput;
import org.elasticsearch.common.io.stream.StreamOutput;
import org.elasticsearch.common.io.stream.Writeable;
import org.elasticsearch.common.xcontent.StatusToXContent;
import org.elasticsearch.common.xcontent.ToXContentObject;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.index.IndexSettings;
import org.elasticsearch.index.seqno.SequenceNumbersService;
@ -42,7 +43,7 @@ import java.util.Locale;
/**
* A base class for the response of a write operation that involves a single doc
*/
public abstract class DocWriteResponse extends ReplicationResponse implements WriteResponse, StatusToXContent {
public abstract class DocWriteResponse extends ReplicationResponse implements WriteResponse, StatusToXContent, ToXContentObject {
/**
* An enum that represents the the results of CRUD operations, primarily used to communicate the type of
@ -244,15 +245,22 @@ public abstract class DocWriteResponse extends ReplicationResponse implements Wr
}
@Override
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
public final 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 {
ReplicationResponse.ShardInfo shardInfo = getShardInfo();
builder.field("_index", shardId.getIndexName())
.field("_type", type)
.field("_id", id)
.field("_version", version)
.field("result", getResult().getLowercase());
.field("_type", type)
.field("_id", id)
.field("_version", version)
.field("result", getResult().getLowercase());
if (forcedRefresh) {
builder.field("forced_refresh", forcedRefresh);
builder.field("forced_refresh", true);
}
shardInfo.toXContent(builder, params);
if (getSeqNo() >= 0) {

View File

@ -54,7 +54,7 @@ public class BulkItemResponse implements Streamable, StatusToXContent {
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
builder.startObject(opType.getLowercase());
if (failure == null) {
response.toXContent(builder, params);
response.innerToXContent(builder, params);
builder.field(Fields.STATUS, response.status().getStatus());
} else {
builder.field(Fields._INDEX, failure.getIndex());

View File

@ -48,9 +48,9 @@ public class DeleteResponse extends DocWriteResponse {
}
@Override
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
public XContentBuilder innerToXContent(XContentBuilder builder, Params params) throws IOException {
builder.field("found", result == Result.DELETED);
super.toXContent(builder, params);
super.innerToXContent(builder, params);
return builder;
}

View File

@ -62,8 +62,8 @@ public class IndexResponse extends DocWriteResponse {
}
@Override
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
super.toXContent(builder, params);
public XContentBuilder innerToXContent(XContentBuilder builder, Params params) throws IOException {
super.innerToXContent(builder, params);
builder.field("created", result == Result.CREATED);
return builder;
}

View File

@ -87,8 +87,8 @@ public class UpdateResponse extends DocWriteResponse {
}
@Override
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
super.toXContent(builder, params);
public XContentBuilder innerToXContent(XContentBuilder builder, Params params) throws IOException {
super.innerToXContent(builder, params);
if (getGetResult() != null) {
builder.startObject(Fields.GET);
getGetResult().toXContentEmbedded(builder, params);

View File

@ -99,18 +99,14 @@ public class DocWriteResponseTests extends ESTestCase {
response.setShardInfo(new ShardInfo(1, 1));
response.setForcedRefresh(false);
try (XContentBuilder builder = JsonXContent.contentBuilder()) {
builder.startObject();
response.toXContent(builder, ToXContent.EMPTY_PARAMS);
builder.endObject();
try (XContentParser parser = createParser(JsonXContent.jsonXContent, builder.bytes())) {
assertThat(parser.map(), not(hasKey("forced_refresh")));
}
}
response.setForcedRefresh(true);
try (XContentBuilder builder = JsonXContent.contentBuilder()) {
builder.startObject();
response.toXContent(builder, ToXContent.EMPTY_PARAMS);
builder.endObject();
try (XContentParser parser = createParser(JsonXContent.jsonXContent, builder.bytes())) {
assertThat(parser.map(), hasEntry("forced_refresh", true));
}