[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:
parent
ce25e1f4f3
commit
779e6f6dba
|
@ -98,7 +98,7 @@ extends Action<GetInfluencersAction.Request, GetInfluencersAction.Response, GetI
|
||||||
private PageParams pageParams = new PageParams();
|
private PageParams pageParams = new PageParams();
|
||||||
private double influencerScore = 0.0;
|
private double influencerScore = 0.0;
|
||||||
private String sort = Influencer.INFLUENCER_SCORE.getPreferredName();
|
private String sort = Influencer.INFLUENCER_SCORE.getPreferredName();
|
||||||
private boolean descending = false;
|
private boolean descending = true;
|
||||||
|
|
||||||
Request() {
|
Request() {
|
||||||
}
|
}
|
||||||
|
|
|
@ -41,8 +41,8 @@ public class RestGetBucketsAction extends BaseRestHandler {
|
||||||
protected RestChannelConsumer prepareRequest(RestRequest restRequest, NodeClient client) throws IOException {
|
protected RestChannelConsumer prepareRequest(RestRequest restRequest, NodeClient client) throws IOException {
|
||||||
String jobId = restRequest.param(Job.ID.getPreferredName());
|
String jobId = restRequest.param(Job.ID.getPreferredName());
|
||||||
final GetBucketsAction.Request request;
|
final GetBucketsAction.Request request;
|
||||||
if (restRequest.hasContent()) {
|
if (restRequest.hasContentOrSourceParam()) {
|
||||||
XContentParser parser = restRequest.contentParser();
|
XContentParser parser = restRequest.contentOrSourceParamParser();
|
||||||
request = GetBucketsAction.Request.parseRequest(jobId, parser);
|
request = GetBucketsAction.Request.parseRequest(jobId, parser);
|
||||||
} else {
|
} else {
|
||||||
request = new GetBucketsAction.Request(jobId);
|
request = new GetBucketsAction.Request(jobId);
|
||||||
|
|
|
@ -6,7 +6,6 @@
|
||||||
package org.elasticsearch.xpack.ml.rest.results;
|
package org.elasticsearch.xpack.ml.rest.results;
|
||||||
|
|
||||||
import org.elasticsearch.client.node.NodeClient;
|
import org.elasticsearch.client.node.NodeClient;
|
||||||
import org.elasticsearch.common.bytes.BytesReference;
|
|
||||||
import org.elasticsearch.common.settings.Settings;
|
import org.elasticsearch.common.settings.Settings;
|
||||||
import org.elasticsearch.common.xcontent.XContentParser;
|
import org.elasticsearch.common.xcontent.XContentParser;
|
||||||
import org.elasticsearch.rest.BaseRestHandler;
|
import org.elasticsearch.rest.BaseRestHandler;
|
||||||
|
@ -44,10 +43,9 @@ public class RestGetCategoriesAction extends BaseRestHandler {
|
||||||
String jobId = restRequest.param(Job.ID.getPreferredName());
|
String jobId = restRequest.param(Job.ID.getPreferredName());
|
||||||
Long categoryId = restRequest.hasParam(Request.CATEGORY_ID.getPreferredName()) ? Long.parseLong(
|
Long categoryId = restRequest.hasParam(Request.CATEGORY_ID.getPreferredName()) ? Long.parseLong(
|
||||||
restRequest.param(Request.CATEGORY_ID.getPreferredName())) : null;
|
restRequest.param(Request.CATEGORY_ID.getPreferredName())) : null;
|
||||||
BytesReference bodyBytes = restRequest.content();
|
|
||||||
|
|
||||||
if (bodyBytes != null && bodyBytes.length() > 0) {
|
if (restRequest.hasContentOrSourceParam()) {
|
||||||
XContentParser parser = restRequest.contentParser();
|
XContentParser parser = restRequest.contentOrSourceParamParser();
|
||||||
request = GetCategoriesAction.Request.parseRequest(jobId, parser);
|
request = GetCategoriesAction.Request.parseRequest(jobId, parser);
|
||||||
if (categoryId != null) {
|
if (categoryId != null) {
|
||||||
request.setCategoryId(categoryId);
|
request.setCategoryId(categoryId);
|
||||||
|
@ -62,8 +60,8 @@ public class RestGetCategoriesAction extends BaseRestHandler {
|
||||||
|| categoryId == null){
|
|| categoryId == null){
|
||||||
|
|
||||||
request.setPageParams(new PageParams(
|
request.setPageParams(new PageParams(
|
||||||
restRequest.paramAsInt(Request.FROM.getPreferredName(), 0),
|
restRequest.paramAsInt(Request.FROM.getPreferredName(), PageParams.DEFAULT_FROM),
|
||||||
restRequest.paramAsInt(Request.SIZE.getPreferredName(), 100)
|
restRequest.paramAsInt(Request.SIZE.getPreferredName(), PageParams.DEFAULT_SIZE)
|
||||||
));
|
));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -14,9 +14,8 @@ import org.elasticsearch.rest.RestRequest;
|
||||||
import org.elasticsearch.rest.action.RestToXContentListener;
|
import org.elasticsearch.rest.action.RestToXContentListener;
|
||||||
import org.elasticsearch.xpack.ml.MachineLearning;
|
import org.elasticsearch.xpack.ml.MachineLearning;
|
||||||
import org.elasticsearch.xpack.ml.action.GetInfluencersAction;
|
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.action.util.PageParams;
|
||||||
|
import org.elasticsearch.xpack.ml.job.config.Job;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
|
||||||
|
@ -37,21 +36,23 @@ public class RestGetInfluencersAction extends BaseRestHandler {
|
||||||
String start = restRequest.param(GetInfluencersAction.Request.START.getPreferredName());
|
String start = restRequest.param(GetInfluencersAction.Request.START.getPreferredName());
|
||||||
String end = restRequest.param(GetInfluencersAction.Request.END.getPreferredName());
|
String end = restRequest.param(GetInfluencersAction.Request.END.getPreferredName());
|
||||||
final GetInfluencersAction.Request request;
|
final GetInfluencersAction.Request request;
|
||||||
if (restRequest.hasContent()) {
|
if (restRequest.hasContentOrSourceParam()) {
|
||||||
XContentParser parser = restRequest.contentParser();
|
XContentParser parser = restRequest.contentOrSourceParamParser();
|
||||||
request = GetInfluencersAction.Request.parseRequest(jobId, parser);
|
request = GetInfluencersAction.Request.parseRequest(jobId, parser);
|
||||||
} else {
|
} else {
|
||||||
request = new GetInfluencersAction.Request(jobId);
|
request = new GetInfluencersAction.Request(jobId);
|
||||||
request.setStart(start);
|
request.setStart(start);
|
||||||
request.setEnd(end);
|
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),
|
request.setPageParams(new PageParams(restRequest.paramAsInt(PageParams.FROM.getPreferredName(), PageParams.DEFAULT_FROM),
|
||||||
restRequest.paramAsInt(PageParams.SIZE.getPreferredName(), PageParams.DEFAULT_SIZE)));
|
restRequest.paramAsInt(PageParams.SIZE.getPreferredName(), PageParams.DEFAULT_SIZE)));
|
||||||
request.setInfluencerScore(
|
request.setInfluencerScore(
|
||||||
Double.parseDouble(restRequest.param(GetInfluencersAction.Request.INFLUENCER_SCORE.getPreferredName(), "0.0")));
|
Double.parseDouble(restRequest.param(GetInfluencersAction.Request.INFLUENCER_SCORE.getPreferredName(),
|
||||||
request.setSort(restRequest.param(GetInfluencersAction.Request.SORT_FIELD.getPreferredName(),
|
String.valueOf(request.getInfluencerScore()))));
|
||||||
Influencer.INFLUENCER_SCORE.getPreferredName()));
|
request.setSort(restRequest.param(GetInfluencersAction.Request.SORT_FIELD.getPreferredName(), request.getSort()));
|
||||||
request.setDescending(restRequest.paramAsBoolean(GetInfluencersAction.Request.DESCENDING_SORT.getPreferredName(), true));
|
request.setDescending(restRequest.paramAsBoolean(GetInfluencersAction.Request.DESCENDING_SORT.getPreferredName(),
|
||||||
|
request.isDescending()));
|
||||||
}
|
}
|
||||||
|
|
||||||
return channel -> client.execute(GetInfluencersAction.INSTANCE, request, new RestToXContentListener<>(channel));
|
return channel -> client.execute(GetInfluencersAction.INSTANCE, request, new RestToXContentListener<>(channel));
|
||||||
|
|
|
@ -34,8 +34,8 @@ public class RestGetRecordsAction extends BaseRestHandler {
|
||||||
protected RestChannelConsumer prepareRequest(RestRequest restRequest, NodeClient client) throws IOException {
|
protected RestChannelConsumer prepareRequest(RestRequest restRequest, NodeClient client) throws IOException {
|
||||||
String jobId = restRequest.param(Job.ID.getPreferredName());
|
String jobId = restRequest.param(Job.ID.getPreferredName());
|
||||||
final GetRecordsAction.Request request;
|
final GetRecordsAction.Request request;
|
||||||
if (restRequest.hasContent()) {
|
if (restRequest.hasContentOrSourceParam()) {
|
||||||
XContentParser parser = restRequest.contentParser();
|
XContentParser parser = restRequest.contentOrSourceParamParser();
|
||||||
request = GetRecordsAction.Request.parseRequest(jobId, parser);
|
request = GetRecordsAction.Request.parseRequest(jobId, parser);
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
|
@ -47,7 +47,8 @@ public class RestGetRecordsAction extends BaseRestHandler {
|
||||||
request.setPageParams(new PageParams(restRequest.paramAsInt(PageParams.FROM.getPreferredName(), PageParams.DEFAULT_FROM),
|
request.setPageParams(new PageParams(restRequest.paramAsInt(PageParams.FROM.getPreferredName(), PageParams.DEFAULT_FROM),
|
||||||
restRequest.paramAsInt(PageParams.SIZE.getPreferredName(), PageParams.DEFAULT_SIZE)));
|
restRequest.paramAsInt(PageParams.SIZE.getPreferredName(), PageParams.DEFAULT_SIZE)));
|
||||||
request.setRecordScore(
|
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(),
|
request.setSort(restRequest.param(GetRecordsAction.Request.SORT.getPreferredName(),
|
||||||
AnomalyRecord.RECORD_SCORE.getPreferredName()));
|
AnomalyRecord.RECORD_SCORE.getPreferredName()));
|
||||||
request.setDescending(restRequest.paramAsBoolean(GetRecordsAction.Request.DESCENDING.getPreferredName(),
|
request.setDescending(restRequest.paramAsBoolean(GetRecordsAction.Request.DESCENDING.getPreferredName(),
|
||||||
|
|
|
@ -92,6 +92,41 @@ setup:
|
||||||
- is_false: buckets.1.partition_scores
|
- is_false: buckets.1.partition_scores
|
||||||
- is_false: buckets.2.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":
|
"Test get buckets given exclude_interim is false":
|
||||||
- do:
|
- do:
|
||||||
|
|
|
@ -69,6 +69,32 @@ setup:
|
||||||
- match: { categories.0.job_id: jobs-get-result-categories }
|
- match: { categories.0.job_id: jobs-get-result-categories }
|
||||||
- match: { categories.0.category_id: 2 }
|
- 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":
|
"Test get category by id":
|
||||||
- do:
|
- do:
|
||||||
|
|
|
@ -80,6 +80,41 @@ setup:
|
||||||
- match: { influencers.2.influencer_score: 50 }
|
- match: { influencers.2.influencer_score: 50 }
|
||||||
- match: { influencers.2.timestamp: 1464739200000 }
|
- 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":
|
"Test get influencers given exclude_interim false":
|
||||||
- do:
|
- do:
|
||||||
|
|
|
@ -60,6 +60,39 @@ setup:
|
||||||
- match: { records.1.job_id: jobs-get-result-records}
|
- match: { records.1.job_id: jobs-get-result-records}
|
||||||
- match: { records.1.result_type: record}
|
- 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":
|
"Test get records given exclude_interim is false":
|
||||||
- do:
|
- do:
|
||||||
|
|
Loading…
Reference in New Issue