From 7f6907da8bbad23cded276a324508a736953b5a7 Mon Sep 17 00:00:00 2001 From: Zachary Tong Date: Tue, 29 Nov 2016 13:22:54 -0500 Subject: [PATCH] Convert GetList from SingleDocument to QueryPage (elastic/elasticsearch#408) Related to elastic/elasticsearch#314 Original commit: elastic/x-pack-elasticsearch@40702a3d5b998b5d205d0bfa7f69956e7734afa7 --- .../xpack/prelert/action/GetListAction.java | 39 ++++++++++--------- .../xpack/prelert/lists/ListDocument.java | 3 ++ .../action/GetListActionResponseTests.java | 13 +++---- .../rest-api-spec/test/list_crud.yaml | 14 ++----- 4 files changed, 33 insertions(+), 36 deletions(-) diff --git a/elasticsearch/src/main/java/org/elasticsearch/xpack/prelert/action/GetListAction.java b/elasticsearch/src/main/java/org/elasticsearch/xpack/prelert/action/GetListAction.java index df1887fd4a8..8e79d064382 100644 --- a/elasticsearch/src/main/java/org/elasticsearch/xpack/prelert/action/GetListAction.java +++ b/elasticsearch/src/main/java/org/elasticsearch/xpack/prelert/action/GetListAction.java @@ -34,10 +34,11 @@ import org.elasticsearch.common.xcontent.XContentParser; import org.elasticsearch.rest.RestStatus; import org.elasticsearch.threadpool.ThreadPool; import org.elasticsearch.transport.TransportService; +import org.elasticsearch.xpack.prelert.job.persistence.QueryPage; import org.elasticsearch.xpack.prelert.lists.ListDocument; -import org.elasticsearch.xpack.prelert.utils.SingleDocument; import java.io.IOException; +import java.util.Collections; import java.util.Objects; import static org.elasticsearch.action.ValidateActions.addValidationError; @@ -125,44 +126,44 @@ public class GetListAction extends Action response; + private QueryPage lists; - public Response(SingleDocument document) { - this.response = document; + public Response(QueryPage lists) { + this.lists = lists; } Response() { } - public SingleDocument getResponse() { - return response; + public QueryPage getLists() { + return lists; } @Override public void readFrom(StreamInput in) throws IOException { super.readFrom(in); - response = new SingleDocument<>(in, ListDocument::new); + lists = new QueryPage<>(in, ListDocument::new); } @Override public void writeTo(StreamOutput out) throws IOException { super.writeTo(out); - response.writeTo(out); + lists.writeTo(out); } @Override public RestStatus status() { - return response.status(); + return lists.count() == 0 ? RestStatus.NOT_FOUND : RestStatus.OK; } @Override public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException { - return response.toXContent(builder, params); + return lists.doXContentBody(builder, params); } @Override public int hashCode() { - return Objects.hash(response); + return Objects.hash(lists); } @Override @@ -174,7 +175,7 @@ public class GetListAction extends Action responseBody; + QueryPage responseBody; if (getDocResponse.isExists()) { BytesReference docSource = getDocResponse.getSourceAsBytesRef(); XContentParser parser = XContentFactory.xContent(docSource).createParser(docSource); ListDocument listDocument = ListDocument.PARSER.apply(parser, () -> parseFieldMatcher); - responseBody = new SingleDocument<>(ListDocument.TYPE.getPreferredName(), listDocument); + responseBody = new QueryPage<>(Collections.singletonList(listDocument), 1, ListDocument.RESULTS_FIELD); + + Response listResponse = new Response(responseBody); + listener.onResponse(listResponse); } else { - responseBody = SingleDocument.empty(ListDocument.TYPE.getPreferredName()); + this.onFailure(QueryPage.emptyQueryPage(ListDocument.RESULTS_FIELD)); } - Response listResponse = new Response(responseBody); - listener.onResponse(listResponse); + } catch (Exception e) { this.onFailure(e); } diff --git a/elasticsearch/src/main/java/org/elasticsearch/xpack/prelert/lists/ListDocument.java b/elasticsearch/src/main/java/org/elasticsearch/xpack/prelert/lists/ListDocument.java index 3a1705cc27e..3111b520903 100644 --- a/elasticsearch/src/main/java/org/elasticsearch/xpack/prelert/lists/ListDocument.java +++ b/elasticsearch/src/main/java/org/elasticsearch/xpack/prelert/lists/ListDocument.java @@ -25,6 +25,9 @@ public class ListDocument extends ToXContentToBytes implements Writeable { public static final ParseField ID = new ParseField("id"); public static final ParseField ITEMS = new ParseField("items"); + // For QueryPage + public static final ParseField RESULTS_FIELD = new ParseField("lists"); + @SuppressWarnings("unchecked") public static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>( TYPE.getPreferredName(), a -> new ListDocument((String) a[0], (List) a[1])); diff --git a/elasticsearch/src/test/java/org/elasticsearch/xpack/prelert/action/GetListActionResponseTests.java b/elasticsearch/src/test/java/org/elasticsearch/xpack/prelert/action/GetListActionResponseTests.java index 7c5c9619f12..ed4ae3e6bb1 100644 --- a/elasticsearch/src/test/java/org/elasticsearch/xpack/prelert/action/GetListActionResponseTests.java +++ b/elasticsearch/src/test/java/org/elasticsearch/xpack/prelert/action/GetListActionResponseTests.java @@ -6,6 +6,7 @@ package org.elasticsearch.xpack.prelert.action; import org.elasticsearch.xpack.prelert.action.GetListAction.Response; +import org.elasticsearch.xpack.prelert.job.persistence.QueryPage; import org.elasticsearch.xpack.prelert.lists.ListDocument; import org.elasticsearch.xpack.prelert.support.AbstractStreamableTestCase; import org.elasticsearch.xpack.prelert.utils.SingleDocument; @@ -16,13 +17,11 @@ public class GetListActionResponseTests extends AbstractStreamableTestCase result; - if (randomBoolean()) { - result = SingleDocument.empty(ListDocument.TYPE.getPreferredName()); - } else { - result = new SingleDocument<>(ListDocument.TYPE.getPreferredName(), - new ListDocument(randomAsciiOfLengthBetween(1, 20), Collections.singletonList(randomAsciiOfLengthBetween(1, 20)))); - } + final QueryPage result; + + ListDocument doc = new ListDocument( + randomAsciiOfLengthBetween(1, 20), Collections.singletonList(randomAsciiOfLengthBetween(1, 20))); + result = new QueryPage<>(Collections.singletonList(doc), 1, ListDocument.RESULTS_FIELD); return new Response(result); } diff --git a/elasticsearch/src/test/resources/rest-api-spec/test/list_crud.yaml b/elasticsearch/src/test/resources/rest-api-spec/test/list_crud.yaml index 390871ee01e..61d0ba03b20 100644 --- a/elasticsearch/src/test/resources/rest-api-spec/test/list_crud.yaml +++ b/elasticsearch/src/test/resources/rest-api-spec/test/list_crud.yaml @@ -19,10 +19,6 @@ setup: xpack.prelert.get_lists: list_id: "do_not_exist" - - match: { exists: false } - - match: { type: "list" } - - is_false: document - --- "Test get list API": @@ -30,10 +26,9 @@ setup: xpack.prelert.get_lists: list_id: "foo" - - match: { exists: true } - - match: { type: "list" } + - match: { count: 1 } - match: - document: + lists.0: id: "foo" items: ["abc", "xyz"] @@ -52,10 +47,9 @@ setup: xpack.prelert.get_lists: list_id: "foo2" - - match: { exists: true } - - match: { type: "list" } + - match: { count: 1 } - match: - document: + lists.0: id: "foo2" items: ["abc", "xyz"]