Delete API
editDelete API
editDelete Request
editA DeleteRequest
has two required arguments:
Optional arguments
editThe following arguments can optionally be provided:
Timeout to wait for primary shard to become available as a |
|
Timeout to wait for primary shard to become available as a |
Synchronous execution
editWhen executing a DeleteRequest
in the following manner, the client waits
for the DeleteResponse
to be returned before continuing with code execution:
DeleteResponse deleteResponse = client.delete( request, RequestOptions.DEFAULT);
Synchronous calls may throw an IOException
in case of either failing to
parse the REST response in the high-level REST client, the request times out
or similar cases where there is no response coming back from the server.
In cases where the server returns a 4xx
or 5xx
error code, the high-level
client tries to parse the response body error details instead and then throws
a generic ElasticsearchException
and adds the original ResponseException
as a
suppressed exception to it.
Asynchronous execution
editExecuting a DeleteRequest
can also be done in an asynchronous fashion so that
the client can return directly. Users need to specify how the response or
potential failures will be handled by passing the request and a listener to the
asynchronous delete method:
The asynchronous method does not block and returns immediately. Once it is
completed the ActionListener
is called back using the onResponse
method
if the execution successfully completed or using the onFailure
method if
it failed. Failure scenarios and expected exceptions are the same as in the
synchronous execution case.
A typical listener for delete
looks like:
Delete Response
editThe returned DeleteResponse
allows to retrieve information about the executed
operation as follows:
String index = deleteResponse.getIndex(); String id = deleteResponse.getId(); long version = deleteResponse.getVersion(); ReplicationResponse.ShardInfo shardInfo = deleteResponse.getShardInfo(); if (shardInfo.getTotal() != shardInfo.getSuccessful()) { } if (shardInfo.getFailed() > 0) { for (ReplicationResponse.ShardInfo.Failure failure : shardInfo.getFailures()) { String reason = failure.reason(); } }
Handle the situation where number of successful shards is less than total shards |
|
Handle the potential failures |
It is also possible to check whether the document was found or not:
DeleteRequest request = new DeleteRequest("posts", "does_not_exist"); DeleteResponse deleteResponse = client.delete( request, RequestOptions.DEFAULT); if (deleteResponse.getResult() == DocWriteResponse.Result.NOT_FOUND) { }
If there is a version conflict, an ElasticsearchException
will
be thrown: