Skip to content

Commit a5b34c7

Browse files
HLRC: Add ML Get Records API (#33085)
Relates #29827
1 parent 22415fa commit a5b34c7

File tree

12 files changed

+784
-15
lines changed

12 files changed

+784
-15
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.GetBucketsRequest;
3030
import org.elasticsearch.client.ml.GetJobRequest;
31+
import org.elasticsearch.client.ml.GetRecordsRequest;
3132
import org.elasticsearch.client.ml.OpenJobRequest;
3233
import org.elasticsearch.client.ml.PutJobRequest;
3334
import org.elasticsearch.common.Strings;
@@ -124,4 +125,18 @@ static Request getBuckets(GetBucketsRequest getBucketsRequest) throws IOExceptio
124125
request.setEntity(createEntity(getBucketsRequest, REQUEST_BODY_CONTENT_TYPE));
125126
return request;
126127
}
128+
129+
static Request getRecords(GetRecordsRequest getRecordsRequest) throws IOException {
130+
String endpoint = new EndpointBuilder()
131+
.addPathPartAsIs("_xpack")
132+
.addPathPartAsIs("ml")
133+
.addPathPartAsIs("anomaly_detectors")
134+
.addPathPart(getRecordsRequest.getJobId())
135+
.addPathPartAsIs("results")
136+
.addPathPartAsIs("records")
137+
.build();
138+
Request request = new Request(HttpGet.METHOD_NAME, endpoint);
139+
request.setEntity(createEntity(getRecordsRequest, REQUEST_BODY_CONTENT_TYPE));
140+
return request;
141+
}
127142
}

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

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@
2727
import org.elasticsearch.client.ml.GetBucketsResponse;
2828
import org.elasticsearch.client.ml.GetJobRequest;
2929
import org.elasticsearch.client.ml.GetJobResponse;
30+
import org.elasticsearch.client.ml.GetRecordsRequest;
31+
import org.elasticsearch.client.ml.GetRecordsResponse;
3032
import org.elasticsearch.client.ml.OpenJobRequest;
3133
import org.elasticsearch.client.ml.OpenJobResponse;
3234
import org.elasticsearch.client.ml.PutJobRequest;
@@ -285,4 +287,40 @@ public void getBucketsAsync(GetBucketsRequest request, RequestOptions options, A
285287
listener,
286288
Collections.emptySet());
287289
}
290+
291+
/**
292+
* Gets the records for a Machine Learning Job.
293+
* <p>
294+
* For additional info
295+
* see <a href="https://www.elastic.co/guide/en/elasticsearch/reference/current/ml-get-record.html">ML GET records documentation</a>
296+
*
297+
* @param request the request
298+
* @param options Additional request options (e.g. headers), use {@link RequestOptions#DEFAULT} if nothing needs to be customized
299+
*/
300+
public GetRecordsResponse getRecords(GetRecordsRequest request, RequestOptions options) throws IOException {
301+
return restHighLevelClient.performRequestAndParseEntity(request,
302+
MLRequestConverters::getRecords,
303+
options,
304+
GetRecordsResponse::fromXContent,
305+
Collections.emptySet());
306+
}
307+
308+
/**
309+
* Gets the records for a Machine Learning Job, notifies listener once the requested records are retrieved.
310+
* <p>
311+
* For additional info
312+
* see <a href="https://www.elastic.co/guide/en/elasticsearch/reference/current/ml-get-record.html">ML GET records documentation</a>
313+
*
314+
* @param request the request
315+
* @param options Additional request options (e.g. headers), use {@link RequestOptions#DEFAULT} if nothing needs to be customized
316+
* @param listener Listener to be notified upon request completion
317+
*/
318+
public void getRecordsAsync(GetRecordsRequest request, RequestOptions options, ActionListener<GetRecordsResponse> listener) {
319+
restHighLevelClient.performRequestAsyncAndParseEntity(request,
320+
MLRequestConverters::getRecords,
321+
options,
322+
GetRecordsResponse::fromXContent,
323+
listener,
324+
Collections.emptySet());
325+
}
288326
}
Lines changed: 222 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,222 @@
1+
/*
2+
* Licensed to Elasticsearch under one or more contributor
3+
* license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright
5+
* ownership. Elasticsearch licenses this file to you under
6+
* the Apache License, Version 2.0 (the "License"); you may
7+
* not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
package org.elasticsearch.client.ml;
20+
21+
import org.elasticsearch.client.Validatable;
22+
import org.elasticsearch.client.ml.job.config.Job;
23+
import org.elasticsearch.client.ml.job.util.PageParams;
24+
import org.elasticsearch.common.ParseField;
25+
import org.elasticsearch.common.xcontent.ObjectParser;
26+
import org.elasticsearch.common.xcontent.ToXContentObject;
27+
import org.elasticsearch.common.xcontent.XContentBuilder;
28+
29+
import java.io.IOException;
30+
import java.util.Objects;
31+
32+
/**
33+
* A request to retrieve records of a given job
34+
*/
35+
public class GetRecordsRequest implements ToXContentObject, Validatable {
36+
37+
public static final ParseField EXCLUDE_INTERIM = new ParseField("exclude_interim");
38+
public static final ParseField START = new ParseField("start");
39+
public static final ParseField END = new ParseField("end");
40+
public static final ParseField RECORD_SCORE = new ParseField("record_score");
41+
public static final ParseField SORT = new ParseField("sort");
42+
public static final ParseField DESCENDING = new ParseField("desc");
43+
44+
public static final ObjectParser<GetRecordsRequest, Void> PARSER = new ObjectParser<>("get_buckets_request", GetRecordsRequest::new);
45+
46+
static {
47+
PARSER.declareString((request, jobId) -> request.jobId = jobId, Job.ID);
48+
PARSER.declareBoolean(GetRecordsRequest::setExcludeInterim, EXCLUDE_INTERIM);
49+
PARSER.declareStringOrNull(GetRecordsRequest::setStart, START);
50+
PARSER.declareStringOrNull(GetRecordsRequest::setEnd, END);
51+
PARSER.declareObject(GetRecordsRequest::setPageParams, PageParams.PARSER, PageParams.PAGE);
52+
PARSER.declareDouble(GetRecordsRequest::setRecordScore, RECORD_SCORE);
53+
PARSER.declareString(GetRecordsRequest::setSort, SORT);
54+
PARSER.declareBoolean(GetRecordsRequest::setDescending, DESCENDING);
55+
}
56+
57+
private String jobId;
58+
private Boolean excludeInterim;
59+
private String start;
60+
private String end;
61+
private PageParams pageParams;
62+
private Double recordScore;
63+
private String sort;
64+
private Boolean descending;
65+
66+
private GetRecordsRequest() {}
67+
68+
/**
69+
* Constructs a request to retrieve records of a given job
70+
* @param jobId id of the job to retrieve records of
71+
*/
72+
public GetRecordsRequest(String jobId) {
73+
this.jobId = Objects.requireNonNull(jobId);
74+
}
75+
76+
public String getJobId() {
77+
return jobId;
78+
}
79+
80+
public boolean isExcludeInterim() {
81+
return excludeInterim;
82+
}
83+
84+
/**
85+
* Sets the value of "exclude_interim".
86+
* When {@code true}, interim records will be filtered out.
87+
* @param excludeInterim value of "exclude_interim" to be set
88+
*/
89+
public void setExcludeInterim(boolean excludeInterim) {
90+
this.excludeInterim = excludeInterim;
91+
}
92+
93+
public String getStart() {
94+
return start;
95+
}
96+
97+
/**
98+
* Sets the value of "start" which is a timestamp.
99+
* Only records whose timestamp is on or after the "start" value will be returned.
100+
* @param start value of "start" to be set
101+
*/
102+
public void setStart(String start) {
103+
this.start = start;
104+
}
105+
106+
public String getEnd() {
107+
return end;
108+
}
109+
110+
/**
111+
* Sets the value of "end" which is a timestamp.
112+
* Only records whose timestamp is before the "end" value will be returned.
113+
* @param end value of "end" to be set
114+
*/
115+
public void setEnd(String end) {
116+
this.end = end;
117+
}
118+
119+
public PageParams getPageParams() {
120+
return pageParams;
121+
}
122+
123+
/**
124+
* Sets the paging parameters
125+
* @param pageParams The paging parameters
126+
*/
127+
public void setPageParams(PageParams pageParams) {
128+
this.pageParams = pageParams;
129+
}
130+
131+
public Double getRecordScore() {
132+
return recordScore;
133+
}
134+
135+
/**
136+
* Sets the value of "record_score".
137+
* Only records with "record_score" equal or greater will be returned.
138+
* @param recordScore value of "record_score".
139+
*/
140+
public void setRecordScore(double recordScore) {
141+
this.recordScore = recordScore;
142+
}
143+
144+
public String getSort() {
145+
return sort;
146+
}
147+
148+
/**
149+
* Sets the value of "sort".
150+
* Specifies the bucket field to sort on.
151+
* @param sort value of "sort".
152+
*/
153+
public void setSort(String sort) {
154+
this.sort = sort;
155+
}
156+
157+
public boolean isDescending() {
158+
return descending;
159+
}
160+
161+
/**
162+
* Sets the value of "desc".
163+
* Specifies the sorting order.
164+
* @param descending value of "desc"
165+
*/
166+
public void setDescending(boolean descending) {
167+
this.descending = descending;
168+
}
169+
170+
@Override
171+
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
172+
builder.startObject();
173+
builder.field(Job.ID.getPreferredName(), jobId);
174+
if (excludeInterim != null) {
175+
builder.field(EXCLUDE_INTERIM.getPreferredName(), excludeInterim);
176+
}
177+
if (start != null) {
178+
builder.field(START.getPreferredName(), start);
179+
}
180+
if (end != null) {
181+
builder.field(END.getPreferredName(), end);
182+
}
183+
if (pageParams != null) {
184+
builder.field(PageParams.PAGE.getPreferredName(), pageParams);
185+
}
186+
if (recordScore != null) {
187+
builder.field(RECORD_SCORE.getPreferredName(), recordScore);
188+
}
189+
if (sort != null) {
190+
builder.field(SORT.getPreferredName(), sort);
191+
}
192+
if (descending != null) {
193+
builder.field(DESCENDING.getPreferredName(), descending);
194+
}
195+
builder.endObject();
196+
return builder;
197+
}
198+
199+
@Override
200+
public int hashCode() {
201+
return Objects.hash(jobId, excludeInterim, recordScore, pageParams, start, end, sort, descending);
202+
}
203+
204+
@Override
205+
public boolean equals(Object obj) {
206+
if (obj == null) {
207+
return false;
208+
}
209+
if (getClass() != obj.getClass()) {
210+
return false;
211+
}
212+
GetRecordsRequest other = (GetRecordsRequest) obj;
213+
return Objects.equals(jobId, other.jobId) &&
214+
Objects.equals(excludeInterim, other.excludeInterim) &&
215+
Objects.equals(recordScore, other.recordScore) &&
216+
Objects.equals(pageParams, other.pageParams) &&
217+
Objects.equals(start, other.start) &&
218+
Objects.equals(end, other.end) &&
219+
Objects.equals(sort, other.sort) &&
220+
Objects.equals(descending, other.descending);
221+
}
222+
}
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
/*
2+
* Licensed to Elasticsearch under one or more contributor
3+
* license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright
5+
* ownership. Elasticsearch licenses this file to you under
6+
* the Apache License, Version 2.0 (the "License"); you may
7+
* not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
package org.elasticsearch.client.ml;
20+
21+
import org.elasticsearch.client.ml.job.results.AnomalyRecord;
22+
import org.elasticsearch.common.ParseField;
23+
import org.elasticsearch.common.xcontent.ConstructingObjectParser;
24+
import org.elasticsearch.common.xcontent.XContentParser;
25+
26+
import java.io.IOException;
27+
import java.util.List;
28+
import java.util.Objects;
29+
30+
/**
31+
* A response containing the requested buckets
32+
*/
33+
public class GetRecordsResponse extends AbstractResultResponse<AnomalyRecord> {
34+
35+
public static final ParseField RECORDS = new ParseField("records");
36+
37+
@SuppressWarnings("unchecked")
38+
public static final ConstructingObjectParser<GetRecordsResponse, Void> PARSER = new ConstructingObjectParser<>("get_records_response",
39+
true, a -> new GetRecordsResponse((List<AnomalyRecord>) a[0], (long) a[1]));
40+
41+
static {
42+
PARSER.declareObjectArray(ConstructingObjectParser.constructorArg(), AnomalyRecord.PARSER, RECORDS);
43+
PARSER.declareLong(ConstructingObjectParser.constructorArg(), COUNT);
44+
}
45+
46+
public static GetRecordsResponse fromXContent(XContentParser parser) throws IOException {
47+
return PARSER.parse(parser, null);
48+
}
49+
50+
GetRecordsResponse(List<AnomalyRecord> buckets, long count) {
51+
super(RECORDS, buckets, count);
52+
}
53+
54+
/**
55+
* The retrieved records
56+
* @return the retrieved records
57+
*/
58+
public List<AnomalyRecord> records() {
59+
return results;
60+
}
61+
62+
@Override
63+
public int hashCode() {
64+
return Objects.hash(count, results);
65+
}
66+
67+
@Override
68+
public boolean equals(Object obj) {
69+
if (obj == null) {
70+
return false;
71+
}
72+
if (getClass() != obj.getClass()) {
73+
return false;
74+
}
75+
GetRecordsResponse other = (GetRecordsResponse) obj;
76+
return count == other.count && Objects.equals(results, other.results);
77+
}
78+
}

0 commit comments

Comments
 (0)