exists.asciidoc 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. [[java-rest-high-document-exists]]
  2. === Exists API
  3. The exists API returns `true` if a document exists, and `false` otherwise.
  4. [[java-rest-high-document-exists-request]]
  5. ==== Exists Request
  6. It uses `GetRequest` just like the <<java-rest-high-document-get>>.
  7. All of its <<java-rest-high-document-get-request-optional-arguments, optional arguments>>
  8. are supported. Since `exists()` only returns `true` or `false`, we recommend
  9. turning off fetching `_source` and any stored fields so the request is
  10. slightly lighter:
  11. ["source","java",subs="attributes,callouts,macros"]
  12. --------------------------------------------------
  13. include-tagged::{doc-tests}/CRUDDocumentationIT.java[exists-request]
  14. --------------------------------------------------
  15. <1> Index
  16. <2> Type
  17. <3> Document id
  18. <4> Disable fetching `_source`.
  19. <5> Disable fetching stored fields.
  20. [[java-rest-high-document-exists-sync]]
  21. ==== Synchronous Execution
  22. ["source","java",subs="attributes,callouts,macros"]
  23. --------------------------------------------------
  24. include-tagged::{doc-tests}/CRUDDocumentationIT.java[exists-execute]
  25. --------------------------------------------------
  26. [[java-rest-high-document-exists-async]]
  27. ==== Asynchronous Execution
  28. The asynchronous execution of exists request requires both the `GetRequest`
  29. instance and an `ActionListener` instance to be passed to the asynchronous
  30. method:
  31. ["source","java",subs="attributes,callouts,macros"]
  32. --------------------------------------------------
  33. include-tagged::{doc-tests}/CRUDDocumentationIT.java[exists-execute-async]
  34. --------------------------------------------------
  35. <1> The `GetRequest` to execute and the `ActionListener` to use when
  36. the execution completes.
  37. The asynchronous method does not block and returns immediately. Once it is
  38. completed the `ActionListener` is called back using the `onResponse` method
  39. if the execution successfully completed or using the `onFailure` method if
  40. it failed.
  41. A typical listener for `GetResponse` looks like:
  42. ["source","java",subs="attributes,callouts,macros"]
  43. --------------------------------------------------
  44. include-tagged::{doc-tests}/CRUDDocumentationIT.java[exists-execute-listener]
  45. --------------------------------------------------
  46. <1> Called when the execution is successfully completed. The response is
  47. provided as an argument.
  48. <2> Called in case of failure. The raised exception is provided as an argument.