Skip to content

Commit bd61738

Browse files
committed
Merge branch 'master' of github.com:elastic/elasticsearch into global-checkpoint-poll
* 'master' of github.com:elastic/elasticsearch: Fix docs for fixed filename for heap dump path (elastic#32882) Painless: Special Case def (elastic#32871) AwaitFix FullClusterRestartIT#testRollupIDSchemeAfterRestart. [Test] Fix DuelScrollIT#testDuelIndexOrderQueryThenFetch HLRC: adding machine learning delete job (elastic#32820) [DOCS] Update WordPress plugins links (elastic#32194) Remove passphrase support from reload settings API (elastic#32889) AwaitFix AckIT. Mutes test in DuelScrollIT CharArraysTests: Fix test bug. [ML] Choose seconds to fix intermittent DatafeeedConfigTest failure Test: Fix unpredictive merges in DocumentSubsetReaderTests [DOCS] Clarify sentence in network-host.asciidoc (elastic#32429) Docs enhancement: added reference to cluster-level setting `search.default_allow_partial_results` (elastic#32810) [DOCS] Fixing cross doc link to Stack Overview security topic. Move CharArrays to core lib (elastic#32851) Fix global checkpoint listeners test HLRC: adding machine learning open job (elastic#32860) [ML] Add log structure finder functionality (elastic#32788) INGEST: Add Configuration Except. Data to Metdata (elastic#32322)
2 parents 80526b2 + b5a8536 commit bd61738

File tree

112 files changed

+6999
-470
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

112 files changed

+6999
-470
lines changed

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

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

2121
import org.elasticsearch.action.ActionListener;
22+
import org.elasticsearch.protocol.xpack.ml.DeleteJobRequest;
23+
import org.elasticsearch.protocol.xpack.ml.DeleteJobResponse;
24+
import org.elasticsearch.protocol.xpack.ml.OpenJobRequest;
25+
import org.elasticsearch.protocol.xpack.ml.OpenJobResponse;
2226
import org.elasticsearch.protocol.xpack.ml.PutJobRequest;
2327
import org.elasticsearch.protocol.xpack.ml.PutJobResponse;
2428

@@ -77,4 +81,89 @@ public void putJobAsync(PutJobRequest request, RequestOptions options, ActionLis
7781
listener,
7882
Collections.emptySet());
7983
}
84+
85+
/**
86+
* Deletes the given Machine Learning Job
87+
* <p>
88+
* For additional info
89+
* see <a href="http://www.elastic.co/guide/en/elasticsearch/reference/current/ml-delete-job.html">ML Delete Job documentation</a>
90+
* </p>
91+
* @param request the request to delete the job
92+
* @param options Additional request options (e.g. headers), use {@link RequestOptions#DEFAULT} if nothing needs to be customized
93+
* @return action acknowledgement
94+
* @throws IOException when there is a serialization issue sending the request or receiving the response
95+
*/
96+
public DeleteJobResponse deleteJob(DeleteJobRequest request, RequestOptions options) throws IOException {
97+
return restHighLevelClient.performRequestAndParseEntity(request,
98+
RequestConverters::deleteMachineLearningJob,
99+
options,
100+
DeleteJobResponse::fromXContent,
101+
Collections.emptySet());
102+
}
103+
104+
/**
105+
* Deletes the given Machine Learning Job asynchronously and notifies the listener on completion
106+
* <p>
107+
* For additional info
108+
* see <a href="http://www.elastic.co/guide/en/elasticsearch/reference/current/ml-delete-job.html">ML Delete Job documentation</a>
109+
* </p>
110+
* @param request the request to delete the job
111+
* @param options Additional request options (e.g. headers), use {@link RequestOptions#DEFAULT} if nothing needs to be customized
112+
* @param listener Listener to be notified upon request completion
113+
*/
114+
public void deleteJobAsync(DeleteJobRequest request, RequestOptions options, ActionListener<DeleteJobResponse> listener) {
115+
restHighLevelClient.performRequestAsyncAndParseEntity(request,
116+
RequestConverters::deleteMachineLearningJob,
117+
options,
118+
DeleteJobResponse::fromXContent,
119+
listener,
120+
Collections.emptySet());
121+
}
122+
123+
/**
124+
* Opens a Machine Learning Job.
125+
* When you open a new job, it starts with an empty model.
126+
*
127+
* When you open an existing job, the most recent model state is automatically loaded.
128+
* The job is ready to resume its analysis from where it left off, once new data is received.
129+
*
130+
* <p>
131+
* For additional info
132+
* see <a href="https://www.elastic.co/guide/en/elasticsearch/reference/current/ml-open-job.html"></a>
133+
* </p>
134+
* @param request request containing job_id and additional optional options
135+
* @param options Additional request options (e.g. headers), use {@link RequestOptions#DEFAULT} if nothing needs to be customized
136+
* @return response containing if the job was successfully opened or not.
137+
* @throws IOException when there is a serialization issue sending the request or receiving the response
138+
*/
139+
public OpenJobResponse openJob(OpenJobRequest request, RequestOptions options) throws IOException {
140+
return restHighLevelClient.performRequestAndParseEntity(request,
141+
RequestConverters::machineLearningOpenJob,
142+
options,
143+
OpenJobResponse::fromXContent,
144+
Collections.emptySet());
145+
}
146+
147+
/**
148+
* Opens a Machine Learning Job asynchronously, notifies listener on completion.
149+
* When you open a new job, it starts with an empty model.
150+
*
151+
* When you open an existing job, the most recent model state is automatically loaded.
152+
* The job is ready to resume its analysis from where it left off, once new data is received.
153+
* <p>
154+
* For additional info
155+
* see <a href="https://www.elastic.co/guide/en/elasticsearch/reference/current/ml-open-job.html"></a>
156+
* </p>
157+
* @param request request containing job_id and additional optional options
158+
* @param options Additional request options (e.g. headers), use {@link RequestOptions#DEFAULT} if nothing needs to be customized
159+
* @param listener Listener to be notified upon request completion
160+
*/
161+
public void openJobAsync(OpenJobRequest request, RequestOptions options, ActionListener<OpenJobResponse> listener) {
162+
restHighLevelClient.performRequestAsyncAndParseEntity(request,
163+
RequestConverters::machineLearningOpenJob,
164+
options,
165+
OpenJobResponse::fromXContent,
166+
listener,
167+
Collections.emptySet());
168+
}
80169
}

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

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,8 @@
112112
import org.elasticsearch.protocol.xpack.license.GetLicenseRequest;
113113
import org.elasticsearch.protocol.xpack.license.PutLicenseRequest;
114114
import org.elasticsearch.protocol.xpack.migration.IndexUpgradeInfoRequest;
115+
import org.elasticsearch.protocol.xpack.ml.DeleteJobRequest;
116+
import org.elasticsearch.protocol.xpack.ml.OpenJobRequest;
115117
import org.elasticsearch.protocol.xpack.ml.PutJobRequest;
116118
import org.elasticsearch.protocol.xpack.watcher.DeleteWatchRequest;
117119
import org.elasticsearch.protocol.xpack.watcher.PutWatchRequest;
@@ -1210,6 +1212,34 @@ static Request putMachineLearningJob(PutJobRequest putJobRequest) throws IOExcep
12101212
return request;
12111213
}
12121214

