[ML] Handle requests with source (elastic/x-pack-elasticsearch#1553)

REST endpoints that support GET and POST need
to also support source parsing. As these
endpoints can accept a body but some clients
do not allow doing a GET with a request body,
elasticsearch has support for parsing via a
source URI parameter. This commit adds source
handling to all such endpoints.

relates elastic/x-pack-elasticsearch#1204

Original commit: elastic/x-pack-elasticsearch@3949ea31fe
This commit is contained in:
Dimitris Athanasiou 2017-05-25 11:57:16 +01:00 committed by GitHub
parent ce25e1f4f3
commit 779e6f6dba
9 changed files with 150 additions and 21 deletions

View File

@ -98,7 +98,7 @@ extends Action<GetInfluencersAction.Request, GetInfluencersAction.Response, GetI
private PageParams pageParams = new PageParams();
private double influencerScore = 0.0;
private String sort = Influencer.INFLUENCER_SCORE.getPreferredName();
private boolean descending = false;
private boolean descending = true;
Request() {
}

View File

@ -41,8 +41,8 @@ public class RestGetBucketsAction extends BaseRestHandler {
protected RestChannelConsumer prepareRequest(RestRequest restRequest, NodeClient client) throws IOException {
String jobId = restRequest.param(Job.ID.getPreferredName());
final GetBucketsAction.Request request;
if (restRequest.hasContent()) {
XContentParser parser = restRequest.contentParser();
if (restRequest.hasContentOrSourceParam()) {
XContentParser parser = restRequest.contentOrSourceParamParser();
request = GetBucketsAction.Request.parseRequest(jobId, parser);
} else {
request = new GetBucketsAction.Request(jobId);

View File

@ -6,7 +6,6 @@
package org.elasticsearch.xpack.ml.rest.results;
import org.elasticsearch.client.node.NodeClient;
import org.elasticsearch.common.bytes.BytesReference;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.xcontent.XContentParser;
import org.elasticsearch.rest.BaseRestHandler;
@ -44,10 +43,9 @@ public class RestGetCategoriesAction extends BaseRestHandler {
String jobId = restRequest.param(Job.ID.getPreferredName());
Long categoryId = restRequest.hasParam(Request.CATEGORY_ID.getPreferredName()) ? Long.parseLong(
restRequest.param(Request.CATEGORY_ID.getPreferredName())) : null;
BytesReference bodyBytes = restRequest.content();
if (bodyBytes != null && bodyBytes.length() > 0) {
XContentParser parser = restRequest.contentParser();
if (restRequest.hasContentOrSourceParam()) {
XContentParser parser = restRequest.contentOrSourceParamParser();
request = GetCategoriesAction.Request.parseRequest(jobId, parser);
if (categoryId != null) {
request.setCategoryId(categoryId);
@ -62,8 +60,8 @@ public class RestGetCategoriesAction extends BaseRestHandler {
|| categoryId == null){
request.setPageParams(new PageParams(
restRequest.paramAsInt(Request.FROM.getPreferredName(), 0),
restRequest.paramAsInt(Request.SIZE.getPreferredName(), 100)
restRequest.paramAsInt(Request.FROM.getPreferredName(), PageParams.DEFAULT_FROM),
restRequest.paramAsInt(Request.SIZE.getPreferredName(), PageParams.DEFAULT_SIZE)
));
}
}

View File

@ -14,9 +14,8 @@ import org.elasticsearch.rest.RestRequest;
import org.elasticsearch.rest.action.RestToXContentListener;
import org.elasticsearch.xpack.ml.MachineLearning;
import org.elasticsearch.xpack.ml.action.GetInfluencersAction;
import org.elasticsearch.xpack.ml.job.config.Job;
import org.elasticsearch.xpack.ml.job.results.Influencer;
import org.elasticsearch.xpack.ml.action.util.PageParams;
import org.elasticsearch.xpack.ml.job.config.Job;
import java.io.IOException;
@ -37,21 +36,23 @@ public class RestGetInfluencersAction extends BaseRestHandler {
String start = restRequest.param(GetInfluencersAction.Request.START.getPreferredName());
String end = restRequest.param(GetInfluencersAction.Request.END.getPreferredName());
final GetInfluencersAction.Request request;
if (restRequest.hasContent()) {
XContentParser parser = restRequest.contentParser();
if (restRequest.hasContentOrSourceParam()) {
XContentParser parser = restRequest.contentOrSourceParamParser();
request = GetInfluencersAction.Request.parseRequest(jobId, parser);
} else {
request = new GetInfluencersAction.Request(jobId);
request.setStart(start);
request.setEnd(end);
request.setExcludeInterim(restRequest.paramAsBoolean(GetInfluencersAction.Request.EXCLUDE_INTERIM.getPreferredName(), false));
request.setExcludeInterim(restRequest.paramAsBoolean(GetInfluencersAction.Request.EXCLUDE_INTERIM.getPreferredName(),
request.isExcludeInterim()));
request.setPageParams(new PageParams(restRequest.paramAsInt(PageParams.FROM.getPreferredName(), PageParams.DEFAULT_FROM),
restRequest.paramAsInt(PageParams.SIZE.getPreferredName(), PageParams.DEFAULT_SIZE)));
request.setInfluencerScore(
Double.parseDouble(restRequest.param(GetInfluencersAction.Request.INFLUENCER_SCORE.getPreferredName(), "0.0")));
request.setSort(restRequest.param(GetInfluencersAction.Request.SORT_FIELD.getPreferredName(),
Influencer.INFLUENCER_SCORE.getPreferredName()));
request.setDescending(restRequest.paramAsBoolean(GetInfluencersAction.Request.DESCENDING_SORT.getPreferredName(), true));
Double.parseDouble(restRequest.param(GetInfluencersAction.Request.INFLUENCER_SCORE.getPreferredName(),
String.valueOf(request.getInfluencerScore()))));
request.setSort(restRequest.param(GetInfluencersAction.Request.SORT_FIELD.getPreferredName(), request.getSort()));
request.setDescending(restRequest.paramAsBoolean(GetInfluencersAction.Request.DESCENDING_SORT.getPreferredName(),
request.isDescending()));
}
return channel -> client.execute(GetInfluencersAction.INSTANCE, request, new RestToXContentListener<>(channel));

View File

@ -34,8 +34,8 @@ public class RestGetRecordsAction extends BaseRestHandler {
protected RestChannelConsumer prepareRequest(RestRequest restRequest, NodeClient client) throws IOException {
String jobId = restRequest.param(Job.ID.getPreferredName());
final GetRecordsAction.Request request;
if (restRequest.hasContent()) {
XContentParser parser = restRequest.contentParser();
if (restRequest.hasContentOrSourceParam()) {
XContentParser parser = restRequest.contentOrSourceParamParser();
request = GetRecordsAction.Request.parseRequest(jobId, parser);
}
else {
@ -47,7 +47,8 @@ public class RestGetRecordsAction extends BaseRestHandler {
request.setPageParams(new PageParams(restRequest.paramAsInt(PageParams.FROM.getPreferredName(), PageParams.DEFAULT_FROM),
restRequest.paramAsInt(PageParams.SIZE.getPreferredName(), PageParams.DEFAULT_SIZE)));
request.setRecordScore(
Double.parseDouble(restRequest.param(GetRecordsAction.Request.RECORD_SCORE_FILTER.getPreferredName(), "0.0")));
Double.parseDouble(restRequest.param(GetRecordsAction.Request.RECORD_SCORE_FILTER.getPreferredName(),
String.valueOf(request.getRecordScoreFilter()))));
request.setSort(restRequest.param(GetRecordsAction.Request.SORT.getPreferredName(),
AnomalyRecord.RECORD_SCORE.getPreferredName()));
request.setDescending(restRequest.paramAsBoolean(GetRecordsAction.Request.DESCENDING.getPreferredName(),

View File

@ -92,6 +92,41 @@ setup:
- is_false: buckets.1.partition_scores
- is_false: buckets.2.partition_scores
---
"Test get buckets with paging":
- do:
xpack.ml.get_buckets:
job_id: "jobs-get-result-buckets"
from: 1
size: 2
- match: { count: 3 }
- length: { buckets: 2 }
- match: { buckets.0.timestamp: 1470009600000 }
- match: { buckets.0.result_type: bucket}
- match: { buckets.1.timestamp: 1470096000000 }
- match: { buckets.1.result_type: bucket}
---
"Test get buckets with paging in body":
- do:
xpack.ml.get_buckets:
job_id: "jobs-get-result-buckets"
body: >
{
"page": {
"from": 1,
"size": 2
}
}
- match: { count: 3 }
- length: { buckets: 2 }
- match: { buckets.0.timestamp: 1470009600000 }
- match: { buckets.0.result_type: bucket}
- match: { buckets.1.timestamp: 1470096000000 }
- match: { buckets.1.result_type: bucket}
---
"Test get buckets given exclude_interim is false":
- do:

View File

@ -69,6 +69,32 @@ setup:
- match: { categories.0.job_id: jobs-get-result-categories }
- match: { categories.0.category_id: 2 }
---
"Test post get categories with pagination":
- do:
xpack.ml.get_categories:
job_id: "jobs-get-result-categories"
body: >
{
"page": { "size": 1}
}
- length: { categories: 1 }
- match: { categories.0.job_id: jobs-get-result-categories }
- match: { categories.0.category_id: 1 }
- do:
xpack.ml.get_categories:
job_id: "jobs-get-result-categories"
body: >
{
"page": { "from":1, "size": 1}
}
- length: { categories: 1 }
- match: { categories.0.job_id: jobs-get-result-categories }
- match: { categories.0.category_id: 2 }
---
"Test get category by id":
- do:

View File

@ -80,6 +80,41 @@ setup:
- match: { influencers.2.influencer_score: 50 }
- match: { influencers.2.timestamp: 1464739200000 }
---
"Test get influencers with paging":
- do:
xpack.ml.get_influencers:
job_id: "get-influencers-test"
from: 1
size: 2
- match: { count: 3 }
- length: { influencers: 2 }
- match: { influencers.0.influencer_score: 60 }
- match: { influencers.0.timestamp: 1464912000000 }
- match: { influencers.1.influencer_score: 50 }
- match: { influencers.1.timestamp: 1464739200000 }
---
"Test get influencers with paging in body":
- do:
xpack.ml.get_influencers:
job_id: "get-influencers-test"
body: >
{
"page": {
"from": 1,
"size": 2
}
}
- match: { count: 3 }
- length: { influencers: 2 }
- match: { influencers.0.influencer_score: 60 }
- match: { influencers.0.timestamp: 1464912000000 }
- match: { influencers.1.influencer_score: 50 }
- match: { influencers.1.timestamp: 1464739200000 }
---
"Test get influencers given exclude_interim false":
- do:

View File

@ -60,6 +60,39 @@ setup:
- match: { records.1.job_id: jobs-get-result-records}
- match: { records.1.result_type: record}
---
"Test get records with paging":
- do:
xpack.ml.get_records:
job_id: "jobs-get-result-records"
from: 1
size: 1
- match: { count: 2 }
- length: { records: 1 }
- match: { records.0.timestamp: 1464739200000 }
- match: { records.0.job_id: jobs-get-result-records}
- match: { records.0.result_type: record}
---
"Test get records with paging in body":
- do:
xpack.ml.get_records:
job_id: "jobs-get-result-records"
body: >
{
"page": {
"from": 1,
"size": 1
}
}
- match: { count: 2 }
- length: { records: 1 }
- match: { records.0.timestamp: 1464739200000 }
- match: { records.0.job_id: jobs-get-result-records}
- match: { records.0.result_type: record}
---
"Test get records given exclude_interim is false":
- do: