mirror of
https://github.com/honeymoose/OpenSearch.git
synced 2025-03-28 02:48:38 +00:00
Add delete API to the High Level Rest Client
This commit is contained in:
parent
3c26754f87
commit
7df1df24e8
@ -20,6 +20,7 @@
|
||||
package org.elasticsearch.client;
|
||||
|
||||
import org.apache.http.HttpEntity;
|
||||
import org.elasticsearch.action.delete.DeleteRequest;
|
||||
import org.elasticsearch.action.get.GetRequest;
|
||||
import org.elasticsearch.common.Strings;
|
||||
import org.elasticsearch.common.lucene.uid.Versions;
|
||||
@ -67,6 +68,10 @@ final class Request {
|
||||
return new Request("GET", getEndpoint(getRequest), getParams(getRequest), null);
|
||||
}
|
||||
|
||||
static Request delete(DeleteRequest deleteRequest) {
|
||||
return new Request("DELETE", deleteEndpoint(deleteRequest), deleteParams(deleteRequest), null);
|
||||
}
|
||||
|
||||
private static Map<String, String> getParams(GetRequest getRequest) {
|
||||
Map<String, String> params = new HashMap<>();
|
||||
putParam("preference", getRequest.preference(), params);
|
||||
@ -102,11 +107,29 @@ final class Request {
|
||||
return Collections.unmodifiableMap(params);
|
||||
}
|
||||
|
||||
private static Map<String, String> deleteParams(DeleteRequest deleteRequest) {
|
||||
Map<String, String> params = new HashMap<>();
|
||||
putParam("routing", deleteRequest.routing(), params);
|
||||
putParam("parent", deleteRequest.parent(), params);
|
||||
if (deleteRequest.version() != Versions.MATCH_ANY) {
|
||||
params.put("version", Long.toString(deleteRequest.version()));
|
||||
}
|
||||
if (deleteRequest.versionType() != VersionType.INTERNAL) {
|
||||
params.put("version_type", deleteRequest.versionType().name().toLowerCase(Locale.ROOT));
|
||||
}
|
||||
return Collections.unmodifiableMap(params);
|
||||
}
|
||||
|
||||
private static String getEndpoint(GetRequest getRequest) {
|
||||
StringJoiner pathJoiner = new StringJoiner("/", "/", "");
|
||||
return pathJoiner.add(getRequest.index()).add(getRequest.type()).add(getRequest.id()).toString();
|
||||
}
|
||||
|
||||
private static String deleteEndpoint(DeleteRequest deleteRequest) {
|
||||
StringJoiner pathJoiner = new StringJoiner("/", "/", "");
|
||||
return pathJoiner.add(deleteRequest.index()).add(deleteRequest.type()).add(deleteRequest.id()).toString();
|
||||
}
|
||||
|
||||
private static void putParam(String key, String value, Map<String, String> params) {
|
||||
if (Strings.hasLength(value)) {
|
||||
params.put(key, value);
|
||||
|
@ -26,6 +26,8 @@ import org.elasticsearch.ElasticsearchStatusException;
|
||||
import org.elasticsearch.action.ActionListener;
|
||||
import org.elasticsearch.action.ActionRequest;
|
||||
import org.elasticsearch.action.ActionRequestValidationException;
|
||||
import org.elasticsearch.action.delete.DeleteRequest;
|
||||
import org.elasticsearch.action.delete.DeleteResponse;
|
||||
import org.elasticsearch.action.get.GetRequest;
|
||||
import org.elasticsearch.action.get.GetResponse;
|
||||
import org.elasticsearch.action.main.MainRequest;
|
||||
@ -92,6 +94,22 @@ public class RestHighLevelClient {
|
||||
Collections.emptySet(), headers);
|
||||
}
|
||||
|
||||
/**
|
||||
* Deletes a document by id using the delete api
|
||||
*/
|
||||
public DeleteResponse delete(DeleteRequest deleteRequest, Header... headers) throws IOException {
|
||||
return performRequestAndParseEntity(deleteRequest, Request::delete, DeleteResponse::fromXContent, Collections.singleton(404),
|
||||
headers);
|
||||
}
|
||||
|
||||
/**
|
||||
* Asynchronously deletes a document by id using the delete api
|
||||
*/
|
||||
public void deleteAsync(DeleteRequest deleteRequest, ActionListener<DeleteResponse> listener, Header... headers) {
|
||||
performRequestAsyncAndParseEntity(deleteRequest, Request::delete, DeleteResponse::fromXContent, listener,
|
||||
Collections.singleton(404), headers);
|
||||
}
|
||||
|
||||
private <Req extends ActionRequest, Resp> Resp performRequestAndParseEntity(Req request, Function<Req, Request> requestConverter,
|
||||
CheckedFunction<XContentParser, Resp, IOException> entityParser, Set<Integer> ignores, Header... headers) throws IOException {
|
||||
return performRequest(request, requestConverter, (response) -> parseEntity(response.getEntity(), entityParser), ignores, headers);
|
||||
|
@ -22,6 +22,9 @@ package org.elasticsearch.client;
|
||||
import org.apache.http.entity.ContentType;
|
||||
import org.apache.http.entity.StringEntity;
|
||||
import org.elasticsearch.ElasticsearchException;
|
||||
import org.elasticsearch.action.DocWriteResponse;
|
||||
import org.elasticsearch.action.delete.DeleteRequest;
|
||||
import org.elasticsearch.action.delete.DeleteResponse;
|
||||
import org.elasticsearch.action.get.GetRequest;
|
||||
import org.elasticsearch.action.get.GetResponse;
|
||||
import org.elasticsearch.common.Strings;
|
||||
@ -141,4 +144,40 @@ public class CrudIT extends ESRestHighLevelClientTestCase {
|
||||
assertEquals("value1", sourceAsMap.get("field1"));
|
||||
}
|
||||
}
|
||||
|
||||
public void testDelete() throws IOException {
|
||||
{
|
||||
DeleteRequest deleteRequest = new DeleteRequest("index", "type", "does_not_exist");
|
||||
DeleteResponse deleteResponse = execute(deleteRequest, highLevelClient()::delete, highLevelClient()::deleteAsync);
|
||||
assertEquals("index", deleteResponse.getIndex());
|
||||
assertEquals("type", deleteResponse.getType());
|
||||
assertEquals("does_not_exist", deleteResponse.getId());
|
||||
assertEquals(DocWriteResponse.Result.NOT_FOUND, deleteResponse.getResult());
|
||||
assertEquals(1, deleteResponse.getVersion());
|
||||
}
|
||||
String document = "{\"field1\":\"value1\",\"field2\":\"value2\"}";
|
||||
StringEntity stringEntity = new StringEntity(document, ContentType.APPLICATION_JSON);
|
||||
Response response = client().performRequest("PUT", "/index/type/id", Collections.singletonMap("refresh", "wait_for"), stringEntity);
|
||||
assertEquals(201, response.getStatusLine().getStatusCode());
|
||||
{
|
||||
DeleteRequest deleteRequest = new DeleteRequest("index", "type", "id").version(2);
|
||||
ElasticsearchException exception = expectThrows(ElasticsearchException.class,
|
||||
() -> execute(deleteRequest, highLevelClient()::delete, highLevelClient()::deleteAsync));
|
||||
assertEquals(RestStatus.CONFLICT, exception.status());
|
||||
assertEquals("Elasticsearch exception [type=version_conflict_engine_exception, " + "reason=[type][id]: " +
|
||||
"version conflict, current version [1] is different than the one provided [2]]", exception.getMessage());
|
||||
assertEquals("index", exception.getMetadata("es.index").get(0));
|
||||
}
|
||||
{
|
||||
DeleteRequest deleteRequest = new DeleteRequest("index", "type", "id");
|
||||
if (randomBoolean()) {
|
||||
deleteRequest.version(1L);
|
||||
}
|
||||
DeleteResponse deleteResponse = execute(deleteRequest, highLevelClient()::delete, highLevelClient()::deleteAsync);
|
||||
assertEquals("index", deleteResponse.getIndex());
|
||||
assertEquals("type", deleteResponse.getType());
|
||||
assertEquals("id", deleteResponse.getId());
|
||||
assertEquals(DocWriteResponse.Result.DELETED, deleteResponse.getResult());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user