Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

import org.elasticsearch.action.ActionListener;
import org.elasticsearch.client.core.AcknowledgedResponse;
import org.elasticsearch.client.enrich.DeletePolicyRequest;
import org.elasticsearch.client.enrich.PutPolicyRequest;

import java.io.IOException;
Expand All @@ -43,7 +44,7 @@ public final class EnrichClient {
/**
* Executes the put policy api, which stores an enrich policy.
*
* See <a href="https://www.elastic.co/guide/en/elasticsearch/reference/current/enrich-processor.html#put-policy-api">
* See <a href="https://www.elastic.co/guide/en/elasticsearch/reference/current/enrich-policy-apis.html#put-policy-api">
* the docs</a> for more.
*
* @param request the {@link PutPolicyRequest}
Expand All @@ -64,7 +65,7 @@ public AcknowledgedResponse putPolicy(PutPolicyRequest request, RequestOptions o
/**
* Asynchronously executes the put policy api, which stores an enrich policy.
*
* See <a href="https://www.elastic.co/guide/en/elasticsearch/reference/current/enrich-processor.html#put-policy-api">
* See <a href="https://www.elastic.co/guide/en/elasticsearch/reference/enrich-policy-apis.html#put-policy-api">
* the docs</a> for more.
*
* @param request the {@link PutPolicyRequest}
Expand All @@ -83,4 +84,48 @@ public void putPolicyAsync(PutPolicyRequest request,
Collections.emptySet()
);
}

/**
* Executes the delete policy api, which deletes an enrich policy.
*
* See <a href="https://www.elastic.co/guide/en/elasticsearch/reference/current/enrich-policy-apis.html#delete-policy-api">
* the docs</a> for more.
*
* @param request the {@link DeletePolicyRequest}
* @param options the request options (e.g. headers), use {@link RequestOptions#DEFAULT} if nothing needs to be customized
* @return the response
* @throws IOException in case there is a problem sending the request or parsing back the response
*/
public AcknowledgedResponse deletePolicy(DeletePolicyRequest request, RequestOptions options) throws IOException {
return restHighLevelClient.performRequestAndParseEntity(
request,
EnrichRequestConverters::deletePolicy,
options,
AcknowledgedResponse::fromXContent,
Collections.emptySet()
);
}

/**
* Asynchronously executes the delete policy api, which deletes an enrich policy.
*
* See <a href="https://www.elastic.co/guide/en/elasticsearch/reference/current/enrich-policy-apis.html#delete-policy-api">
* the docs</a> for more.
*
* @param request the {@link DeletePolicyRequest}
* @param options the request options (e.g. headers), use {@link RequestOptions#DEFAULT} if nothing needs to be customized
* @param listener the listener to be notified upon request completion
*/
public void deletePolicyAsync(DeletePolicyRequest request,
RequestOptions options,
ActionListener<AcknowledgedResponse> listener) {
restHighLevelClient.performRequestAsyncAndParseEntity(
request,
EnrichRequestConverters::deletePolicy,
options,
AcknowledgedResponse::fromXContent,
listener,
Collections.emptySet()
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@
*/
package org.elasticsearch.client;

import org.apache.http.client.methods.HttpDelete;
import org.apache.http.client.methods.HttpPut;
import org.elasticsearch.client.enrich.DeletePolicyRequest;
import org.elasticsearch.client.enrich.PutPolicyRequest;

import java.io.IOException;
Expand All @@ -38,4 +40,12 @@ static Request putPolicy(PutPolicyRequest putPolicyRequest) throws IOException {
return request;
}

static Request deletePolicy(DeletePolicyRequest deletePolicyRequest) {
String endpoint = new RequestConverters.EndpointBuilder()
.addPathPartAsIs("_enrich", "policy")
.addPathPart(deletePolicyRequest.getName())
.build();
return new Request(HttpDelete.METHOD_NAME, endpoint);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*
* 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.enrich;

import org.elasticsearch.client.Validatable;
import org.elasticsearch.common.Strings;

public class DeletePolicyRequest implements Validatable {

private final String name;

public DeletePolicyRequest(String name) {
if (Strings.hasLength(name) == false) {
throw new IllegalArgumentException("name must be a non-null and non-empty string");
}
this.name = name;
}

public String getName() {
return name;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

import org.apache.http.util.EntityUtils;
import org.elasticsearch.client.core.AcknowledgedResponse;
import org.elasticsearch.client.enrich.DeletePolicyRequest;
import org.elasticsearch.client.enrich.PutPolicyRequest;
import org.elasticsearch.common.xcontent.XContentHelper;
import org.elasticsearch.common.xcontent.json.JsonXContent;
Expand All @@ -46,15 +47,26 @@ public void testCRUD() throws Exception {
Response getPolicyResponse = highLevelClient().getLowLevelClient().performRequest(getPolicyRequest);
assertThat(getPolicyResponse.getHttpResponse().getStatusLine().getStatusCode(), equalTo(200));
Map<String, Object> responseBody = toMap(getPolicyResponse);
@SuppressWarnings("unchecked")
List<Map<String, Object>> responsePolicies = (List<Map<String, Object>>) responseBody.get("policies");
List<?> responsePolicies = (List<?>) responseBody.get("policies");
assertThat(responsePolicies.size(), equalTo(1));
assertThat(XContentMapValues.extractValue("exact_match.indices", responsePolicies.get(0)),
equalTo(putPolicyRequest.getIndices()));
assertThat(XContentMapValues.extractValue("exact_match.match_field", responsePolicies.get(0)),
equalTo(putPolicyRequest.getMatchField()));
assertThat(XContentMapValues.extractValue("exact_match.enrich_fields", responsePolicies.get(0)),
Map<?, ?> responsePolicy = (Map<?, ?>) responsePolicies.get(0);
assertThat(XContentMapValues.extractValue("exact_match.indices", responsePolicy), equalTo(putPolicyRequest.getIndices()));
assertThat(XContentMapValues.extractValue("exact_match.match_field", responsePolicy), equalTo(putPolicyRequest.getMatchField()));
assertThat(XContentMapValues.extractValue("exact_match.enrich_fields", responsePolicy),
equalTo(putPolicyRequest.getEnrichFields()));

DeletePolicyRequest deletePolicyRequest = new DeletePolicyRequest("my-policy");
AcknowledgedResponse deletePolicyResponse =
execute(deletePolicyRequest, enrichClient::deletePolicy, enrichClient::deletePolicyAsync);
assertThat(deletePolicyResponse.isAcknowledged(), is(true));

// TODO: Replace with get policy hlrc code:
getPolicyRequest = new Request("get", "/_enrich/policy");
getPolicyResponse = highLevelClient().getLowLevelClient().performRequest(getPolicyRequest);
assertThat(getPolicyResponse.getHttpResponse().getStatusLine().getStatusCode(), equalTo(200));
responseBody = toMap(getPolicyResponse);
responsePolicies = (List<?>) responseBody.get("policies");
assertThat(responsePolicies.size(), equalTo(0));
}

private static Map<String, Object> toMap(Response response) throws IOException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,15 @@
*/
package org.elasticsearch.client;

import org.apache.http.client.methods.HttpDelete;
import org.apache.http.client.methods.HttpPut;
import org.elasticsearch.client.enrich.DeletePolicyRequest;
import org.elasticsearch.client.enrich.PutPolicyRequest;
import org.elasticsearch.client.enrich.PutPolicyRequestTests;
import org.elasticsearch.test.ESTestCase;

import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.nullValue;

public class EnrichRequestConvertersTests extends ESTestCase {

Expand All @@ -33,7 +36,18 @@ public void testPutPolicy() throws Exception {

assertThat(result.getMethod(), equalTo(HttpPut.METHOD_NAME));
assertThat(result.getEndpoint(), equalTo("/_enrich/policy/" + request.getName()));
assertThat(result.getParameters().size(), equalTo(0));
RequestConvertersTests.assertToXContentBody(request, result.getEntity());
}

public void testDeletePolicy() throws Exception {
DeletePolicyRequest request = new DeletePolicyRequest(randomAlphaOfLength(4));
Request result = EnrichRequestConverters.deletePolicy(request);

assertThat(result.getMethod(), equalTo(HttpDelete.METHOD_NAME));
assertThat(result.getEndpoint(), equalTo("/_enrich/policy/" + request.getName()));
assertThat(result.getParameters().size(), equalTo(0));
assertThat(result.getEntity(), nullValue());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,27 @@
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.client.core.AcknowledgedResponse;
import org.elasticsearch.client.enrich.DeletePolicyRequest;
import org.elasticsearch.client.enrich.PutPolicyRequest;
import org.junit.After;

import java.util.List;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;

public class EnrichDocumentationIT extends ESRestHighLevelClientTestCase {

@After
public void cleanup() {
RestHighLevelClient client = highLevelClient();
DeletePolicyRequest deletePolicyRequest = new DeletePolicyRequest("users-policy");
try {
client.enrich().deletePolicy(deletePolicyRequest, RequestOptions.DEFAULT);
} catch (Exception e) {
// ignore... it is ok if policy has already been removed
}
}

public void testPutPolicy() throws Exception {
RestHighLevelClient client = highLevelClient();
// tag::enrich-put-policy-request
Expand All @@ -54,8 +67,7 @@ public void testPutPolicy() throws Exception {
ActionListener<AcknowledgedResponse> listener = new ActionListener<>() {
@Override
public void onResponse(AcknowledgedResponse response) { // <1>
boolean isAcknowledged =
putPolicyResponse.isAcknowledged();
boolean isAcknowledged = response.isAcknowledged();
}

@Override
Expand All @@ -77,4 +89,56 @@ public void onFailure(Exception e) {
assertTrue(latch.await(30L, TimeUnit.SECONDS));
}

public void testDeletePolicy() throws Exception {
RestHighLevelClient client = highLevelClient();

{
// Add a policy, so that it can be deleted:
PutPolicyRequest putPolicyRequest = new PutPolicyRequest(
"users-policy", "exact_match", List.of("users"),
"email", List.of("address", "zip", "city", "state"));
client.enrich().putPolicy(putPolicyRequest, RequestOptions.DEFAULT);
}

// tag::enrich-delete-policy-request
DeletePolicyRequest deletePolicyRequest =
new DeletePolicyRequest("users-policy");
// end::enrich-delete-policy-request

// tag::enrich-delete-policy-execute
AcknowledgedResponse deletePolicyResponse = client.enrich()
.deletePolicy(deletePolicyRequest, RequestOptions.DEFAULT);
// end::enrich-delete-policy-execute

// tag::enrich-delete-policy-response
boolean isAcknowledged =
deletePolicyResponse.isAcknowledged(); // <1>
// end::enrich-delete-policy-response

// tag::enrich-delete-policy-execute-listener
ActionListener<AcknowledgedResponse> listener = new ActionListener<>() {
@Override
public void onResponse(AcknowledgedResponse response) { // <1>
boolean isAcknowledged = response.isAcknowledged();
}

@Override
public void onFailure(Exception e) {
// <2>
}
};
// end::enrich-delete-policy-execute-listener

// Replace the empty listener by a blocking listener in test
final CountDownLatch latch = new CountDownLatch(1);
listener = new LatchedActionListener<>(listener, latch);

// tag::enrich-delete-policy-execute-async
client.enrich().deletePolicyAsync(deletePolicyRequest,
RequestOptions.DEFAULT, listener); // <1>
// end::enrich-delete-policy-execute-async

assertTrue(latch.await(30L, TimeUnit.SECONDS));
}

}
31 changes: 31 additions & 0 deletions docs/java-rest/high-level/enrich/delete_policy.asciidoc
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
--
:api: enrich-delete-policy
:request: DeletePolicyRequest
:response: AcknowledgedResponse
--

[id="{upid}-{api}"]
=== Delete Policy API

[id="{upid}-{api}-request"]
==== Request

The Delete Policy API deletes an enrich policy from Elasticsearch.

["source","java",subs="attributes,callouts,macros"]
--------------------------------------------------
include-tagged::{doc-tests-file}[{api}-request]
--------------------------------------------------

[id="{upid}-{api}-response"]
==== Response

The returned +{response}+ indicates if the delete policy request was acknowledged.

["source","java",subs="attributes,callouts,macros"]
--------------------------------------------------
include-tagged::{doc-tests-file}[{api}-response]
--------------------------------------------------
<1> Whether delete policy request was acknowledged.

include::../execution.asciidoc[]
2 changes: 2 additions & 0 deletions docs/java-rest/high-level/supported-apis.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -603,5 +603,7 @@ include::dataframe/stop_data_frame.asciidoc[]
The Java High Level REST Client supports the following Enrich APIs:

* <<{upid}-enrich-put-policy>>
* <<{upid}-enrich-delete-policy>>

include::enrich/put_policy.asciidoc[]
include::enrich/delete_policy.asciidoc[]