diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/MLRequestConverters.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/MLRequestConverters.java index 44f23c597c8..4c309068066 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/MLRequestConverters.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/MLRequestConverters.java @@ -32,6 +32,7 @@ import org.elasticsearch.client.ml.DeleteCalendarRequest; import org.elasticsearch.client.ml.DeleteDatafeedRequest; import org.elasticsearch.client.ml.DeleteForecastRequest; import org.elasticsearch.client.ml.DeleteJobRequest; +import org.elasticsearch.client.ml.DeleteModelSnapshotRequest; import org.elasticsearch.client.ml.FlushJobRequest; import org.elasticsearch.client.ml.ForecastJobRequest; import org.elasticsearch.client.ml.GetBucketsRequest; @@ -335,6 +336,18 @@ final class MLRequestConverters { return request; } + static Request deleteModelSnapshot(DeleteModelSnapshotRequest deleteModelSnapshotRequest) { + String endpoint = new EndpointBuilder() + .addPathPartAsIs("_xpack") + .addPathPartAsIs("ml") + .addPathPartAsIs("anomaly_detectors") + .addPathPart(deleteModelSnapshotRequest.getJobId()) + .addPathPartAsIs("model_snapshots") + .addPathPart(deleteModelSnapshotRequest.getSnapshotId()) + .build(); + return new Request(HttpDelete.METHOD_NAME, endpoint); + } + static Request getBuckets(GetBucketsRequest getBucketsRequest) throws IOException { String endpoint = new EndpointBuilder() .addPathPartAsIs("_xpack") diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/MachineLearningClient.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/MachineLearningClient.java index d977ad791a8..7afc5a462ed 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/MachineLearningClient.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/MachineLearningClient.java @@ -27,6 +27,7 @@ import org.elasticsearch.client.ml.DeleteDatafeedRequest; import org.elasticsearch.client.ml.DeleteForecastRequest; import org.elasticsearch.client.ml.DeleteJobRequest; import org.elasticsearch.client.ml.DeleteJobResponse; +import org.elasticsearch.client.ml.DeleteModelSnapshotRequest; import org.elasticsearch.client.ml.FlushJobRequest; import org.elasticsearch.client.ml.FlushJobResponse; import org.elasticsearch.client.ml.ForecastJobRequest; @@ -464,6 +465,47 @@ public final class MachineLearningClient { Collections.emptySet()); } + /** + * Deletes Machine Learning Model Snapshots + *
+ * For additional info + * see + * ML Delete Model Snapshot documentation + * + * @param request The request to delete the model snapshot + * @param options Additional request options (e.g. headers), use {@link RequestOptions#DEFAULT} if nothing needs to be customized + * @return action acknowledgement + * @throws IOException when there is a serialization issue sending the request or receiving the response + */ + public AcknowledgedResponse deleteModelSnapshot(DeleteModelSnapshotRequest request, RequestOptions options) throws IOException { + return restHighLevelClient.performRequestAndParseEntity(request, + MLRequestConverters::deleteModelSnapshot, + options, + AcknowledgedResponse::fromXContent, + Collections.emptySet()); + } + + /** + * Deletes Machine Learning Model Snapshots asynchronously and notifies the listener on completion + *
+ * For additional info
+ * see
+ * ML Delete Model Snapshot documentation
+ *
+ * @param request The request to delete the model snapshot
+ * @param options Additional request options (e.g. headers), use {@link RequestOptions#DEFAULT} if nothing needs to be customized
+ * @param listener Listener to be notified upon request completion
+ */
+ public void deleteModelSnapshotAsync(DeleteModelSnapshotRequest request, RequestOptions options,
+ ActionListener
diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/DeleteModelSnapshotRequest.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/DeleteModelSnapshotRequest.java
new file mode 100644
index 00000000000..1c153e3555b
--- /dev/null
+++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/DeleteModelSnapshotRequest.java
@@ -0,0 +1,74 @@
+/*
+ * Licensed to Elasticsearch under one or more contributor
+ * license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright
+ * ownership. Elasticsearch licenses this file to you under
+ * the Apache License, Version 2.0 (the "License"); you may
+ * not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.elasticsearch.client.ml;
+
+import org.elasticsearch.action.ActionRequest;
+import org.elasticsearch.action.ActionRequestValidationException;
+import org.elasticsearch.client.ml.job.config.Job;
+import org.elasticsearch.client.ml.job.process.ModelSnapshot;
+
+
+import java.util.Objects;
+
+/**
+ * Request to delete a Machine Learning Model Snapshot Job via its Job and Snapshot IDs
+ */
+public class DeleteModelSnapshotRequest extends ActionRequest {
+
+ private final String jobId;
+ private final String snapshotId;
+
+ public DeleteModelSnapshotRequest(String jobId, String snapshotId) {
+ this.jobId = Objects.requireNonNull(jobId, "[" + Job.ID + "] must not be null");
+ this.snapshotId = Objects.requireNonNull(snapshotId, "[" + ModelSnapshot.SNAPSHOT_ID + "] must not be null");
+ }
+
+ public String getJobId() {
+ return jobId;
+ }
+
+ public String getSnapshotId() {
+ return snapshotId;
+ }
+
+ @Override
+ public ActionRequestValidationException validate() {
+ return null;
+ }
+
+ @Override
+ public int hashCode() {
+ return Objects.hash(jobId, snapshotId);
+ }
+
+ @Override
+ public boolean equals(Object obj) {
+ if (this == obj) {
+ return true;
+ }
+
+ if (obj == null || obj.getClass() != getClass()) {
+ return false;
+ }
+
+ DeleteModelSnapshotRequest other = (DeleteModelSnapshotRequest) obj;
+ return Objects.equals(jobId, other.jobId) && Objects.equals(snapshotId, other.snapshotId);
+ }
+
+}
diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/MLRequestConvertersTests.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/MLRequestConvertersTests.java
index ed4d59de318..c6839411877 100644
--- a/client/rest-high-level/src/test/java/org/elasticsearch/client/MLRequestConvertersTests.java
+++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/MLRequestConvertersTests.java
@@ -28,6 +28,7 @@ import org.elasticsearch.client.ml.DeleteCalendarRequest;
import org.elasticsearch.client.ml.DeleteDatafeedRequest;
import org.elasticsearch.client.ml.DeleteForecastRequest;
import org.elasticsearch.client.ml.DeleteJobRequest;
+import org.elasticsearch.client.ml.DeleteModelSnapshotRequest;
import org.elasticsearch.client.ml.FlushJobRequest;
import org.elasticsearch.client.ml.ForecastJobRequest;
import org.elasticsearch.client.ml.GetBucketsRequest;
@@ -362,6 +363,16 @@ public class MLRequestConvertersTests extends ESTestCase {
request.getParameters().get(DeleteForecastRequest.ALLOW_NO_FORECASTS.getPreferredName()));
}
+ public void testDeleteModelSnapshot() {
+ String jobId = randomAlphaOfLength(10);
+ String snapshotId = randomAlphaOfLength(10);
+ DeleteModelSnapshotRequest deleteModelSnapshotRequest = new DeleteModelSnapshotRequest(jobId, snapshotId);
+
+ Request request = MLRequestConverters.deleteModelSnapshot(deleteModelSnapshotRequest);
+ assertEquals(HttpDelete.METHOD_NAME, request.getMethod());
+ assertEquals("/_xpack/ml/anomaly_detectors/" + jobId + "/model_snapshots/" + snapshotId, request.getEndpoint());
+ }
+
public void testGetBuckets() throws IOException {
String jobId = randomAlphaOfLength(10);
GetBucketsRequest getBucketsRequest = new GetBucketsRequest(jobId);
diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/MachineLearningIT.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/MachineLearningIT.java
index 51c583e139c..5a9c7890fdc 100644
--- a/client/rest-high-level/src/test/java/org/elasticsearch/client/MachineLearningIT.java
+++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/MachineLearningIT.java
@@ -34,6 +34,7 @@ import org.elasticsearch.client.ml.DeleteDatafeedRequest;
import org.elasticsearch.client.ml.DeleteForecastRequest;
import org.elasticsearch.client.ml.DeleteJobRequest;
import org.elasticsearch.client.ml.DeleteJobResponse;
+import org.elasticsearch.client.ml.DeleteModelSnapshotRequest;
import org.elasticsearch.client.ml.FlushJobRequest;
import org.elasticsearch.client.ml.FlushJobResponse;
import org.elasticsearch.client.ml.ForecastJobRequest;
@@ -996,4 +997,38 @@ public class MachineLearningIT extends ESRestHighLevelClientTestCase {
highLevelClient().machineLearning().putDatafeed(new PutDatafeedRequest(datafeed), RequestOptions.DEFAULT);
return datafeedId;
}
+
+ public void createModelSnapshot(String jobId, String snapshotId) throws IOException {
+ Job job = MachineLearningIT.buildJob(jobId);
+ highLevelClient().machineLearning().putJob(new PutJobRequest(job), RequestOptions.DEFAULT);
+
+ IndexRequest indexRequest = new IndexRequest(".ml-anomalies-shared", "doc");
+ indexRequest.setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE);
+ indexRequest.source("{\"job_id\":\"" + jobId + "\", \"timestamp\":1541587919000, " +
+ "\"description\":\"State persisted due to job close at 2018-11-07T10:51:59+0000\", " +
+ "\"snapshot_id\":\"" + snapshotId + "\", \"snapshot_doc_count\":1, \"model_size_stats\":{" +
+ "\"job_id\":\"" + jobId + "\", \"result_type\":\"model_size_stats\",\"model_bytes\":51722, " +
+ "\"total_by_field_count\":3, \"total_over_field_count\":0, \"total_partition_field_count\":2," +
+ "\"bucket_allocation_failures_count\":0, \"memory_status\":\"ok\", \"log_time\":1541587919000, " +
+ "\"timestamp\":1519930800000}, \"latest_record_time_stamp\":1519931700000," +
+ "\"latest_result_time_stamp\":1519930800000, \"retain\":false}", XContentType.JSON);
+
+ highLevelClient().index(indexRequest, RequestOptions.DEFAULT);
+ }
+
+ public void testDeleteModelSnapshot() throws IOException {
+ String jobId = "test-delete-model-snapshot";
+ String snapshotId = "1541587919";
+
+ createModelSnapshot(jobId, snapshotId);
+
+ MachineLearningClient machineLearningClient = highLevelClient().machineLearning();
+
+ DeleteModelSnapshotRequest request = new DeleteModelSnapshotRequest(jobId, snapshotId);
+
+ AcknowledgedResponse response = execute(request, machineLearningClient::deleteModelSnapshot,
+ machineLearningClient::deleteModelSnapshotAsync);
+
+ assertTrue(response.isAcknowledged());
+ }
}
diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/documentation/MlClientDocumentationIT.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/documentation/MlClientDocumentationIT.java
index 1d9f511245a..3b1352b1706 100644
--- a/client/rest-high-level/src/test/java/org/elasticsearch/client/documentation/MlClientDocumentationIT.java
+++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/documentation/MlClientDocumentationIT.java
@@ -40,6 +40,7 @@ import org.elasticsearch.client.ml.DeleteDatafeedRequest;
import org.elasticsearch.client.ml.DeleteForecastRequest;
import org.elasticsearch.client.ml.DeleteJobRequest;
import org.elasticsearch.client.ml.DeleteJobResponse;
+import org.elasticsearch.client.ml.DeleteModelSnapshotRequest;
import org.elasticsearch.client.ml.FlushJobRequest;
import org.elasticsearch.client.ml.FlushJobResponse;
import org.elasticsearch.client.ml.ForecastJobRequest;
@@ -1867,6 +1868,73 @@ public class MlClientDocumentationIT extends ESRestHighLevelClientTestCase {
}
}
+ public void testDeleteModelSnapshot() throws IOException, InterruptedException {
+ RestHighLevelClient client = highLevelClient();
+
+ String jobId = "test-delete-model-snapshot";
+ String snapshotId = "1541587919";
+ Job job = MachineLearningIT.buildJob(jobId);
+ client.machineLearning().putJob(new PutJobRequest(job), RequestOptions.DEFAULT);
+
+ // Let us index a snapshot
+ IndexRequest indexRequest = new IndexRequest(".ml-anomalies-shared", "doc");
+ indexRequest.setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE);
+ indexRequest.source("{\"job_id\":\"" + jobId + "\", \"timestamp\":1541587919000, " +
+ "\"description\":\"State persisted due to job close at 2018-11-07T10:51:59+0000\", " +
+ "\"snapshot_id\":\"" + snapshotId + "\", \"snapshot_doc_count\":1, \"model_size_stats\":{" +
+ "\"job_id\":\"" + jobId + "\", \"result_type\":\"model_size_stats\",\"model_bytes\":51722, " +
+ "\"total_by_field_count\":3, \"total_over_field_count\":0, \"total_partition_field_count\":2," +
+ "\"bucket_allocation_failures_count\":0, \"memory_status\":\"ok\", \"log_time\":1541587919000, " +
+ "\"timestamp\":1519930800000}, \"latest_record_time_stamp\":1519931700000," +
+ "\"latest_result_time_stamp\":1519930800000, \"retain\":false}", XContentType.JSON);
+ {
+ client.index(indexRequest, RequestOptions.DEFAULT);
+
+ // tag::delete-model-snapshot-request
+ DeleteModelSnapshotRequest request = new DeleteModelSnapshotRequest(jobId, snapshotId); // <1>
+ // end::delete-model-snapshot-request
+
+ // tag::delete-model-snapshot-execute
+ AcknowledgedResponse response = client.machineLearning().deleteModelSnapshot(request, RequestOptions.DEFAULT);
+ // end::delete-model-snapshot-execute
+
+ // tag::delete-model-snapshot-response
+ boolean isAcknowledged = response.isAcknowledged(); // <1>
+ // end::delete-model-snapshot-response
+
+ assertTrue(isAcknowledged);
+ }
+ {
+ client.index(indexRequest, RequestOptions.DEFAULT);
+
+ // tag::delete-model-snapshot-execute-listener
+ ActionListener