Skip to content

Commit 3c0fe7d

Browse files
HLRC: Add ML Get Buckets API (#33056)
Relates #29827
1 parent 500d533 commit 3c0fe7d

File tree

15 files changed

+1159
-16
lines changed

15 files changed

+1159
-16
lines changed

client/rest-high-level/src/main/java/org/elasticsearch/client/MLRequestConverters.java

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
import org.elasticsearch.protocol.xpack.ml.CloseJobRequest;
2929
import org.elasticsearch.protocol.xpack.ml.DeleteJobRequest;
3030
import org.elasticsearch.protocol.xpack.ml.GetJobRequest;
31+
import org.elasticsearch.protocol.xpack.ml.GetBucketsRequest;
3132
import org.elasticsearch.protocol.xpack.ml.OpenJobRequest;
3233
import org.elasticsearch.protocol.xpack.ml.PutJobRequest;
3334

@@ -69,7 +70,7 @@ static Request getJob(GetJobRequest getJobRequest) {
6970
return request;
7071
}
7172

72-
static Request openJob(OpenJobRequest openJobRequest) throws IOException {
73+
static Request openJob(OpenJobRequest openJobRequest) {
7374
String endpoint = new EndpointBuilder()
7475
.addPathPartAsIs("_xpack")
7576
.addPathPartAsIs("ml")
@@ -109,4 +110,18 @@ static Request deleteJob(DeleteJobRequest deleteJobRequest) {
109110

110111
return request;
111112
}
113+
114+
static Request getBuckets(GetBucketsRequest getBucketsRequest) throws IOException {
115+
String endpoint = new EndpointBuilder()
116+
.addPathPartAsIs("_xpack")
117+
.addPathPartAsIs("ml")
118+
.addPathPartAsIs("anomaly_detectors")
119+
.addPathPart(getBucketsRequest.getJobId())
120+
.addPathPartAsIs("results")
121+
.addPathPartAsIs("buckets")
122+
.build();
123+
Request request = new Request(HttpGet.METHOD_NAME, endpoint);
124+
request.setEntity(createEntity(getBucketsRequest, REQUEST_BODY_CONTENT_TYPE));
125+
return request;
126+
}
112127
}

client/rest-high-level/src/main/java/org/elasticsearch/client/MachineLearningClient.java

Lines changed: 48 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@
2323
import org.elasticsearch.protocol.xpack.ml.CloseJobResponse;
2424
import org.elasticsearch.protocol.xpack.ml.DeleteJobRequest;
2525
import org.elasticsearch.protocol.xpack.ml.DeleteJobResponse;
26+
import org.elasticsearch.protocol.xpack.ml.GetBucketsRequest;
27+
import org.elasticsearch.protocol.xpack.ml.GetBucketsResponse;
2628
import org.elasticsearch.protocol.xpack.ml.GetJobRequest;
2729
import org.elasticsearch.protocol.xpack.ml.GetJobResponse;
2830
import org.elasticsearch.protocol.xpack.ml.OpenJobRequest;
@@ -54,7 +56,7 @@ public final class MachineLearningClient {
5456
* For additional info
5557
* see <a href="https://www.elastic.co/guide/en/elasticsearch/reference/current/ml-put-job.html">ML PUT job documentation</a>
5658
*
57-
* @param request the PutJobRequest containing the {@link org.elasticsearch.protocol.xpack.ml.job.config.Job} settings
59+
* @param request The PutJobRequest containing the {@link org.elasticsearch.protocol.xpack.ml.job.config.Job} settings
5860
* @param options Additional request options (e.g. headers), use {@link RequestOptions#DEFAULT} if nothing needs to be customized
5961
* @return PutJobResponse with enclosed {@link org.elasticsearch.protocol.xpack.ml.job.config.Job} object
6062
* @throws IOException when there is a serialization issue sending the request or receiving the response
@@ -73,7 +75,7 @@ public PutJobResponse putJob(PutJobRequest request, RequestOptions options) thro
7375
* For additional info
7476
* see <a href="https://www.elastic.co/guide/en/elasticsearch/reference/current/ml-put-job.html">ML PUT job documentation</a>
7577
*
76-
* @param request the request containing the {@link org.elasticsearch.protocol.xpack.ml.job.config.Job} settings
78+
* @param request The request containing the {@link org.elasticsearch.protocol.xpack.ml.job.config.Job} settings
7779
* @param options Additional request options (e.g. headers), use {@link RequestOptions#DEFAULT} if nothing needs to be customized
7880
* @param listener Listener to be notified upon request completion
7981
*/
@@ -93,7 +95,7 @@ public void putJobAsync(PutJobRequest request, RequestOptions options, ActionLis
9395
* For additional info
9496
* see <a href="https://www.elastic.co/guide/en/elasticsearch/reference/current/ml-get-job.html"></a>
9597
* </p>
96-
* @param request {@link GetJobRequest} request containing a list of jobId(s) and additional options
98+
* @param request {@link GetJobRequest} Request containing a list of jobId(s) and additional options
9799
* @param options Additional request options (e.g. headers), use {@link RequestOptions#DEFAULT} if nothing needs to be customized
98100
* @return {@link GetJobResponse} response object containing
99101
* the {@link org.elasticsearch.protocol.xpack.ml.job.config.Job} objects and the number of jobs found
@@ -114,7 +116,7 @@ public GetJobResponse getJob(GetJobRequest request, RequestOptions options) thro
114116
* For additional info
115117
* see <a href="https://www.elastic.co/guide/en/elasticsearch/reference/current/ml-get-job.html"></a>
116118
* </p>
117-
* @param request {@link GetJobRequest} request containing a list of jobId(s) and additional options
119+
* @param request {@link GetJobRequest} Request containing a list of jobId(s) and additional options
118120
* @param options Additional request options (e.g. headers), use {@link RequestOptions#DEFAULT} if nothing needs to be customized
119121
* @param listener Listener to be notified with {@link GetJobResponse} upon request completion
120122
*/
@@ -133,7 +135,7 @@ public void getJobAsync(GetJobRequest request, RequestOptions options, ActionLis
133135
* For additional info
134136
* see <a href="http://www.elastic.co/guide/en/elasticsearch/reference/current/ml-delete-job.html">ML Delete Job documentation</a>
135137
* </p>
136-
* @param request the request to delete the job
138+
* @param request The request to delete the job
137139
* @param options Additional request options (e.g. headers), use {@link RequestOptions#DEFAULT} if nothing needs to be customized
138140
* @return action acknowledgement
139141
* @throws IOException when there is a serialization issue sending the request or receiving the response
@@ -152,7 +154,7 @@ public DeleteJobResponse deleteJob(DeleteJobRequest request, RequestOptions opti
152154
* For additional info
153155
* see <a href="http://www.elastic.co/guide/en/elasticsearch/reference/current/ml-delete-job.html">ML Delete Job documentation</a>
154156
* </p>
155-
* @param request the request to delete the job
157+
* @param request The request to delete the job
156158
* @param options Additional request options (e.g. headers), use {@link RequestOptions#DEFAULT} if nothing needs to be customized
157159
* @param listener Listener to be notified upon request completion
158160
*/
@@ -176,7 +178,7 @@ public void deleteJobAsync(DeleteJobRequest request, RequestOptions options, Act
176178
* For additional info
177179
* see <a href="https://www.elastic.co/guide/en/elasticsearch/reference/current/ml-open-job.html"></a>
178180
* </p>
179-
* @param request request containing job_id and additional optional options
181+
* @param request Request containing job_id and additional optional options
180182
* @param options Additional request options (e.g. headers), use {@link RequestOptions#DEFAULT} if nothing needs to be customized
181183
* @return response containing if the job was successfully opened or not.
182184
* @throws IOException when there is a serialization issue sending the request or receiving the response
@@ -199,7 +201,7 @@ public OpenJobResponse openJob(OpenJobRequest request, RequestOptions options) t
199201
* For additional info
200202
* see <a href="https://www.elastic.co/guide/en/elasticsearch/reference/current/ml-open-job.html"></a>
201203
* </p>
202-
* @param request request containing job_id and additional optional options
204+
* @param request Request containing job_id and additional optional options
203205
* @param options Additional request options (e.g. headers), use {@link RequestOptions#DEFAULT} if nothing needs to be customized
204206
* @param listener Listener to be notified upon request completion
205207
*/
@@ -217,7 +219,7 @@ public void openJobAsync(OpenJobRequest request, RequestOptions options, ActionL
217219
*
218220
* A closed job cannot receive data or perform analysis operations, but you can still explore and navigate results.
219221
*
220-
* @param request request containing job_ids and additional options. See {@link CloseJobRequest}
222+
* @param request Request containing job_ids and additional options. See {@link CloseJobRequest}
221223
* @param options Additional request options (e.g. headers), use {@link RequestOptions#DEFAULT} if nothing needs to be customized
222224
* @return response containing if the job was successfully closed or not.
223225
* @throws IOException when there is a serialization issue sending the request or receiving the response
@@ -235,7 +237,7 @@ public CloseJobResponse closeJob(CloseJobRequest request, RequestOptions options
235237
*
236238
* A closed job cannot receive data or perform analysis operations, but you can still explore and navigate results.
237239
*
238-
* @param request request containing job_ids and additional options. See {@link CloseJobRequest}
240+
* @param request Request containing job_ids and additional options. See {@link CloseJobRequest}
239241
* @param options Additional request options (e.g. headers), use {@link RequestOptions#DEFAULT} if nothing needs to be customized
240242
* @param listener Listener to be notified upon request completion
241243
*/
@@ -247,4 +249,40 @@ public void closeJobAsync(CloseJobRequest request, RequestOptions options, Actio
247249
listener,
248250
Collections.emptySet());
249251
}
252+
253+
/**
254+
* Gets the buckets for a Machine Learning Job.
255+
* <p>
256+
* For additional info
257+
* see <a href="https://www.elastic.co/guide/en/elasticsearch/reference/current/ml-get-bucket.html">ML GET buckets documentation</a>
258+
*
259+
* @param request The request
260+
* @param options Additional request options (e.g. headers), use {@link RequestOptions#DEFAULT} if nothing needs to be customized
261+
*/
262+
public GetBucketsResponse getBuckets(GetBucketsRequest request, RequestOptions options) throws IOException {
263+
return restHighLevelClient.performRequestAndParseEntity(request,
264+
MLRequestConverters::getBuckets,
265+
options,
266+
GetBucketsResponse::fromXContent,
267+
Collections.emptySet());
268+
}
269+
270+
/**
271+
* Gets the buckets for a Machine Learning Job, notifies listener once the requested buckets are retrieved.
272+
* <p>
273+
* For additional info
274+
* see <a href="https://www.elastic.co/guide/en/elasticsearch/reference/current/ml-get-bucket.html">ML GET buckets documentation</a>
275+
*
276+
* @param request The request
277+
* @param options Additional request options (e.g. headers), use {@link RequestOptions#DEFAULT} if nothing needs to be customized
278+
* @param listener Listener to be notified upon request completion
279+
*/
280+
public void getBucketsAsync(GetBucketsRequest request, RequestOptions options, ActionListener<GetBucketsResponse> listener) {
281+
restHighLevelClient.performRequestAsyncAndParseEntity(request,
282+
MLRequestConverters::getBuckets,
283+
options,
284+
GetBucketsResponse::fromXContent,
285+
listener,
286+
Collections.emptySet());
287+
}
250288
}

client/rest-high-level/src/test/java/org/elasticsearch/client/MLRequestConvertersTests.java

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,17 +22,20 @@
2222
import org.apache.http.client.methods.HttpDelete;
2323
import org.apache.http.client.methods.HttpGet;
2424
import org.apache.http.client.methods.HttpPost;
25+
import org.apache.http.client.methods.HttpPut;
2526
import org.elasticsearch.common.unit.TimeValue;
2627
import org.elasticsearch.common.xcontent.XContentParser;
2728
import org.elasticsearch.common.xcontent.json.JsonXContent;
2829
import org.elasticsearch.protocol.xpack.ml.CloseJobRequest;
2930
import org.elasticsearch.protocol.xpack.ml.DeleteJobRequest;
31+
import org.elasticsearch.protocol.xpack.ml.GetBucketsRequest;
3032
import org.elasticsearch.protocol.xpack.ml.GetJobRequest;
3133
import org.elasticsearch.protocol.xpack.ml.OpenJobRequest;
3234
import org.elasticsearch.protocol.xpack.ml.PutJobRequest;
3335
import org.elasticsearch.protocol.xpack.ml.job.config.AnalysisConfig;
3436
import org.elasticsearch.protocol.xpack.ml.job.config.Detector;
3537
import org.elasticsearch.protocol.xpack.ml.job.config.Job;
38+
import org.elasticsearch.protocol.xpack.ml.job.util.PageParams;
3639
import org.elasticsearch.test.ESTestCase;
3740

3841
import java.io.ByteArrayOutputStream;
@@ -49,6 +52,7 @@ public void testPutJob() throws IOException {
4952

5053
Request request = MLRequestConverters.putJob(putJobRequest);
5154

55+
assertEquals(HttpPut.METHOD_NAME, request.getMethod());
5256
assertThat(request.getEndpoint(), equalTo("/_xpack/ml/anomaly_detectors/foo"));
5357
try (XContentParser parser = createParser(JsonXContent.jsonXContent, request.getEntity().getContent())) {
5458
Job parsedJob = Job.PARSER.apply(parser, null).build();
@@ -118,6 +122,23 @@ public void testDeleteJob() {
118122
assertEquals(Boolean.toString(true), request.getParameters().get("force"));
119123
}
120124

125+
public void testGetBuckets() throws IOException {
126+
String jobId = randomAlphaOfLength(10);
127+
GetBucketsRequest getBucketsRequest = new GetBucketsRequest(jobId);
128+
getBucketsRequest.setPageParams(new PageParams(100, 300));
129+
getBucketsRequest.setAnomalyScore(75.0);
130+
getBucketsRequest.setSort("anomaly_score");
131+
getBucketsRequest.setDescending(true);
132+
133+
Request request = MLRequestConverters.getBuckets(getBucketsRequest);
134+
assertEquals(HttpGet.METHOD_NAME, request.getMethod());
135+
assertEquals("/_xpack/ml/anomaly_detectors/" + jobId + "/results/buckets", request.getEndpoint());
136+
try (XContentParser parser = createParser(JsonXContent.jsonXContent, request.getEntity().getContent())) {
137+
GetBucketsRequest parsedRequest = GetBucketsRequest.PARSER.apply(parser, null);
138+
assertThat(parsedRequest, equalTo(getBucketsRequest));
139+
}
140+
}
141+
121142
private static Job createValidJob(String jobId) {
122143
AnalysisConfig.Builder analysisConfig = AnalysisConfig.builder(Collections.singletonList(
123144
Detector.builder().setFunction("count").build()));

0 commit comments

Comments
 (0)