1215+
static Request deleteMachineLearningJob(DeleteJobRequest deleteJobRequest) {
1216+
String endpoint = new EndpointBuilder()
1217+
.addPathPartAsIs("_xpack")
1218+
.addPathPartAsIs("ml")
1219+
.addPathPartAsIs("anomaly_detectors")
1220+
.addPathPart(deleteJobRequest.getJobId())
1221+
.build();
1222+
Request request = new Request(HttpDelete.METHOD_NAME, endpoint);
1223+
1224+
Params params = new Params(request);
1225+
params.putParam("force", Boolean.toString(deleteJobRequest.isForce()));
1226+
1227+
return request;
1228+
}
1229+
1230+
static Request machineLearningOpenJob(OpenJobRequest openJobRequest) throws IOException {
1231+
String endpoint = new EndpointBuilder()
1232+
.addPathPartAsIs("_xpack")
1233+
.addPathPartAsIs("ml")
1234+
.addPathPartAsIs("anomaly_detectors")
1235+
.addPathPart(openJobRequest.getJobId())
1236+
.addPathPartAsIs("_open")
1237+
.build();
1238+
Request request = new Request(HttpPost.METHOD_NAME, endpoint);
1239+
request.setJsonEntity(openJobRequest.toString());
1240+
return request;
1241+
}
1242+
12131243
static Request getMigrationAssistance(IndexUpgradeInfoRequest indexUpgradeInfoRequest) {
12141244
EndpointBuilder endpointBuilder = new EndpointBuilder()
12151245
.addPathPartAsIs("_xpack/migration/assistance")

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

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,10 @@
2020

2121
import com.carrotsearch.randomizedtesting.generators.CodepointSetGenerator;
2222
import org.elasticsearch.common.unit.TimeValue;
23+
import org.elasticsearch.protocol.xpack.ml.DeleteJobRequest;
24+
import org.elasticsearch.protocol.xpack.ml.DeleteJobResponse;
25+
import org.elasticsearch.protocol.xpack.ml.OpenJobRequest;
26+
import org.elasticsearch.protocol.xpack.ml.OpenJobResponse;
2327
import org.elasticsearch.protocol.xpack.ml.PutJobRequest;
2428
import org.elasticsearch.protocol.xpack.ml.PutJobResponse;
2529
import org.elasticsearch.protocol.xpack.ml.job.config.AnalysisConfig;
@@ -46,12 +50,37 @@ public void testPutJob() throws Exception {
4650
assertThat(createdJob.getJobType(), is(Job.ANOMALY_DETECTOR_JOB_TYPE));
4751
}
4852

53+
public void testDeleteJob() throws Exception {
54+
String jobId = randomValidJobId();
55+
Job job = buildJob(jobId);
56+
MachineLearningClient machineLearningClient = highLevelClient().machineLearning();
57+
machineLearningClient.putJob(new PutJobRequest(job), RequestOptions.DEFAULT);
58+
59+
DeleteJobResponse response = execute(new DeleteJobRequest(jobId),
60+
machineLearningClient::deleteJob,
61+
machineLearningClient::deleteJobAsync);
62+
63+
assertTrue(response.isAcknowledged());
64+
}
65+
66+
public void testOpenJob() throws Exception {
67+
String jobId = randomValidJobId();
68+
Job job = buildJob(jobId);
69+
MachineLearningClient machineLearningClient = highLevelClient().machineLearning();
70+
71+
machineLearningClient.putJob(new PutJobRequest(job), RequestOptions.DEFAULT);
72+
73+
OpenJobResponse response = execute(new OpenJobRequest(jobId), machineLearningClient::openJob, machineLearningClient::openJobAsync);
74+
75+
assertTrue(response.isOpened());
76+
}
77+
4978
public static String randomValidJobId() {
5079
CodepointSetGenerator generator = new CodepointSetGenerator("abcdefghijklmnopqrstuvwxyz0123456789".toCharArray());
5180
return generator.ofCodePointsLength(random(), 10, 10);
5281
}
5382

54-
private static Job buildJob(String jobId) {
83+
public static Job buildJob(String jobId) {
5584
Job.Builder builder = new Job.Builder(jobId);
5685
builder.setDescription(randomAlphaOfLength(10));
5786

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

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,8 @@
127127
import org.elasticsearch.index.rankeval.RestRankEvalAction;
128128
import org.elasticsearch.protocol.xpack.XPackInfoRequest;
129129
import org.elasticsearch.protocol.xpack.migration.IndexUpgradeInfoRequest;
130+
import org.elasticsearch.protocol.xpack.ml.DeleteJobRequest;
131+
import org.elasticsearch.protocol.xpack.ml.OpenJobRequest;
130132
import org.elasticsearch.protocol.xpack.watcher.DeleteWatchRequest;
131133
import org.elasticsearch.protocol.xpack.watcher.PutWatchRequest;
132134
import org.elasticsearch.repositories.fs.FsRepository;
@@ -2610,6 +2612,33 @@ public void testXPackDeleteWatch() {
26102612
assertThat(request.getEntity(), nullValue());
26112613
}
26122614

2615+
public void testDeleteMachineLearningJob() {
2616+
String jobId = randomAlphaOfLength(10);
2617+
DeleteJobRequest deleteJobRequest = new DeleteJobRequest(jobId);
2618+
2619+
Request request = RequestConverters.deleteMachineLearningJob(deleteJobRequest);
2620+
assertEquals(HttpDelete.METHOD_NAME, request.getMethod());
2621+
assertEquals("/_xpack/ml/anomaly_detectors/" + jobId, request.getEndpoint());
2622+
assertEquals(Boolean.toString(false), request.getParameters().get("force"));
2623+
2624+
deleteJobRequest.setForce(true);
2625+
request = RequestConverters.deleteMachineLearningJob(deleteJobRequest);
2626+
assertEquals(Boolean.toString(true), request.getParameters().get("force"));
2627+
}
2628+
2629+
public void testPostMachineLearningOpenJob() throws Exception {
2630+
String jobId = "some-job-id";
2631+
OpenJobRequest openJobRequest = new OpenJobRequest(jobId);
2632+
openJobRequest.setTimeout(TimeValue.timeValueMinutes(10));
2633+
2634+
Request request = RequestConverters.machineLearningOpenJob(openJobRequest);
2635+
assertEquals(HttpPost.METHOD_NAME, request.getMethod());
2636+
assertEquals("/_xpack/ml/anomaly_detectors/" + jobId + "/_open", request.getEndpoint());
2637+
ByteArrayOutputStream bos = new ByteArrayOutputStream();
2638+
request.getEntity().writeTo(bos);
2639+
assertEquals(bos.toString("UTF-8"), "{\"job_id\":\""+ jobId +"\",\"timeout\":\"10m\"}");
2640+
}
2641+
26132642
/**
26142643
* Randomize the {@link FetchSourceContext} request parameters.
26152644
*/

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

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,14 @@
2121
import org.elasticsearch.action.ActionListener;
2222
import org.elasticsearch.action.LatchedActionListener;
2323
import org.elasticsearch.client.ESRestHighLevelClientTestCase;
24+
import org.elasticsearch.client.MachineLearningIT;
2425
import org.elasticsearch.client.RequestOptions;
2526
import org.elasticsearch.client.RestHighLevelClient;
2627
import org.elasticsearch.common.unit.TimeValue;
28+
import org.elasticsearch.protocol.xpack.ml.DeleteJobRequest;
29+
import org.elasticsearch.protocol.xpack.ml.DeleteJobResponse;
30+
import org.elasticsearch.protocol.xpack.ml.OpenJobRequest;
31+
import org.elasticsearch.protocol.xpack.ml.OpenJobResponse;
2732
import org.elasticsearch.protocol.xpack.ml.PutJobRequest;
2833
import org.elasticsearch.protocol.xpack.ml.PutJobResponse;
2934
import org.elasticsearch.protocol.xpack.ml.job.config.AnalysisConfig;
@@ -118,4 +123,102 @@ public void onFailure(Exception e) {
118123
assertTrue(latch.await(30L, TimeUnit.SECONDS));
119124
}
120125
}
126+
127+
public void testDeleteJob() throws Exception {
128+
RestHighLevelClient client = highLevelClient();
129+
130+
String jobId = "my-first-machine-learning-job";
131+
132+
Job job = MachineLearningIT.buildJob(jobId);
133+
client.machineLearning().putJob(new PutJobRequest(job), RequestOptions.DEFAULT);
134+
135+
Job secondJob = MachineLearningIT.buildJob("my-second-machine-learning-job");
136+
client.machineLearning().putJob(new PutJobRequest(secondJob), RequestOptions.DEFAULT);
137+
138+
{
139+
//tag::x-pack-delete-ml-job-request
140+
DeleteJobRequest deleteJobRequest = new DeleteJobRequest("my-first-machine-learning-job");
141+
deleteJobRequest.setForce(false); //<1>
142+
DeleteJobResponse deleteJobResponse = client.machineLearning().deleteJob(deleteJobRequest, RequestOptions.DEFAULT);
143+
//end::x-pack-delete-ml-job-request
144+
145+
//tag::x-pack-delete-ml-job-response
146+
boolean isAcknowledged = deleteJobResponse.isAcknowledged(); //<1>
147+
//end::x-pack-delete-ml-job-response
148+
}
149+
{
150+
//tag::x-pack-delete-ml-job-request-listener
151+
ActionListener<DeleteJobResponse> listener = new ActionListener<DeleteJobResponse>() {
152+
@Override
153+
public void onResponse(DeleteJobResponse deleteJobResponse) {
154+
// <1>
155+
}
156+
157+
@Override
158+
public void onFailure(Exception e) {
159+
// <2>
160+
}
161+
};
162+
//end::x-pack-delete-ml-job-request-listener
163+
164+
// Replace the empty listener by a blocking listener in test
165+
final CountDownLatch latch = new CountDownLatch(1);
166+
listener = new LatchedActionListener<>(listener, latch);
167+
168+
//tag::x-pack-delete-ml-job-request-async
169+
DeleteJobRequest deleteJobRequest = new DeleteJobRequest("my-second-machine-learning-job");
170+
client.machineLearning().deleteJobAsync(deleteJobRequest, RequestOptions.DEFAULT, listener); // <1>
171+
//end::x-pack-delete-ml-job-request-async
172+
173+
assertTrue(latch.await(30L, TimeUnit.SECONDS));
174+
}
175+
}
176+
177+
public void testOpenJob() throws Exception {
178+
RestHighLevelClient client = highLevelClient();
179+
180+
Job job = MachineLearningIT.buildJob("opening-my-first-machine-learning-job");
181+
client.machineLearning().putJob(new PutJobRequest(job), RequestOptions.DEFAULT);
182+
183+
Job secondJob = MachineLearningIT.buildJob("opening-my-second-machine-learning-job");
184+
client.machineLearning().putJob(new PutJobRequest(secondJob), RequestOptions.DEFAULT);
185+
186+
{
187+
//tag::x-pack-ml-open-job-request
188+
OpenJobRequest openJobRequest = new OpenJobRequest("opening-my-first-machine-learning-job"); //<1>
189+
openJobRequest.setTimeout(TimeValue.timeValueMinutes(10)); //<2>
190+
//end::x-pack-ml-open-job-request
191+
192+
//tag::x-pack-ml-open-job-execute
193+
OpenJobResponse openJobResponse = client.machineLearning().openJob(openJobRequest, RequestOptions.DEFAULT);
194+
boolean isOpened = openJobResponse.isOpened(); //<1>
195+
//end::x-pack-ml-open-job-execute
196+
197+
}
198+
{
199+
//tag::x-pack-ml-open-job-listener
200+
ActionListener<OpenJobResponse> listener = new ActionListener<OpenJobResponse>() {
201+
@Override
202+
public void onResponse(OpenJobResponse openJobResponse) {
203+
//<1>
204+
}
205+
206+
@Override
207+
public void onFailure(Exception e) {
208+
// <2>
209+
}
210+
};
211+
//end::x-pack-ml-open-job-listener
212+
OpenJobRequest openJobRequest = new OpenJobRequest("opening-my-second-machine-learning-job");
213+
// Replace the empty listener by a blocking listener in test
214+
final CountDownLatch latch = new CountDownLatch(1);
215+
listener = new LatchedActionListener<>(listener, latch);
216+
217+
// tag::x-pack-ml-open-job-execute-async
218+
client.machineLearning().openJobAsync(openJobRequest, RequestOptions.DEFAULT, listener); //<1>
219+
// end::x-pack-ml-open-job-execute-async
220+
221+
assertTrue(latch.await(30L, TimeUnit.SECONDS));
222+
}
223+
}
121224
}

0 commit comments

Comments
 (0)