Skip to content

Commit aff5658

Browse files
HLRC: Add ML get influencers API (#33389)
Relates #29827
1 parent 71c0ee5 commit aff5658

18 files changed

+864
-50
lines changed

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

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
import org.elasticsearch.client.ml.DeleteJobRequest;
2929
import org.elasticsearch.client.ml.FlushJobRequest;
3030
import org.elasticsearch.client.ml.GetBucketsRequest;
31+
import org.elasticsearch.client.ml.GetInfluencersRequest;
3132
import org.elasticsearch.client.ml.GetJobRequest;
3233
import org.elasticsearch.client.ml.GetJobStatsRequest;
3334
import org.elasticsearch.client.ml.GetOverallBucketsRequest;
@@ -186,4 +187,18 @@ static Request getRecords(GetRecordsRequest getRecordsRequest) throws IOExceptio
186187
request.setEntity(createEntity(getRecordsRequest, REQUEST_BODY_CONTENT_TYPE));
187188
return request;
188189
}
190+
191+
static Request getInfluencers(GetInfluencersRequest getInfluencersRequest) throws IOException {
192+
String endpoint = new EndpointBuilder()
193+
.addPathPartAsIs("_xpack")
194+
.addPathPartAsIs("ml")
195+
.addPathPartAsIs("anomaly_detectors")
196+
.addPathPart(getInfluencersRequest.getJobId())
197+
.addPathPartAsIs("results")
198+
.addPathPartAsIs("influencers")
199+
.build();
200+
Request request = new Request(HttpGet.METHOD_NAME, endpoint);
201+
request.setEntity(createEntity(getInfluencersRequest, REQUEST_BODY_CONTENT_TYPE));
202+
return request;
203+
}
189204
}

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

Lines changed: 46 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,19 +19,20 @@
1919
package org.elasticsearch.client;
2020

2121
import org.elasticsearch.action.ActionListener;
22-
import org.elasticsearch.client.ml.FlushJobRequest;
23-
import org.elasticsearch.client.ml.FlushJobResponse;
24-
import org.elasticsearch.client.ml.GetJobStatsRequest;
25-
import org.elasticsearch.client.ml.GetJobStatsResponse;
26-
import org.elasticsearch.client.ml.job.stats.JobStats;
2722
import org.elasticsearch.client.ml.CloseJobRequest;
2823
import org.elasticsearch.client.ml.CloseJobResponse;
2924
import org.elasticsearch.client.ml.DeleteJobRequest;
3025
import org.elasticsearch.client.ml.DeleteJobResponse;
26+
import org.elasticsearch.client.ml.FlushJobRequest;
27+
import org.elasticsearch.client.ml.FlushJobResponse;
3128
import org.elasticsearch.client.ml.GetBucketsRequest;
3229
import org.elasticsearch.client.ml.GetBucketsResponse;
30+
import org.elasticsearch.client.ml.GetInfluencersRequest;
31+
import org.elasticsearch.client.ml.GetInfluencersResponse;
3332
import org.elasticsearch.client.ml.GetJobRequest;
3433
import org.elasticsearch.client.ml.GetJobResponse;
34+
import org.elasticsearch.client.ml.GetJobStatsRequest;
35+
import org.elasticsearch.client.ml.GetJobStatsResponse;
3536
import org.elasticsearch.client.ml.GetOverallBucketsRequest;
3637
import org.elasticsearch.client.ml.GetOverallBucketsResponse;
3738
import org.elasticsearch.client.ml.GetRecordsRequest;
@@ -40,6 +41,7 @@
4041
import org.elasticsearch.client.ml.OpenJobResponse;
4142
import org.elasticsearch.client.ml.PutJobRequest;
4243
import org.elasticsearch.client.ml.PutJobResponse;
44+
import org.elasticsearch.client.ml.job.stats.JobStats;
4345

