diff --git a/plugin/src/main/java/org/elasticsearch/xpack/ml/rest/results/RestGetBucketsAction.java b/plugin/src/main/java/org/elasticsearch/xpack/ml/rest/results/RestGetBucketsAction.java index a653f272089..b73a8bd0755 100644 --- a/plugin/src/main/java/org/elasticsearch/xpack/ml/rest/results/RestGetBucketsAction.java +++ b/plugin/src/main/java/org/elasticsearch/xpack/ml/rest/results/RestGetBucketsAction.java @@ -6,6 +6,7 @@ package org.elasticsearch.xpack.ml.rest.results; import org.elasticsearch.client.node.NodeClient; +import org.elasticsearch.common.Strings; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.xcontent.XContentParser; import org.elasticsearch.rest.BaseRestHandler; @@ -45,20 +46,23 @@ public class RestGetBucketsAction extends BaseRestHandler { @Override protected RestChannelConsumer prepareRequest(RestRequest restRequest, NodeClient client) throws IOException { String jobId = restRequest.param(Job.ID.getPreferredName()); + String timestamp = restRequest.param(GetBucketsAction.Request.TIMESTAMP.getPreferredName()); final GetBucketsAction.Request request; if (restRequest.hasContentOrSourceParam()) { XContentParser parser = restRequest.contentOrSourceParamParser(); request = GetBucketsAction.Request.parseRequest(jobId, parser); + + // A timestamp in the URL overrides any timestamp that may also have been set in the body + if (!Strings.isNullOrEmpty(timestamp)) { + request.setTimestamp(timestamp); + } } else { request = new GetBucketsAction.Request(jobId); // Check if the REST param is set first so mutually exclusive - // options will only cause an error if set - if (restRequest.hasParam(GetBucketsAction.Request.TIMESTAMP.getPreferredName())) { - String timestamp = restRequest.param(GetBucketsAction.Request.TIMESTAMP.getPreferredName()); - if (timestamp != null && !timestamp.isEmpty()) { - request.setTimestamp(timestamp); - } + // options will cause an error if set + if (!Strings.isNullOrEmpty(timestamp)) { + request.setTimestamp(timestamp); } // multiple bucket options if (restRequest.hasParam(PageParams.FROM.getPreferredName()) || restRequest.hasParam(PageParams.SIZE.getPreferredName())) { diff --git a/plugin/src/test/resources/rest-api-spec/test/ml/jobs_get_result_buckets.yml b/plugin/src/test/resources/rest-api-spec/test/ml/jobs_get_result_buckets.yml index 02bebcf33a5..2a7a7970e5d 100644 --- a/plugin/src/test/resources/rest-api-spec/test/ml/jobs_get_result_buckets.yml +++ b/plugin/src/test/resources/rest-api-spec/test/ml/jobs_get_result_buckets.yml @@ -170,6 +170,18 @@ setup: - match: { buckets.0.job_id: jobs-get-result-buckets } - match: { buckets.0.result_type: bucket} +--- +"Test result single bucket api with empty body": + - do: + xpack.ml.get_buckets: + job_id: "jobs-get-result-buckets" + timestamp: "2016-06-01T00:00:00Z" + body: {} + + - match: { buckets.0.timestamp: 1464739200000} + - match: { buckets.0.job_id: jobs-get-result-buckets } + - match: { buckets.0.result_type: bucket} + --- "Test mutually-exclusive params": - do: