Add toString() implementation for UpdateRequest (#27997)

This commit is contained in:
Tony Zeng 2018-01-17 11:04:04 -06:00 committed by David Turner
parent 4ea9ddb7d3
commit 1335232e6b
2 changed files with 37 additions and 0 deletions

View File

@ -19,6 +19,7 @@
package org.elasticsearch.action.update;
import java.util.Arrays;
import org.elasticsearch.action.ActionRequestValidationException;
import org.elasticsearch.action.DocWriteRequest;
import org.elasticsearch.action.index.IndexRequest;
@ -893,4 +894,28 @@ public class UpdateRequest extends InstanceShardOperationRequest<UpdateRequest>
builder.endObject();
return builder;
}
@Override
public String toString() {
StringBuilder res = new StringBuilder()
.append("update {[").append(index)
.append("][").append(type)
.append("][").append(id).append("]");
res.append(", doc_as_upsert[").append(docAsUpsert).append("]");
if (doc != null) {
res.append(", doc[").append(doc).append("]");
}
if (script != null) {
res.append(", script[").append(script).append("]");
}
if (upsertRequest != null) {
res.append(", upsert[").append(upsertRequest).append("]");
}
res.append(", scripted_upsert[").append(scriptedUpsert).append("]");
res.append(", detect_noop[").append(detectNoop).append("]");
if (fields != null) {
res.append(", fields[").append(Arrays.toString(fields)).append("]");
}
return res.append("}").toString();
}
}

View File

@ -639,4 +639,16 @@ public class UpdateRequestTests extends ESTestCase {
assertThat(result.action(), instanceOf(UpdateResponse.class));
assertThat(result.getResponseResult(), equalTo(DocWriteResponse.Result.NOOP));
}
public void testToString() throws IOException {
UpdateRequest request = new UpdateRequest("test", "type1", "1")
.script(mockInlineScript("ctx._source.body = \"foo\""));
assertThat(request.toString(), equalTo("update {[test][type1][1], doc_as_upsert[false], "
+ "script[Script{type=inline, lang='mock', idOrCode='ctx._source.body = \"foo\"', options={}, params={}}], "
+ "scripted_upsert[false], detect_noop[true]}"));
request = new UpdateRequest("test", "type1", "1").fromXContent(
createParser(JsonXContent.jsonXContent, new BytesArray("{\"doc\": {\"body\": \"bar\"}}")));
assertThat(request.toString(), equalTo("update {[test][type1][1], doc_as_upsert[false], "
+ "doc[index {[null][null][null], source[{\"body\":\"bar\"}]}], scripted_upsert[false], detect_noop[true]}"));
}
}