4446
import java.io.IOException;
4547
import java.util.Collections;
@@ -464,4 +466,43 @@ public void getRecordsAsync(GetRecordsRequest request, RequestOptions options, A
464466
listener,
465467
Collections.emptySet());
466468
}
469+
470+
/**
471+
* Gets the influencers for a Machine Learning Job.
472+
* <p>
473+
* For additional info
474+
* see <a href="https://www.elastic.co/guide/en/elasticsearch/reference/current/ml-get-influencer.html">
475+
* ML GET influencers documentation</a>
476+
*
477+
* @param request the request
478+
* @param options Additional request options (e.g. headers), use {@link RequestOptions#DEFAULT} if nothing needs to be customized
479+
*/
480+
public GetInfluencersResponse getInfluencers(GetInfluencersRequest request, RequestOptions options) throws IOException {
481+
return restHighLevelClient.performRequestAndParseEntity(request,
482+
MLRequestConverters::getInfluencers,
483+
options,
484+
GetInfluencersResponse::fromXContent,
485+
Collections.emptySet());
486+
}
487+
488+
/**
489+
* Gets the influencers for a Machine Learning Job, notifies listener once the requested influencers are retrieved.
490+
* <p>
491+
* For additional info
492+
* * see <a href="https://www.elastic.co/guide/en/elasticsearch/reference/current/ml-get-influencer.html">
493+
* ML GET influencers documentation</a>
494+
*
495+
* @param request the request
496+
* @param options Additional request options (e.g. headers), use {@link RequestOptions#DEFAULT} if nothing needs to be customized
497+
* @param listener Listener to be notified upon request completion
498+
*/
499+
public void getInfluencersAsync(GetInfluencersRequest request, RequestOptions options,
500+
ActionListener<GetInfluencersResponse> listener) {
501+
restHighLevelClient.performRequestAsyncAndParseEntity(request,
502+
MLRequestConverters::getInfluencers,
503+
options,
504+
GetInfluencersResponse::fromXContent,
505+
listener,
506+
Collections.emptySet());
507+
}
467508
}

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

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,6 @@ public class GetBucketsRequest extends ActionRequest implements ToXContentObject
4141
public static final ParseField START = new ParseField("start");
4242
public static final ParseField END = new ParseField("end");
4343
public static final ParseField ANOMALY_SCORE = new ParseField("anomaly_score");
44-
public static final ParseField TIMESTAMP = new ParseField("timestamp");
4544
public static final ParseField SORT = new ParseField("sort");
4645
public static final ParseField DESCENDING = new ParseField("desc");
4746

@@ -87,7 +86,7 @@ public String getJobId() {
8786

8887
/**
8988
* Sets the timestamp of a specific bucket to be retrieved.
90-
* @param timestamp the timestamp of a specific bucket to be retrieved
89+
* @param timestamp String representation of a timestamp; may be an epoch seconds, epoch millis or an ISO string
9190
*/
9291
public void setTimestamp(String timestamp) {
9392
this.timestamp = timestamp;
@@ -106,11 +105,11 @@ public boolean isExpand() {
106105
* When {@code true}, buckets will be expanded to include their records.
107106
* @param expand value of "expand" to be set
108107
*/
109-
public void setExpand(boolean expand) {
108+
public void setExpand(Boolean expand) {
110109
this.expand = expand;
111110
}
112111

113-
public boolean isExcludeInterim() {
112+
public Boolean isExcludeInterim() {
114113
return excludeInterim;
115114
}
116115

@@ -119,7 +118,7 @@ public boolean isExcludeInterim() {
119118
* When {@code true}, interim buckets will be filtered out.
120119
* @param excludeInterim value of "exclude_interim" to be set
121120
*/
122-
public void setExcludeInterim(boolean excludeInterim) {
121+
public void setExcludeInterim(Boolean excludeInterim) {
123122
this.excludeInterim = excludeInterim;
124123
}
125124

@@ -130,7 +129,7 @@ public String getStart() {
130129
/**
131130
* Sets the value of "start" which is a timestamp.
132131
* Only buckets whose timestamp is on or after the "start" value will be returned.
133-
* @param start value of "start" to be set
132+
* @param start String representation of a timestamp; may be an epoch seconds, epoch millis or an ISO string
134133
*/
135134
public void setStart(String start) {
136135
this.start = start;
@@ -143,7 +142,7 @@ public String getEnd() {
143142
/**
144143
* Sets the value of "end" which is a timestamp.
145144
* Only buckets whose timestamp is before the "end" value will be returned.
146-
* @param end value of "end" to be set
145+
* @param end String representation of a timestamp; may be an epoch seconds, epoch millis or an ISO string
147146
*/
148147
public void setEnd(String end) {
149148
this.end = end;
@@ -170,7 +169,7 @@ public Double getAnomalyScore() {
170169
* Only buckets with "anomaly_score" equal or greater will be returned.
171170
* @param anomalyScore value of "anomaly_score".
172171
*/
173-
public void setAnomalyScore(double anomalyScore) {
172+
public void setAnomalyScore(Double anomalyScore) {
174173
this.anomalyScore = anomalyScore;
175174
}
176175

@@ -187,7 +186,7 @@ public void setSort(String sort) {
187186
this.sort = sort;
188187
}
189188

190-
public boolean isDescending() {
189+
public Boolean isDescending() {
191190
return descending;
192191
}
193192

0 commit comments

Comments
 (0)