From 7b5964f9c5de4ed597666a56c2e99de01785a591 Mon Sep 17 00:00:00 2001 From: xuzha Date: Mon, 28 Sep 2015 13:47:49 -0700 Subject: [PATCH] Add response into ClearScrollResponse When deleting an individual scroll ID, ES does produce a 200 in the header if successful and a 404 if the scroll ID wasn't found, but returns empty response body. It will be more user friendly to provide some information on whether the scroll deletion is successful. --- .../action/search/ClearScrollResponse.java | 10 ++++++++- .../search/scroll/SearchScrollIT.java | 21 +++++++++++++++++++ 2 files changed, 30 insertions(+), 1 deletion(-) diff --git a/core/src/main/java/org/elasticsearch/action/search/ClearScrollResponse.java b/core/src/main/java/org/elasticsearch/action/search/ClearScrollResponse.java index ffe476366f7..3540daa255c 100644 --- a/core/src/main/java/org/elasticsearch/action/search/ClearScrollResponse.java +++ b/core/src/main/java/org/elasticsearch/action/search/ClearScrollResponse.java @@ -19,12 +19,12 @@ package org.elasticsearch.action.search; -import org.elasticsearch.Version; import org.elasticsearch.action.ActionResponse; 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.XContentBuilder; +import org.elasticsearch.common.xcontent.XContentBuilderString; import org.elasticsearch.rest.RestStatus; import java.io.IOException; @@ -69,6 +69,8 @@ public class ClearScrollResponse extends ActionResponse implements StatusToXCont @Override public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException { + builder.field(Fields.SUCCEEDED, succeeded); + builder.field(Fields.NUMFREED, numFreed); return builder; } @@ -85,4 +87,10 @@ public class ClearScrollResponse extends ActionResponse implements StatusToXCont out.writeBoolean(succeeded); out.writeVInt(numFreed); } + + static final class Fields { + static final XContentBuilderString SUCCEEDED = new XContentBuilderString("succeeded"); + static final XContentBuilderString NUMFREED = new XContentBuilderString("num_freed"); + } + } diff --git a/core/src/test/java/org/elasticsearch/search/scroll/SearchScrollIT.java b/core/src/test/java/org/elasticsearch/search/scroll/SearchScrollIT.java index e4e7a69670b..4aeb4161fde 100644 --- a/core/src/test/java/org/elasticsearch/search/scroll/SearchScrollIT.java +++ b/core/src/test/java/org/elasticsearch/search/scroll/SearchScrollIT.java @@ -27,7 +27,10 @@ import org.elasticsearch.common.bytes.BytesReference; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.unit.TimeValue; import org.elasticsearch.common.util.concurrent.UncategorizedExecutionException; +import org.elasticsearch.common.xcontent.ToXContent; +import org.elasticsearch.common.xcontent.XContentBuilder; import org.elasticsearch.common.xcontent.XContentFactory; +import org.elasticsearch.common.xcontent.XContentParser; import org.elasticsearch.index.query.QueryBuilders; import org.elasticsearch.rest.RestStatus; import org.elasticsearch.rest.action.search.RestClearScrollAction; @@ -294,6 +297,7 @@ public class SearchScrollIT extends ESIntegTestCase { assertThat(clearResponse.isSucceeded(), is(true)); assertThat(clearResponse.getNumFreed(), greaterThan(0)); assertThat(clearResponse.status(), equalTo(RestStatus.OK)); + assertToXContentResponse(clearResponse, true, clearResponse.getNumFreed()); assertThrows(client().prepareSearchScroll(searchResponse1.getScrollId()).setScroll(TimeValue.timeValueMinutes(2)), RestStatus.NOT_FOUND); assertThrows(client().prepareSearchScroll(searchResponse2.getScrollId()).setScroll(TimeValue.timeValueMinutes(2)), RestStatus.NOT_FOUND); @@ -310,6 +314,7 @@ public class SearchScrollIT extends ESIntegTestCase { assertThat(response.isSucceeded(), is(true)); assertThat(response.getNumFreed(), equalTo(0)); assertThat(response.status(), equalTo(RestStatus.NOT_FOUND)); + assertToXContentResponse(response, true, response.getNumFreed()); } @Test @@ -404,6 +409,7 @@ public class SearchScrollIT extends ESIntegTestCase { assertThat(clearResponse.isSucceeded(), is(true)); assertThat(clearResponse.getNumFreed(), greaterThan(0)); assertThat(clearResponse.status(), equalTo(RestStatus.OK)); + assertToXContentResponse(clearResponse, true, clearResponse.getNumFreed()); assertThrows(internalCluster().transportClient().prepareSearchScroll(searchResponse1.getScrollId()).setScroll(TimeValue.timeValueMinutes(2)), RestStatus.NOT_FOUND); assertThrows(internalCluster().transportClient().prepareSearchScroll(searchResponse2.getScrollId()).setScroll(TimeValue.timeValueMinutes(2)), RestStatus.NOT_FOUND); @@ -593,4 +599,19 @@ public class SearchScrollIT extends ESIntegTestCase { } } + private void assertToXContentResponse(ClearScrollResponse response, boolean succeed, int numFreed) throws IOException { + XContentBuilder builder = XContentFactory.jsonBuilder(); + builder.startObject(); + response.toXContent(builder, ToXContent.EMPTY_PARAMS); + builder.endObject(); + + BytesReference bytesReference = builder.bytes(); + Map map; + try (XContentParser parser = XContentFactory.xContent(bytesReference).createParser(bytesReference)) { + map = parser.map(); + } + + assertThat(map.get("succeeded"), is(succeed)); + assertThat(map.get("num_freed"), equalTo(numFreed)); + } }