Skip to content

Commit 9b6bbc0

Browse files
authored
HLRC: ML Update Job (#33392)
* HLRC: ML Update Job
1 parent ef207ed commit 9b6bbc0

File tree

11 files changed

+974
-0
lines changed

11 files changed

+974
-0
lines changed

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

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
import org.elasticsearch.client.ml.GetRecordsRequest;
3636
import org.elasticsearch.client.ml.OpenJobRequest;
3737
import org.elasticsearch.client.ml.PutJobRequest;
38+
import org.elasticsearch.client.ml.UpdateJobRequest;
3839
import org.elasticsearch.common.Strings;
3940

4041
import java.io.IOException;
@@ -146,6 +147,19 @@ static Request flushJob(FlushJobRequest flushJobRequest) throws IOException {
146147
return request;
147148
}
148149

150+
static Request updateJob(UpdateJobRequest updateJobRequest) throws IOException {
151+
String endpoint = new EndpointBuilder()
152+
.addPathPartAsIs("_xpack")
153+
.addPathPartAsIs("ml")
154+
.addPathPartAsIs("anomaly_detectors")
155+
.addPathPart(updateJobRequest.getJobUpdate().getJobId())
156+
.addPathPartAsIs("_update")
157+
.build();
158+
Request request = new Request(HttpPost.METHOD_NAME, endpoint);
159+
request.setEntity(createEntity(updateJobRequest.getJobUpdate(), REQUEST_BODY_CONTENT_TYPE));
160+
return request;
161+
}
162+
149163
static Request getBuckets(GetBucketsRequest getBucketsRequest) throws IOException {
150164
String endpoint = new EndpointBuilder()
151165
.addPathPartAsIs("_xpack")

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

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
package org.elasticsearch.client;
2020

2121
import org.elasticsearch.action.ActionListener;
22+
import org.elasticsearch.client.ml.UpdateJobRequest;
2223
import org.elasticsearch.client.ml.CloseJobRequest;
2324
import org.elasticsearch.client.ml.CloseJobResponse;
2425
import org.elasticsearch.client.ml.DeleteJobRequest;
@@ -319,6 +320,7 @@ public void closeJobAsync(CloseJobRequest request, RequestOptions options, Actio
319320
*
320321
* @param request The {@link FlushJobRequest} object enclosing the `jobId` and additional request options
321322
* @param options Additional request options (e.g. headers), use {@link RequestOptions#DEFAULT} if nothing needs to be customized
323+
* @throws IOException when there is a serialization issue sending the request or receiving the response
322324
*/
323325
public FlushJobResponse flushJob(FlushJobRequest request, RequestOptions options) throws IOException {
324326
return restHighLevelClient.performRequestAndParseEntity(request,
@@ -356,6 +358,38 @@ public void flushJobAsync(FlushJobRequest request, RequestOptions options, Actio
356358
Collections.emptySet());
357359
}
358360

361+
/**
362+
* Updates a Machine Learning {@link org.elasticsearch.client.ml.job.config.Job}
363+
*
364+
* @param request the {@link UpdateJobRequest} object enclosing the desired updates
365+
* @param options Additional request options (e.g. headers), use {@link RequestOptions#DEFAULT} if nothing needs to be customized
366+
* @return a PutJobResponse object containing the updated job object
367+
* @throws IOException when there is a serialization issue sending the request or receiving the response
368+
*/
369+
public PutJobResponse updateJob(UpdateJobRequest request, RequestOptions options) throws IOException {
370+
return restHighLevelClient.performRequestAndParseEntity(request,
371+
MLRequestConverters::updateJob,
372+
options,
373+
PutJobResponse::fromXContent,
374+
Collections.emptySet());
375+
}
376+
377+
/**
378+
* Updates a Machine Learning {@link org.elasticsearch.client.ml.job.config.Job} asynchronously
379+
*
380+
* @param request the {@link UpdateJobRequest} object enclosing the desired updates
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 updateJobAsync(UpdateJobRequest request, RequestOptions options, ActionListener<PutJobResponse> listener) {
385+
restHighLevelClient.performRequestAsyncAndParseEntity(request,
386+
MLRequestConverters::updateJob,
387+
options,
388+
PutJobResponse::fromXContent,
389+
listener,
390+
Collections.emptySet());
391+
}
392+
359393
/**
360394
* Gets the buckets for a Machine Learning Job.
361395
* <p>
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
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.action.ActionRequest;
22+
import org.elasticsearch.action.ActionRequestValidationException;
23+
import org.elasticsearch.client.ml.job.config.JobUpdate;
24+
import org.elasticsearch.common.Strings;
25+
import org.elasticsearch.common.xcontent.ToXContentObject;
26+
import org.elasticsearch.common.xcontent.XContentBuilder;
27+
28+
import java.io.IOException;
29+
import java.util.Objects;
30+
31+
/**
32+
* Updates a {@link org.elasticsearch.client.ml.job.config.Job} with the passed {@link JobUpdate}
33+
* settings
34+
*/
35+
public class UpdateJobRequest extends ActionRequest implements ToXContentObject {
36+
37+
private final JobUpdate update;
38+
39+
public UpdateJobRequest(JobUpdate update) {
40+
this.update = update;
41+
}
42+
43+
public JobUpdate getJobUpdate() {
44+
return update;
45+
}
46+
47+
@Override
48+
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
49+
return update.toXContent(builder, params);
50+
}
51+
52+
@Override
53+
public boolean equals(Object o) {
54+
if (this == o) {
55+
return true;
56+
}
57+
58+
if (o == null || getClass() != o.getClass()) {
59+
return false;
60+
}
61+
62+
UpdateJobRequest that = (UpdateJobRequest) o;
63+
return Objects.equals(update, that.update);
64+
}
65+
66+
@Override
67+
public int hashCode() {
68+
return Objects.hash(update);
69+
}
70+
71+
@Override
72+
public final String toString() {
73+
return Strings.toString(this);
74+
}
75+
76+
@Override
77+
public ActionRequestValidationException validate() {
78+
return null;
79+
}
80+
}

0 commit comments

Comments
 (0)