delete.asciidoc 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. [[java-docs-delete]]
  2. === Delete API
  3. The delete API allows one to delete a typed JSON document from a specific
  4. index based on its id. The following example deletes the JSON document
  5. from an index called twitter, under a type called tweet, with id valued
  6. 1:
  7. [source,java]
  8. --------------------------------------------------
  9. DeleteResponse response = client.prepareDelete("twitter", "tweet", "1").get();
  10. --------------------------------------------------
  11. For more information on the delete operation, check out the
  12. {ref}/docs-delete.html[delete API] docs.
  13. [[java-docs-delete-by-query]]
  14. === Delete By Query API
  15. The delete by query API allows one to delete a given set of documents based on
  16. the result of a query:
  17. [source,java]
  18. --------------------------------------------------
  19. BulkByScrollResponse response =
  20. DeleteByQueryAction.INSTANCE.newRequestBuilder(client)
  21. .filter(QueryBuilders.matchQuery("gender", "male")) <1>
  22. .source("persons") <2>
  23. .get(); <3>
  24. long deleted = response.getDeleted(); <4>
  25. --------------------------------------------------
  26. <1> query
  27. <2> index
  28. <3> execute the operation
  29. <4> number of deleted documents
  30. As it can be a long running operation, if you wish to do it asynchronously, you can call `execute` instead of `get`
  31. and provide a listener like:
  32. [source,java]
  33. --------------------------------------------------
  34. DeleteByQueryAction.INSTANCE.newRequestBuilder(client)
  35. .filter(QueryBuilders.matchQuery("gender", "male")) <1>
  36. .source("persons") <2>
  37. .execute(new ActionListener<BulkByScrollResponse>() { <3>
  38. @Override
  39. public void onResponse(BulkByScrollResponse response) {
  40. long deleted = response.getDeleted(); <4>
  41. }
  42. @Override
  43. public void onFailure(Exception e) {
  44. // Handle the exception
  45. }
  46. });
  47. --------------------------------------------------
  48. <1> query
  49. <2> index
  50. <3> listener
  51. <4> number of deleted documents