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