-
Notifications
You must be signed in to change notification settings - Fork 24.7k
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
Changes from 1 commit
7df1df2
0bbcd94
14f0490
5aa2ab3
b2ec4c1
efa28e0
0ee74f9
bf8241e
9bde68e
b680e62
4ebc6dd
3e4b917
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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; | ||
|
@@ -92,6 +94,22 @@ public void existsAsync(GetRequest getRequest, ActionListener<Boolean> listener, | |
Collections.emptySet(), headers); | ||
} | ||
|
||
/** | ||
* Deletes a document by id using the delete api | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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; | ||
|
@@ -141,4 +144,40 @@ public void testGet() throws IOException { | |
assertEquals("value1", sourceAsMap.get("field1")); | ||
} | ||
} | ||
|
||
public void testDelete() throws IOException { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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()); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is surprising to me. I'd expect it to be There was a problem hiding this comment. Choose a reason for hiding this commentThe 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()); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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... There was a problem hiding this comment. Choose a reason for hiding this commentThe 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()); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. |
||
} | ||
} | ||
} |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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