Delete API
Delete Request
DeleteRequest request = new DeleteRequest(
"posts",
"doc",
"1");
可选参数
request.routing("routing");
request.parent("parent");
request.timeout(TimeValue.timeValueMinutes(2));
request.timeout("2m");
request.setRefreshPolicy(WriteRequest.RefreshPolicy.WAIT_UNTIL);
request.setRefreshPolicy("wait_for");
request.version(2);
request.versionType(VersionType.EXTERNAL);
同步请求
DeleteResponse deleteResponse = client.delete(
request, RequestOptions.DEFAULT);
异步请求
client.deleteAsync(request, RequestOptions.DEFAULT, listener);
Delete Response
String index = deleteResponse.getIndex();
String type = deleteResponse.getType();
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();
}
}
可能查核该文件是否已找到:
DeleteRequest request = new DeleteRequest("posts", "doc", "does_not_exist");
DeleteResponse deleteResponse = client.delete(
request, RequestOptions.DEFAULT);
if (deleteResponse.getResult() == DocWriteResponse.Result.NOT_FOUND) {
//如果要删除的文档没有找到,请执行什么操作
}
如果存在版本冲突,将抛出ElasticsearchException:
try {
DeleteResponse deleteResponse = client.delete(
new DeleteRequest("posts", "doc", "1").version(2),
RequestOptions.DEFAULT);
} catch (ElasticsearchException exception) {
if (exception.status() == RestStatus.CONFLICT) {
}
}