From 1335232e6b602c370df237f353f38e23bcfd35f4 Mon Sep 17 00:00:00 2001 From: Tony Zeng Date: Wed, 17 Jan 2018 11:04:04 -0600 Subject: [PATCH] Add toString() implementation for UpdateRequest (#27997) --- .../action/update/UpdateRequest.java | 25 +++++++++++++++++++ .../action/update/UpdateRequestTests.java | 12 +++++++++ 2 files changed, 37 insertions(+) diff --git a/server/src/main/java/org/elasticsearch/action/update/UpdateRequest.java b/server/src/main/java/org/elasticsearch/action/update/UpdateRequest.java index fa8c46edf5b..9bfb78f5058 100644 --- a/server/src/main/java/org/elasticsearch/action/update/UpdateRequest.java +++ b/server/src/main/java/org/elasticsearch/action/update/UpdateRequest.java @@ -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 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(); + } } diff --git a/server/src/test/java/org/elasticsearch/action/update/UpdateRequestTests.java b/server/src/test/java/org/elasticsearch/action/update/UpdateRequestTests.java index 7049d0fa9e9..36266026504 100644 --- a/server/src/test/java/org/elasticsearch/action/update/UpdateRequestTests.java +++ b/server/src/test/java/org/elasticsearch/action/update/UpdateRequestTests.java @@ -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]}")); + } }