Convert GetList from SingleDocument to QueryPage (elastic/elasticsearch#408)
Related to elastic/elasticsearch#314 Original commit: elastic/x-pack-elasticsearch@40702a3d5b
This commit is contained in:
parent
fb2bd73bc1
commit
7f6907da8b
|
@ -34,10 +34,11 @@ import org.elasticsearch.common.xcontent.XContentParser;
|
||||||
import org.elasticsearch.rest.RestStatus;
|
import org.elasticsearch.rest.RestStatus;
|
||||||
import org.elasticsearch.threadpool.ThreadPool;
|
import org.elasticsearch.threadpool.ThreadPool;
|
||||||
import org.elasticsearch.transport.TransportService;
|
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.lists.ListDocument;
|
||||||
import org.elasticsearch.xpack.prelert.utils.SingleDocument;
|
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
import java.util.Collections;
|
||||||
import java.util.Objects;
|
import java.util.Objects;
|
||||||
|
|
||||||
import static org.elasticsearch.action.ValidateActions.addValidationError;
|
import static org.elasticsearch.action.ValidateActions.addValidationError;
|
||||||
|
@ -125,44 +126,44 @@ public class GetListAction extends Action<GetListAction.Request, GetListAction.R
|
||||||
|
|
||||||
public static class Response extends ActionResponse implements StatusToXContent {
|
public static class Response extends ActionResponse implements StatusToXContent {
|
||||||
|
|
||||||
private SingleDocument<ListDocument> response;
|
private QueryPage<ListDocument> lists;
|
||||||
|
|
||||||
public Response(SingleDocument<ListDocument> document) {
|
public Response(QueryPage<ListDocument> lists) {
|
||||||
this.response = document;
|
this.lists = lists;
|
||||||
}
|
}
|
||||||
|
|
||||||
Response() {
|
Response() {
|
||||||
}
|
}
|
||||||
|
|
||||||
public SingleDocument<ListDocument> getResponse() {
|
public QueryPage<ListDocument> getLists() {
|
||||||
return response;
|
return lists;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void readFrom(StreamInput in) throws IOException {
|
public void readFrom(StreamInput in) throws IOException {
|
||||||
super.readFrom(in);
|
super.readFrom(in);
|
||||||
response = new SingleDocument<>(in, ListDocument::new);
|
lists = new QueryPage<>(in, ListDocument::new);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void writeTo(StreamOutput out) throws IOException {
|
public void writeTo(StreamOutput out) throws IOException {
|
||||||
super.writeTo(out);
|
super.writeTo(out);
|
||||||
response.writeTo(out);
|
lists.writeTo(out);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public RestStatus status() {
|
public RestStatus status() {
|
||||||
return response.status();
|
return lists.count() == 0 ? RestStatus.NOT_FOUND : RestStatus.OK;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
|
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
|
||||||
return response.toXContent(builder, params);
|
return lists.doXContentBody(builder, params);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public int hashCode() {
|
public int hashCode() {
|
||||||
return Objects.hash(response);
|
return Objects.hash(lists);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -174,7 +175,7 @@ public class GetListAction extends Action<GetListAction.Request, GetListAction.R
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
Response other = (Response) obj;
|
Response other = (Response) obj;
|
||||||
return Objects.equals(response, other.response);
|
return Objects.equals(lists, other.lists);
|
||||||
}
|
}
|
||||||
|
|
||||||
@SuppressWarnings("deprecation")
|
@SuppressWarnings("deprecation")
|
||||||
|
@ -183,9 +184,7 @@ public class GetListAction extends Action<GetListAction.Request, GetListAction.R
|
||||||
try {
|
try {
|
||||||
XContentBuilder builder = XContentFactory.jsonBuilder();
|
XContentBuilder builder = XContentFactory.jsonBuilder();
|
||||||
builder.prettyPrint();
|
builder.prettyPrint();
|
||||||
builder.startObject();
|
|
||||||
toXContent(builder, EMPTY_PARAMS);
|
toXContent(builder, EMPTY_PARAMS);
|
||||||
builder.endObject();
|
|
||||||
return builder.string();
|
return builder.string();
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
// So we have a stack trace logged somewhere
|
// So we have a stack trace logged somewhere
|
||||||
|
@ -231,17 +230,19 @@ public class GetListAction extends Action<GetListAction.Request, GetListAction.R
|
||||||
public void onResponse(GetResponse getDocResponse) {
|
public void onResponse(GetResponse getDocResponse) {
|
||||||
|
|
||||||
try {
|
try {
|
||||||
SingleDocument<ListDocument> responseBody;
|
QueryPage<ListDocument> responseBody;
|
||||||
if (getDocResponse.isExists()) {
|
if (getDocResponse.isExists()) {
|
||||||
BytesReference docSource = getDocResponse.getSourceAsBytesRef();
|
BytesReference docSource = getDocResponse.getSourceAsBytesRef();
|
||||||
XContentParser parser = XContentFactory.xContent(docSource).createParser(docSource);
|
XContentParser parser = XContentFactory.xContent(docSource).createParser(docSource);
|
||||||
ListDocument listDocument = ListDocument.PARSER.apply(parser, () -> parseFieldMatcher);
|
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 {
|
} 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) {
|
} catch (Exception e) {
|
||||||
this.onFailure(e);
|
this.onFailure(e);
|
||||||
}
|
}
|
||||||
|
|
|
@ -25,6 +25,9 @@ public class ListDocument extends ToXContentToBytes implements Writeable {
|
||||||
public static final ParseField ID = new ParseField("id");
|
public static final ParseField ID = new ParseField("id");
|
||||||
public static final ParseField ITEMS = new ParseField("items");
|
public static final ParseField ITEMS = new ParseField("items");
|
||||||
|
|
||||||
|
// For QueryPage
|
||||||
|
public static final ParseField RESULTS_FIELD = new ParseField("lists");
|
||||||
|
|
||||||
@SuppressWarnings("unchecked")
|
@SuppressWarnings("unchecked")
|
||||||
public static final ConstructingObjectParser<ListDocument, ParseFieldMatcherSupplier> PARSER = new ConstructingObjectParser<>(
|
public static final ConstructingObjectParser<ListDocument, ParseFieldMatcherSupplier> PARSER = new ConstructingObjectParser<>(
|
||||||
TYPE.getPreferredName(), a -> new ListDocument((String) a[0], (List<String>) a[1]));
|
TYPE.getPreferredName(), a -> new ListDocument((String) a[0], (List<String>) a[1]));
|
||||||
|
|
|
@ -6,6 +6,7 @@
|
||||||
package org.elasticsearch.xpack.prelert.action;
|
package org.elasticsearch.xpack.prelert.action;
|
||||||
|
|
||||||
import org.elasticsearch.xpack.prelert.action.GetListAction.Response;
|
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.lists.ListDocument;
|
||||||
import org.elasticsearch.xpack.prelert.support.AbstractStreamableTestCase;
|
import org.elasticsearch.xpack.prelert.support.AbstractStreamableTestCase;
|
||||||
import org.elasticsearch.xpack.prelert.utils.SingleDocument;
|
import org.elasticsearch.xpack.prelert.utils.SingleDocument;
|
||||||
|
@ -16,13 +17,11 @@ public class GetListActionResponseTests extends AbstractStreamableTestCase<GetLi
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected Response createTestInstance() {
|
protected Response createTestInstance() {
|
||||||
final SingleDocument<ListDocument> result;
|
final QueryPage<ListDocument> result;
|
||||||
if (randomBoolean()) {
|
|
||||||
result = SingleDocument.empty(ListDocument.TYPE.getPreferredName());
|
ListDocument doc = new ListDocument(
|
||||||
} else {
|
randomAsciiOfLengthBetween(1, 20), Collections.singletonList(randomAsciiOfLengthBetween(1, 20)));
|
||||||
result = new SingleDocument<>(ListDocument.TYPE.getPreferredName(),
|
result = new QueryPage<>(Collections.singletonList(doc), 1, ListDocument.RESULTS_FIELD);
|
||||||
new ListDocument(randomAsciiOfLengthBetween(1, 20), Collections.singletonList(randomAsciiOfLengthBetween(1, 20))));
|
|
||||||
}
|
|
||||||
return new Response(result);
|
return new Response(result);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -19,10 +19,6 @@ setup:
|
||||||
xpack.prelert.get_lists:
|
xpack.prelert.get_lists:
|
||||||
list_id: "do_not_exist"
|
list_id: "do_not_exist"
|
||||||
|
|
||||||
- match: { exists: false }
|
|
||||||
- match: { type: "list" }
|
|
||||||
- is_false: document
|
|
||||||
|
|
||||||
---
|
---
|
||||||
"Test get list API":
|
"Test get list API":
|
||||||
|
|
||||||
|
@ -30,10 +26,9 @@ setup:
|
||||||
xpack.prelert.get_lists:
|
xpack.prelert.get_lists:
|
||||||
list_id: "foo"
|
list_id: "foo"
|
||||||
|
|
||||||
- match: { exists: true }
|
- match: { count: 1 }
|
||||||
- match: { type: "list" }
|
|
||||||
- match:
|
- match:
|
||||||
document:
|
lists.0:
|
||||||
id: "foo"
|
id: "foo"
|
||||||
items: ["abc", "xyz"]
|
items: ["abc", "xyz"]
|
||||||
|
|
||||||
|
@ -52,10 +47,9 @@ setup:
|
||||||
xpack.prelert.get_lists:
|
xpack.prelert.get_lists:
|
||||||
list_id: "foo2"
|
list_id: "foo2"
|
||||||
|
|
||||||
- match: { exists: true }
|
- match: { count: 1 }
|
||||||
- match: { type: "list" }
|
|
||||||
- match:
|
- match:
|
||||||
document:
|
lists.0:
|
||||||
id: "foo2"
|
id: "foo2"
|
||||||
items: ["abc", "xyz"]
|
items: ["abc", "xyz"]
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue