Skip to content

Commit 54fe7fb

Browse files
HLRC: Add ML get overall buckets API (#33297)
Relates #29827
1 parent 246a7df commit 54fe7fb

13 files changed

+1050
-141
lines changed

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

Lines changed: 41 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -26,14 +26,15 @@
2626
import org.elasticsearch.client.RequestConverters.EndpointBuilder;
2727
import org.elasticsearch.client.ml.CloseJobRequest;
2828
import org.elasticsearch.client.ml.DeleteJobRequest;
29+
import org.elasticsearch.client.ml.FlushJobRequest;
2930
import org.elasticsearch.client.ml.GetBucketsRequest;
3031
import org.elasticsearch.client.ml.GetJobRequest;
3132
import org.elasticsearch.client.ml.GetJobStatsRequest;
33+
import org.elasticsearch.client.ml.GetOverallBucketsRequest;
3234
import org.elasticsearch.client.ml.GetRecordsRequest;
3335
import org.elasticsearch.client.ml.OpenJobRequest;
3436
import org.elasticsearch.client.ml.PutJobRequest;
3537
import org.elasticsearch.common.Strings;
36-
import org.elasticsearch.client.ml.FlushJobRequest;
3738

3839
import java.io.IOException;
3940

@@ -73,6 +74,23 @@ static Request getJob(GetJobRequest getJobRequest) {
7374
return request;
7475
}
7576

77+
static Request getJobStats(GetJobStatsRequest getJobStatsRequest) {
78+
String endpoint = new EndpointBuilder()
79+
.addPathPartAsIs("_xpack")
80+
.addPathPartAsIs("ml")
81+
.addPathPartAsIs("anomaly_detectors")
82+
.addPathPart(Strings.collectionToCommaDelimitedString(getJobStatsRequest.getJobIds()))
83+
.addPathPartAsIs("_stats")
84+
.build();
85+
Request request = new Request(HttpGet.METHOD_NAME, endpoint);
86+
87+
RequestConverters.Params params = new RequestConverters.Params(request);
88+
if (getJobStatsRequest.isAllowNoJobs() != null) {
89+
params.putParam("allow_no_jobs", Boolean.toString(getJobStatsRequest.isAllowNoJobs()));
90+
}
91+
return request;
92+
}
93+
7694
static Request openJob(OpenJobRequest openJobRequest) throws IOException {
7795
String endpoint = new EndpointBuilder()
7896
.addPathPartAsIs("_xpack")
@@ -114,6 +132,19 @@ static Request deleteJob(DeleteJobRequest deleteJobRequest) {
114132
return request;
115133
}
116134

135+
static Request flushJob(FlushJobRequest flushJobRequest) throws IOException {
136+
String endpoint = new EndpointBuilder()
137+
.addPathPartAsIs("_xpack")
138+
.addPathPartAsIs("ml")
139+
.addPathPartAsIs("anomaly_detectors")
140+
.addPathPart(flushJobRequest.getJobId())
141+
.addPathPartAsIs("_flush")
142+
.build();
143+
Request request = new Request(HttpPost.METHOD_NAME, endpoint);
144+
request.setEntity(createEntity(flushJobRequest, REQUEST_BODY_CONTENT_TYPE));
145+
return request;
146+
}
147+
117148
static Request getBuckets(GetBucketsRequest getBucketsRequest) throws IOException {
118149
String endpoint = new EndpointBuilder()
119150
.addPathPartAsIs("_xpack")
@@ -128,33 +159,17 @@ static Request getBuckets(GetBucketsRequest getBucketsRequest) throws IOExceptio
128159
return request;
129160
}
130161

131-
static Request flushJob(FlushJobRequest flushJobRequest) throws IOException {
132-
String endpoint = new EndpointBuilder()
133-
.addPathPartAsIs("_xpack")
134-
.addPathPartAsIs("ml")
135-
.addPathPartAsIs("anomaly_detectors")
136-
.addPathPart(flushJobRequest.getJobId())
137-
.addPathPartAsIs("_flush")
138-
.build();
139-
Request request = new Request(HttpPost.METHOD_NAME, endpoint);
140-
request.setEntity(createEntity(flushJobRequest, REQUEST_BODY_CONTENT_TYPE));
141-
return request;
142-
}
143-
144-
static Request getJobStats(GetJobStatsRequest getJobStatsRequest) {
162+
static Request getOverallBuckets(GetOverallBucketsRequest getOverallBucketsRequest) throws IOException {
145163
String endpoint = new EndpointBuilder()
146-
.addPathPartAsIs("_xpack")
147-
.addPathPartAsIs("ml")
148-
.addPathPartAsIs("anomaly_detectors")
149-
.addPathPart(Strings.collectionToCommaDelimitedString(getJobStatsRequest.getJobIds()))
150-
.addPathPartAsIs("_stats")
151-
.build();
164+
.addPathPartAsIs("_xpack")
165+
.addPathPartAsIs("ml")
166+
.addPathPartAsIs("anomaly_detectors")
167+
.addPathPart(Strings.collectionToCommaDelimitedString(getOverallBucketsRequest.getJobIds()))
168+
.addPathPartAsIs("results")
169+
.addPathPartAsIs("overall_buckets")
170+
.build();
152171
Request request = new Request(HttpGet.METHOD_NAME, endpoint);
153-
154-
RequestConverters.Params params = new RequestConverters.Params(request);
155-
if (getJobStatsRequest.isAllowNoJobs() != null) {
156-
params.putParam("allow_no_jobs", Boolean.toString(getJobStatsRequest.isAllowNoJobs()));
157-
}
172+
request.setEntity(createEntity(getOverallBucketsRequest, REQUEST_BODY_CONTENT_TYPE));
158173
return request;
159174
}
160175

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

Lines changed: 118 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@
3232
import org.elasticsearch.client.ml.GetBucketsResponse;
3333
import org.elasticsearch.client.ml.GetJobRequest;
3434
import org.elasticsearch.client.ml.GetJobResponse;
35+
import org.elasticsearch.client.ml.GetOverallBucketsRequest;
36+
import org.elasticsearch.client.ml.GetOverallBucketsResponse;
3537
import org.elasticsearch.client.ml.GetRecordsRequest;
3638
import org.elasticsearch.client.ml.GetRecordsResponse;
3739
import org.elasticsearch.client.ml.OpenJobRequest;
@@ -136,6 +138,47 @@ public void getJobAsync(GetJobRequest request, RequestOptions options, ActionLis
136138
Collections.emptySet());
137139
}
138140

141+
/**
142+
* Gets usage statistics for one or more Machine Learning jobs
143+
*
144+
* <p>
145+
* For additional info
146+
* see <a href="https://www.elastic.co/guide/en/elasticsearch/reference/current/ml-get-job-stats.html">Get Job stats docs</a>
147+
* </p>
148+
* @param request {@link GetJobStatsRequest} Request containing a list of jobId(s) and additional options
149+
* @param options Additional request options (e.g. headers), use {@link RequestOptions#DEFAULT} if nothing needs to be customized
150+
* @return {@link GetJobStatsResponse} response object containing
151+
* the {@link JobStats} objects and the number of jobs found
152+
* @throws IOException when there is a serialization issue sending the request or receiving the response
153+
*/
154+
public GetJobStatsResponse getJobStats(GetJobStatsRequest request, RequestOptions options) throws IOException {
155+
return restHighLevelClient.performRequestAndParseEntity(request,
156+
MLRequestConverters::getJobStats,
157+
options,
158+
GetJobStatsResponse::fromXContent,
159+
Collections.emptySet());
160+
}
161+
162+
/**
163+
* Gets one or more Machine Learning job configuration info, asynchronously.
164+
*
165+
* <p>
166+
* For additional info
167+
* see <a href="https://www.elastic.co/guide/en/elasticsearch/reference/current/ml-get-job-stats.html">Get Job stats docs</a>
168+
* </p>
169+
* @param request {@link GetJobStatsRequest} Request containing a list of jobId(s) and additional options
170+
* @param options Additional request options (e.g. headers), use {@link RequestOptions#DEFAULT} if nothing needs to be customized
171+
* @param listener Listener to be notified with {@link GetJobStatsResponse} upon request completion
172+
*/
173+
public void getJobStatsAsync(GetJobStatsRequest request, RequestOptions options, ActionListener<GetJobStatsResponse> listener) {
174+
restHighLevelClient.performRequestAsyncAndParseEntity(request,
175+
MLRequestConverters::getJobStats,
176+
options,
177+
GetJobStatsResponse::fromXContent,
178+
listener,
179+
Collections.emptySet());
180+
}
181+
139182
/**
140183
* Deletes the given Machine Learning Job
141184
* <p>
@@ -257,42 +300,6 @@ public void closeJobAsync(CloseJobRequest request, RequestOptions options, Actio
257300
Collections.emptySet());
258301
}
259302

260-
/**
261-
* Gets the buckets for a Machine Learning Job.
262-
* <p>
263-
* For additional info
264-
* see <a href="https://www.elastic.co/guide/en/elasticsearch/reference/current/ml-get-bucket.html">ML GET buckets documentation</a>
265-
*
266-
* @param request The request
267-
* @param options Additional request options (e.g. headers), use {@link RequestOptions#DEFAULT} if nothing needs to be customized
268-
*/
269-
public GetBucketsResponse getBuckets(GetBucketsRequest request, RequestOptions options) throws IOException {
270-
return restHighLevelClient.performRequestAndParseEntity(request,
271-
MLRequestConverters::getBuckets,
272-
options,
273-
GetBucketsResponse::fromXContent,
274-
Collections.emptySet());
275-
}
276-
277-
/**
278-
* Gets the buckets for a Machine Learning Job, notifies listener once the requested buckets are retrieved.
279-
* <p>
280-
* For additional info
281-
* see <a href="https://www.elastic.co/guide/en/elasticsearch/reference/current/ml-get-bucket.html">ML GET buckets documentation</a>
282-
*
283-
* @param request The request
284-
* @param options Additional request options (e.g. headers), use {@link RequestOptions#DEFAULT} if nothing needs to be customized
285-
* @param listener Listener to be notified upon request completion
286-
*/
287-
public void getBucketsAsync(GetBucketsRequest request, RequestOptions options, ActionListener<GetBucketsResponse> listener) {
288-
restHighLevelClient.performRequestAsyncAndParseEntity(request,
289-
MLRequestConverters::getBuckets,
290-
options,
291-
GetBucketsResponse::fromXContent,
292-
listener,
293-
Collections.emptySet());
294-
}
295-
296303
/**
297304
* Flushes internally buffered data for the given Machine Learning Job ensuring all data sent to the has been processed.
298305
* This may cause new results to be calculated depending on the contents of the buffer
@@ -311,13 +318,13 @@ public void getBucketsAsync(GetBucketsRequest request, RequestOptions options, A
311318
* @param request The {@link FlushJobRequest} object enclosing the `jobId` and additional request options
312319
* @param options Additional request options (e.g. headers), use {@link RequestOptions#DEFAULT} if nothing needs to be customized
313320
*/
314-
public FlushJobResponse flushJob(FlushJobRequest request, RequestOptions options) throws IOException {
321+
public FlushJobResponse flushJob(FlushJobRequest request, RequestOptions options) throws IOException {
315322
return restHighLevelClient.performRequestAndParseEntity(request,
316-
MLRequestConverters::flushJob,
317-
options,
318-
FlushJobResponse::fromXContent,
319-
Collections.emptySet());
320-
}
323+
MLRequestConverters::flushJob,
324+
options,
325+
FlushJobResponse::fromXContent,
326+
Collections.emptySet());
327+
}
321328

322329
/**
323330
* Flushes internally buffered data for the given Machine Learning Job asynchronously ensuring all data sent to the has been processed.
@@ -338,54 +345,88 @@ public FlushJobResponse flushJob(FlushJobRequest request, RequestOptions options
338345
* @param options Additional request options (e.g. headers), use {@link RequestOptions#DEFAULT} if nothing needs to be customized
339346
* @param listener Listener to be notified upon request completion
340347
*/
341-
public void flushJobAsync(FlushJobRequest request, RequestOptions options, ActionListener<FlushJobResponse> listener) {
342-
restHighLevelClient.performRequestAsyncAndParseEntity(request,
343-
MLRequestConverters::flushJob,
344-
options,
345-
FlushJobResponse::fromXContent,
346-
listener,
347-
Collections.emptySet());
348-
}
348+
public void flushJobAsync(FlushJobRequest request, RequestOptions options, ActionListener<FlushJobResponse> listener) {
349+
restHighLevelClient.performRequestAsyncAndParseEntity(request,
350+
MLRequestConverters::flushJob,
351+
options,
352+
FlushJobResponse::fromXContent,
353+
listener,
354+
Collections.emptySet());
355+
}
349356

350-
/**
351-
* Gets usage statistics for one or more Machine Learning jobs
352-
*
357+
/**
358+
* Gets the buckets for a Machine Learning Job.
353359
* <p>
354-
* For additional info
355-
* see <a href="https://www.elastic.co/guide/en/elasticsearch/reference/current/ml-get-job-stats.html">Get Job stats docs</a>
356-
* </p>
357-
* @param request {@link GetJobStatsRequest} Request containing a list of jobId(s) and additional options
360+
* For additional info
361+
* see <a href="https://www.elastic.co/guide/en/elasticsearch/reference/current/ml-get-bucket.html">ML GET buckets documentation</a>
362+
*
363+
* @param request The request
358364
* @param options Additional request options (e.g. headers), use {@link RequestOptions#DEFAULT} if nothing needs to be customized
359-
* @return {@link GetJobStatsResponse} response object containing
360-
* the {@link JobStats} objects and the number of jobs found
361-
* @throws IOException when there is a serialization issue sending the request or receiving the response
362365
*/
363-
public GetJobStatsResponse getJobStats(GetJobStatsRequest request, RequestOptions options) throws IOException {
366+
public GetBucketsResponse getBuckets(GetBucketsRequest request, RequestOptions options) throws IOException {
364367
return restHighLevelClient.performRequestAndParseEntity(request,
365-
MLRequestConverters::getJobStats,
366-
options,
367-
GetJobStatsResponse::fromXContent,
368-
Collections.emptySet());
368+
MLRequestConverters::getBuckets,
369+
options,
370+
GetBucketsResponse::fromXContent,
371+
Collections.emptySet());
369372
}
370373

371374
/**
372-
* Gets one or more Machine Learning job configuration info, asynchronously.
375+
* Gets the buckets for a Machine Learning Job, notifies listener once the requested buckets are retrieved.
376+
* <p>
377+
* For additional info
378+
* see <a href="https://www.elastic.co/guide/en/elasticsearch/reference/current/ml-get-bucket.html">ML GET buckets documentation</a>
379+
*
380+
* @param request The request
381+
* @param options Additional request options (e.g. headers), use {@link RequestOptions#DEFAULT} if nothing needs to be customized
382+
* @param listener Listener to be notified upon request completion
383+
*/
384+
public void getBucketsAsync(GetBucketsRequest request, RequestOptions options, ActionListener<GetBucketsResponse> listener) {
385+
restHighLevelClient.performRequestAsyncAndParseEntity(request,
386+
MLRequestConverters::getBuckets,
387+
options,
388+
GetBucketsResponse::fromXContent,
389+
listener,
390+
Collections.emptySet());
391+
}
392+
393+
/**
394+
* Gets overall buckets for a set of Machine Learning Jobs.
395+
* <p>
396+
* For additional info
397+
* see <a href="https://www.elastic.co/guide/en/elasticsearch/reference/current/ml-get-overall-buckets.html">
398+
* ML GET overall buckets documentation</a>
373399
*
400+
* @param request The request
401+
* @param options Additional request options (e.g. headers), use {@link RequestOptions#DEFAULT} if nothing needs to be customized
402+
*/
403+
public GetOverallBucketsResponse getOverallBuckets(GetOverallBucketsRequest request, RequestOptions options) throws IOException {
404+
return restHighLevelClient.performRequestAndParseEntity(request,
405+
MLRequestConverters::getOverallBuckets,
406+
options,
407+
GetOverallBucketsResponse::fromXContent,
408+
Collections.emptySet());
409+
}
410+
411+
/**
412+
* Gets overall buckets for a set of Machine Learning Jobs, notifies listener once the requested buckets are retrieved.
374413
* <p>
375-
* For additional info
376-
* see <a href="https://www.elastic.co/guide/en/elasticsearch/reference/current/ml-get-job-stats.html">Get Job stats docs</a>
377-
* </p>
378-
* @param request {@link GetJobStatsRequest} Request containing a list of jobId(s) and additional options
414+
* For additional info
415+
* see <a href="https://www.elastic.co/guide/en/elasticsearch/reference/current/ml-get-overall-buckets.html">
416+
* ML GET overall buckets documentation</a>
417+
*
418+
* @param request The request
379419
* @param options Additional request options (e.g. headers), use {@link RequestOptions#DEFAULT} if nothing needs to be customized
380-
* @param listener Listener to be notified with {@link GetJobStatsResponse} upon request completion
420+
* @param listener Listener to be notified upon request completion
381421
*/
382-
public void getJobStatsAsync(GetJobStatsRequest request, RequestOptions options, ActionListener<GetJobStatsResponse> listener) {
422+
public void getOverallBucketsAsync(GetOverallBucketsRequest request, RequestOptions options,
423+
ActionListener<GetOverallBucketsResponse> listener) {
383424
restHighLevelClient.performRequestAsyncAndParseEntity(request,
384-
MLRequestConverters::getJobStats,
385-
options,
386-
GetJobStatsResponse::fromXContent,
387-
listener,
388-
Collections.emptySet());
425+
MLRequestConverters::getOverallBuckets,
426+
options,
427+
GetOverallBucketsResponse::fromXContent,
428+
listener,
429+
Collections.emptySet());
389430
}
390431

391432
/**

0 commit comments

Comments
 (0)