Document APIs - Delete By Query API

优质
小牛编辑
129浏览
2023-12-01

Delete By Query API

通过查询条件删除

  1. BulkByScrollResponse response =
  2. DeleteByQueryAction.INSTANCE.newRequestBuilder(client)
  3. .filter(QueryBuilders.matchQuery("gender", "male")) //查询条件
  4. .source("persons") //index(索引名)
  5. .get(); //执行
  6. long deleted = response.getDeleted(); //删除文档的数量

如果需要执行的时间比较长,可以使用异步的方式处理,结果在回调里面获取

  1. DeleteByQueryAction.INSTANCE.newRequestBuilder(client)
  2. .filter(QueryBuilders.matchQuery("gender", "male")) //查询
  3. .source("persons") //index(索引名)
  4. .execute(new ActionListener<BulkByScrollResponse>() { //回调监听
  5. @Override
  6. public void onResponse(BulkByScrollResponse response) {
  7. long deleted = response.getDeleted(); //删除文档的数量
  8. }
  9. @Override
  10. public void onFailure(Exception e) {
  11. // Handle the exception
  12. }
  13. });