一、根据查询结果删除(Delete By Query API)
BulkByScrollResponse response =
DeleteByQueryAction.INSTANCE.newRequestBuilder(client) // transport client
.filter(QueryBuilders.matchQuery("gender", "male")) //查询条件
.source("persons") //索引名称
.get();
long deleted = response.getDeleted(); //已删除的文档数量
二、对查询结果异步删除
DeleteByQueryAction.INSTANCE.newRequestBuilder(client)
.filter(QueryBuilders.matchQuery("gender", "male"))
.source("persons")
.execute(new ActionListener<BulkByScrollResponse>() {
@Override
public void onResponse(BulkByScrollResponse response) {
long deleted = response.getDeleted();
}
@Override
public void onFailure(Exception e) {
// Handle the exception
}
});