Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add delete API to the High Level Rest Client #23187

Merged
merged 12 commits into from
Feb 24, 2017
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -67,6 +68,10 @@ static Request get(GetRequest getRequest) {
return new Request("GET", getEndpoint(getRequest), getParams(getRequest), null);
}

static Request delete(DeleteRequest deleteRequest) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you please move this up so that methods are somewhat alphabetically sorted?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok. I'm also moving ping method. And add a comments for coming developers

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);
Expand Down Expand Up @@ -102,11 +107,29 @@ private static Map<String, String> getParams(GetRequest getRequest) {
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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@
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;
Expand Down Expand Up @@ -92,6 +94,22 @@ public void existsAsync(GetRequest getRequest, ActionListener<Boolean> listener,
Collections.emptySet(), headers);
}

/**
* Deletes a document by id using the delete api
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"Deletes a document using the Delete API" with a link to the documentation like it is done for other methods

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right. I did not notice the modifications when I merged the master branch into my branch.

*/
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) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here for javadoc

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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@
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;
Expand Down Expand Up @@ -141,4 +144,40 @@ public void testGet() throws IOException {
assertEquals("value1", sourceAsMap.get("field1"));
}
}

public void testDelete() throws IOException {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you move this up so that it is alphabetically sorted?

{
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());
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is surprising to me. I'd expect it to be -1 similar to the GET request

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think Get request returns an error with NOT FOUND response code in this case? Which make more sense to me.

Anyway a delete increments the version even if the document is not found, and is kept in the versions map as a tombstone.

}
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());
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we could start using our own high level rest client for those? Now we have Index API support...

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed.

{
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());
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't see tests for routing, parent, version type etc. That would be nice to check that parameters are correctly propagated.

}
}
}