Skip to content

Commit cf60e93

Browse files
authored
Docs: HighLevelRestClient#exists (#29073)
Docs: HighLevelRestClient#exists Add documentation for `HighLevelRestClient#exists`. Relates to #28389
1 parent 404e776 commit cf60e93

File tree

4 files changed

+105
-0
lines changed

4 files changed

+105
-0
lines changed

client/rest-high-level/src/test/java/org/elasticsearch/client/documentation/CRUDDocumentationIT.java

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -932,6 +932,49 @@ public void onFailure(Exception e) {
932932
}
933933
}
934934

935+
public void testExists() throws Exception {
936+
RestHighLevelClient client = highLevelClient();
937+
// tag::exists-request
938+
GetRequest getRequest = new GetRequest(
939+
"posts", // <1>
940+
"doc", // <2>
941+
"1"); // <3>
942+
getRequest.fetchSourceContext(new FetchSourceContext(false)); // <4>
943+
getRequest.storedFields("_none_"); // <5>
944+
// end::exists-request
945+
{
946+
// tag::exists-execute
947+
boolean exists = client.exists(getRequest);
948+
// end::exists-execute
949+
assertFalse(exists);
950+
}
951+
{
952+
// tag::exists-execute-listener
953+
ActionListener<Boolean> listener = new ActionListener<Boolean>() {
954+
@Override
955+
public void onResponse(Boolean exists) {
956+
// <1>
957+
}
958+
959+
@Override
960+
public void onFailure(Exception e) {
961+
// <2>
962+
}
963+
};
964+
// end::exists-execute-listener
965+
966+
// Replace the empty listener by a blocking listener in test
967+
final CountDownLatch latch = new CountDownLatch(1);
968+
listener = new LatchedActionListener<>(listener, latch);
969+
970+
// tag::exists-execute-async
971+
client.existsAsync(getRequest, listener); // <1>
972+
// end::exists-execute-async
973+
974+
assertTrue(latch.await(30L, TimeUnit.SECONDS));
975+
}
976+
}
977+
935978
public void testBulkProcessor() throws InterruptedException {
936979
RestHighLevelClient client = highLevelClient();
937980
{
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
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.

docs/java-rest/high-level/document/get.asciidoc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ include-tagged::{doc-tests}/CRUDDocumentationIT.java[get-request]
1414
<2> Type
1515
<3> Document id
1616

17+
[[java-rest-high-document-get-request-optional-arguments]]
1718
==== Optional arguments
1819
The following arguments can optionally be provided:
1920

docs/java-rest/high-level/supported-apis.asciidoc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ Multi-document APIs::
1717

1818
include::document/index.asciidoc[]
1919
include::document/get.asciidoc[]
20+
include::document/exists.asciidoc[]
2021
include::document/delete.asciidoc[]
2122
include::document/update.asciidoc[]
2223
include::document/bulk.asciidoc[]

0 commit comments

Comments
 (0)