93 lines
2.7 KiB
Plaintext
93 lines
2.7 KiB
Plaintext
|
[[java-rest-high-document-delete]]
|
||
|
=== Delete API
|
||
|
|
||
|
[[java-rest-high-document-delete-request]]
|
||
|
==== Delete Request
|
||
|
|
||
|
The most simple Delete Request needs is:
|
||
|
|
||
|
[source,java]
|
||
|
--------------------------------------------------
|
||
|
DeleteRequest request = new DeleteRequest(
|
||
|
"index", <1>
|
||
|
"type", <2>
|
||
|
"id"); <3>
|
||
|
--------------------------------------------------
|
||
|
<1> Index name
|
||
|
<2> Type
|
||
|
<3> Document id
|
||
|
|
||
|
You can also provide the following properties:
|
||
|
|
||
|
[source,java]
|
||
|
--------------------------------------------------
|
||
|
request.timeout(TimeValue.timeValueSeconds(1)); <1>
|
||
|
request.timeout("1s"); <2>
|
||
|
request.setRefreshPolicy(WriteRequest.RefreshPolicy.WAIT_UNTIL); <3>
|
||
|
request.setRefreshPolicy("wait_for"); <4>
|
||
|
request.version(2); <5>
|
||
|
request.versionType(VersionType.EXTERNAL); <6>
|
||
|
--------------------------------------------------
|
||
|
<1> Timeout
|
||
|
<2> Timeout as String
|
||
|
<3> Refresh policy
|
||
|
<4> Refresh policy as String
|
||
|
<5> Version
|
||
|
<6> Version type
|
||
|
|
||
|
[[java-rest-high-document-delete-sync]]
|
||
|
==== Execution
|
||
|
|
||
|
[source,java]
|
||
|
--------------------------------------------------
|
||
|
DeleteResponse response = client.delete(request);
|
||
|
--------------------------------------------------
|
||
|
|
||
|
[[java-rest-high-document-delete-async]]
|
||
|
==== Asynchronous Execution
|
||
|
|
||
|
[source,java]
|
||
|
--------------------------------------------------
|
||
|
client.deleteAsync(request, new ActionListener<DeleteResponse>() {
|
||
|
@Override
|
||
|
public void onResponse(DeleteResponse deleteResponse) {
|
||
|
<1>
|
||
|
}
|
||
|
|
||
|
@Override
|
||
|
public void onFailure(Exception e) {
|
||
|
<2>
|
||
|
}
|
||
|
});
|
||
|
--------------------------------------------------
|
||
|
<1> Implement if needed when execution did not throw an exception
|
||
|
<2> Implement if needed in case of failure
|
||
|
|
||
|
[[java-rest-high-document-delete-response]]
|
||
|
==== Delete Response
|
||
|
|
||
|
In the Delete Response object, you can check for example the result of the operation:
|
||
|
|
||
|
[source,java]
|
||
|
--------------------------------------------------
|
||
|
if (response.getResult().equals(DocWriteResponse.Result.NOT_FOUND)) {
|
||
|
<1>
|
||
|
}
|
||
|
--------------------------------------------------
|
||
|
<1> Do something if we did not find the document which should have been deleted
|
||
|
|
||
|
Note that if you have a version conflict because you defined the version within the
|
||
|
<<java-rest-high-document-delete-request>>, it will raise an `ElasticsearchException` like:
|
||
|
|
||
|
[source,java]
|
||
|
--------------------------------------------------
|
||
|
try {
|
||
|
client.delete(request);
|
||
|
} catch (ElasticsearchException exception) {
|
||
|
if (exception.status().equals(RestStatus.CONFLICT) {
|
||
|
<1>
|
||
|
}
|
||
|
}
|
||
|
--------------------------------------------------
|
||
|
<1> We got a version conflict
|