Skip to content

Commit 33e25b5

Browse files
committed
Added minimal docs for reindex api in java-api docs
Additionally: * Included the existing update by query java api docs in java-api docs. (for some reason it was never included, it needed some tweaking and then it was good to go) * moved delete-by-query / update-by-query code samples to java file so that we can verify that these samples at least compile. Closes #24203
1 parent 0a70865 commit 33e25b5

File tree

6 files changed

+255
-117
lines changed

6 files changed

+255
-117
lines changed

docs/java-api/docs.asciidoc

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,14 @@ This section describes the following CRUD APIs:
77
* <<java-docs-index>>
88
* <<java-docs-get>>
99
* <<java-docs-delete>>
10-
* <<java-docs-delete-by-query>>
1110
* <<java-docs-update>>
1211

1312
.Multi-document APIs
1413
* <<java-docs-multi-get>>
1514
* <<java-docs-bulk>>
15+
* <<java-docs-reindex>>
16+
* <<java-docs-update-by-query>>
17+
* <<java-docs-delete-by-query>>
1618

1719
NOTE: All CRUD APIs are single-index APIs. The `index` parameter accepts a single
1820
index name, or an `alias` which points to a single index.
@@ -28,3 +30,7 @@ include::docs/update.asciidoc[]
2830
include::docs/multi-get.asciidoc[]
2931

3032
include::docs/bulk.asciidoc[]
33+
34+
include::docs/update-by-query.asciidoc[]
35+
36+
include::docs/reindex.asciidoc[]

docs/java-api/docs/delete.asciidoc

Lines changed: 4 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -42,15 +42,9 @@ DeleteResponse response = client.prepareDelete("twitter", "tweet", "1")
4242
The delete by query API allows one to delete a given set of documents based on
4343
the result of a query:
4444

45-
[source,java]
45+
["source","java",subs="attributes,callouts,macros"]
4646
--------------------------------------------------
47-
BulkByScrollResponse response =
48-
DeleteByQueryAction.INSTANCE.newRequestBuilder(client)
49-
.filter(QueryBuilders.matchQuery("gender", "male")) <1>
50-
.source("persons") <2>
51-
.get(); <3>
52-
53-
long deleted = response.getDeleted(); <4>
47+
include-tagged::{client-reindex-tests}/ReindexDocumentationIT.java[delete-by-query-sync]
5448
--------------------------------------------------
5549
<1> query
5650
<2> index
@@ -60,21 +54,9 @@ long deleted = response.getDeleted(); <4>
6054
As it can be a long running operation, if you wish to do it asynchronously, you can call `execute` instead of `get`
6155
and provide a listener like:
6256

63-
[source,java]
57+
["source","java",subs="attributes,callouts,macros"]
6458
--------------------------------------------------
65-
DeleteByQueryAction.INSTANCE.newRequestBuilder(client)
66-
.filter(QueryBuilders.matchQuery("gender", "male")) <1>
67-
.source("persons") <2>
68-
.execute(new ActionListener<BulkByScrollResponse>() { <3>
69-
@Override
70-
public void onResponse(BulkByScrollResponse response) {
71-
long deleted = response.getDeleted(); <4>
72-
}
73-
@Override
74-
public void onFailure(Exception e) {
75-
// Handle the exception
76-
}
77-
});
59+
include-tagged::{client-reindex-tests}/ReindexDocumentationIT.java[delete-by-query-async]
7860
--------------------------------------------------
7961
<1> query
8062
<2> index

docs/java-api/docs/reindex.asciidoc

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
[[java-docs-reindex]]
2+
=== Reindex API
3+
4+
See {ref}/docs-reindex.html[reindex API].
5+
6+
["source","java",subs="attributes,callouts,macros"]
7+
--------------------------------------------------
8+
include-tagged::{client-reindex-tests}/ReindexDocumentationIT.java[reindex1]
9+
--------------------------------------------------
10+
<1> Optionally a query can provided to filter what documents should be
11+
re-indexed from the source to the target index.

docs/java-api/docs/update-by-query.asciidoc

Lines changed: 37 additions & 94 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,13 @@
1-
[[docs-update-by-query]]
2-
== Update By Query API
1+
[[java-docs-update-by-query]]
2+
=== Update By Query API
33

44
The simplest usage of `updateByQuery` updates each
55
document in an index without changing the source. This usage enables
6-
<<picking-up-a-new-property,picking up a new property>> or another online
7-
mapping change.
6+
picking up a new property or another online mapping change.
87

9-
[source,java]
8+
["source","java",subs="attributes,callouts,macros"]
109
--------------------------------------------------
11-
UpdateByQueryRequestBuilder updateByQuery = UpdateByQueryAction.INSTANCE.newRequestBuilder(client);
12-
13-
updateByQuery.source("source_index").abortOnVersionConflict(false);
14-
15-
BulkByScrollResponse response = updateByQuery.get();
10+
include-tagged::{client-reindex-tests}/ReindexDocumentationIT.java[update-by-query]
1611
--------------------------------------------------
1712

1813
Calls to the `updateByQuery` API start by getting a snapshot of the index, indexing
@@ -41,78 +36,50 @@ The `UpdateByQueryRequestBuilder` API supports filtering the updated documents,
4136
limiting the total number of documents to update, and updating documents
4237
with a script:
4338

44-
[source,java]
45-
--------------------------------------------------
46-
UpdateByQueryRequestBuilder updateByQuery = UpdateByQueryAction.INSTANCE.newRequestBuilder(client);
4739

48-
updateByQuery.source("source_index")
49-
.filter(termQuery("level", "awesome"))
50-
.size(1000)
51-
.script(new Script("ctx._source.awesome = 'absolutely'", ScriptType.INLINE, "painless", emptyMap()));
52-
53-
BulkByScrollResponse response = updateByQuery.get();
40+
["source","java",subs="attributes,callouts,macros"]
41+
--------------------------------------------------
42+
include-tagged::{client-reindex-tests}/ReindexDocumentationIT.java[update-by-query-filter]
5443
--------------------------------------------------
5544

5645
`UpdateByQueryRequestBuilder` also enables direct access to the query used
5746
to select the documents. You can use this access to change the default scroll size or
5847
otherwise modify the request for matching documents.
5948

60-
[source,java]
49+
["source","java",subs="attributes,callouts,macros"]
6150
--------------------------------------------------
62-
UpdateByQueryRequestBuilder updateByQuery = UpdateByQueryAction.INSTANCE.newRequestBuilder(client);
63-
64-
updateByQuery.source("source_index")
65-
.source().setSize(500);
66-
67-
BulkByScrollResponse response = updateByQuery.get();
51+
include-tagged::{client-reindex-tests}/ReindexDocumentationIT.java[update-by-query-size]
6852
--------------------------------------------------
6953

7054
You can also combine `size` with sorting to limit the documents updated:
7155

72-
[source,java]
56+
["source","java",subs="attributes,callouts,macros"]
7357
--------------------------------------------------
74-
UpdateByQueryRequestBuilder updateByQuery = UpdateByQueryAction.INSTANCE.newRequestBuilder(client);
75-
76-
updateByQuery.source("source_index").size(100)
77-
.source().addSort("cat", SortOrder.DESC);
78-
79-
BulkByScrollResponse response = updateByQuery.get();
58+
include-tagged::{client-reindex-tests}/ReindexDocumentationIT.java[update-by-query-sort]
8059
--------------------------------------------------
8160

8261
In addition to changing the `_source` field for the document, you can use a
8362
script to change the action, similar to the Update API:
8463

85-
[source,java]
64+
["source","java",subs="attributes,callouts,macros"]
8665
--------------------------------------------------
87-
UpdateByQueryRequestBuilder updateByQuery = UpdateByQueryAction.INSTANCE.newRequestBuilder(client);
88-
89-
updateByQuery.source("source_index")
90-
.script(new Script(
91-
"if (ctx._source.awesome == 'absolutely) {"
92-
+ " ctx.op='noop'
93-
+ "} else if (ctx._source.awesome == 'lame') {"
94-
+ " ctx.op='delete'"
95-
+ "} else {"
96-
+ "ctx._source.awesome = 'absolutely'}", ScriptType.INLINE, "painless", emptyMap()));
97-
98-
BulkByScrollResponse response = updateByQuery.get();
66+
include-tagged::{client-reindex-tests}/ReindexDocumentationIT.java[update-by-query-script]
9967
--------------------------------------------------
10068

101-
As in the <<docs-update,Update API>>, you can set the value of `ctx.op` to change the
69+
As in the <<java-docs-update,Update API>>, you can set the value of `ctx.op` to change the
10270
operation that executes:
10371

10472
`noop`::
10573

10674
Set `ctx.op = "noop"` if your script doesn't make any
10775
changes. The `updateByQuery` operaton then omits that document from the updates.
108-
This behavior increments the `noop` counter in the
109-
<<docs-update-by-query-response-body, response body>>.
76+
This behavior increments the `noop` counter in the response body.
11077

11178
`delete`::
11279

11380
Set `ctx.op = "delete"` if your script decides that the document must be
11481
deleted. The deletion will be reported in the `deleted` counter in the
115-
<<docs-update-by-query-response-body, response body>>.
82+
response body.
11683

11784
Setting `ctx.op` to any other value generates an error. Setting any
11885
other field in `ctx` generates an error.
@@ -123,79 +90,55 @@ from its original location.
12390

12491
You can also perform these operations on multiple indices and types at once, similar to the search API:
12592

126-
[source,java]
93+
["source","java",subs="attributes,callouts,macros"]
12794
--------------------------------------------------
128-
UpdateByQueryRequestBuilder updateByQuery = UpdateByQueryAction.INSTANCE.newRequestBuilder(client);
129-
130-
updateByQuery.source("foo", "bar").source().setTypes("a", "b");
131-
132-
BulkByScrollResponse response = updateByQuery.get();
95+
include-tagged::{client-reindex-tests}/ReindexDocumentationIT.java[update-by-query-multi-index]
13396
--------------------------------------------------
13497

13598
If you provide a `routing` value then the process copies the routing value to the scroll query,
13699
limiting the process to the shards that match that routing value:
137100

138-
[source,java]
101+
["source","java",subs="attributes,callouts,macros"]
139102
--------------------------------------------------
140-
UpdateByQueryRequestBuilder updateByQuery = UpdateByQueryAction.INSTANCE.newRequestBuilder(client);
141-
142-
updateByQuery.source().setRouting("cat");
143-
144-
BulkByScrollResponse response = updateByQuery.get();
103+
include-tagged::{client-reindex-tests}/ReindexDocumentationIT.java[update-by-query-routing]
145104
--------------------------------------------------
146105

147-
`updateByQuery` can also use the <<ingest>> feature by
106+
`updateByQuery` can also use the ingest node by
148107
specifying a `pipeline` like this:
149108

150-
[source,java]
109+
["source","java",subs="attributes,callouts,macros"]
151110
--------------------------------------------------
152-
UpdateByQueryRequestBuilder updateByQuery = UpdateByQueryAction.INSTANCE.newRequestBuilder(client);
153-
154-
updateByQuery.setPipeline("hurray");
155-
156-
BulkByScrollResponse response = updateByQuery.get();
111+
include-tagged::{client-reindex-tests}/ReindexDocumentationIT.java[update-by-query-pipeline]
157112
--------------------------------------------------
158113

159114
[float]
160-
[[docs-update-by-query-task-api]]
115+
[[java-docs-update-by-query-task-api]]
161116
=== Works with the Task API
162117

163-
You can fetch the status of all running update-by-query requests with the
164-
<<tasks,Task API>>:
118+
You can fetch the status of all running update-by-query requests with the Task API:
165119

166-
[source,java]
120+
["source","java",subs="attributes,callouts,macros"]
167121
--------------------------------------------------
168-
ListTasksResponse tasksList = client.admin().cluster().prepareListTasks()
169-
.setActions(UpdateByQueryAction.NAME).setDetailed(true).get();
170-
171-
for (TaskInfo info: tasksList.getTasks()) {
172-
TaskId taskId = info.getTaskId();
173-
BulkByScrollTask.Status status = (BulkByScrollTask.Status) info.getStatus();
174-
// do stuff
175-
}
176-
122+
include-tagged::{client-reindex-tests}/ReindexDocumentationIT.java[update-by-query-list-tasks]
177123
--------------------------------------------------
178124

179125
With the `TaskId` shown above you can look up the task directly:
180126

181127
// provide API Example
182-
[source,java]
128+
["source","java",subs="attributes,callouts,macros"]
183129
--------------------------------------------------
184-
GetTaskResponse get = client.admin().cluster().prepareGetTask(taskId).get();
130+
include-tagged::{client-reindex-tests}/ReindexDocumentationIT.java[update-by-query-get-task]
185131
--------------------------------------------------
186132

187133
[float]
188-
[[docs-update-by-query-cancel-task-api]]
134+
[[java-docs-update-by-query-cancel-task-api]]
189135
=== Works with the Cancel Task API
190136

191-
Any Update By Query can be canceled using the <<tasks,Task Cancel API>>:
137+
Any Update By Query can be canceled using the Task Cancel API:
192138

193-
[source,java]
139+
["source","java",subs="attributes,callouts,macros"]
194140
--------------------------------------------------
195-
// Cancel all update-by-query requests
196-
client.admin().cluster().prepareCancelTasks().setActions(UpdateByQueryAction.NAME).get().getTasks()
197-
// Cancel a specific update-by-query request
198-
client.admin().cluster().prepareCancelTasks().setTaskId(taskId).get().getTasks()
141+
include-tagged::{client-reindex-tests}/ReindexDocumentationIT.java[update-by-query-cancel-task]
199142
--------------------------------------------------
200143

201144
Use the `list tasks` API to find the value of `taskId`.
@@ -204,14 +147,14 @@ Cancelling a request is typically a very fast process but can take up to a few s
204147
The task status API continues to list the task until the cancellation is complete.
205148

206149
[float]
207-
[[docs-update-by-query-rethrottle]]
150+
[[java-docs-update-by-query-rethrottle]]
208151
=== Rethrottling
209152

210153
Use the `_rethrottle` API to change the value of `requests_per_second` on a running update:
211154

212-
[source,java]
155+
["source","java",subs="attributes,callouts,macros"]
213156
--------------------------------------------------
214-
RethrottleAction.INSTANCE.newRequestBuilder(client).setTaskId(taskId).setRequestsPerSecond(2.0f).get();
157+
include-tagged::{client-reindex-tests}/ReindexDocumentationIT.java[update-by-query-rethrottle]
215158
--------------------------------------------------
216159

217160
Use the `list tasks` API to find the value of `taskId`.

docs/java-api/index.asciidoc

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,8 @@ and add it as a dependency. As an example, we will use the `slf4j-simple` logger
152152

153153
:client-tests: {docdir}/../../server/src/test/java/org/elasticsearch/client/documentation
154154

155+
:client-reindex-tests: {docdir}/../../modules/reindex/src/test/java/org/elasticsearch/client/documentation
156+
155157
include::client.asciidoc[]
156158

157159
include::docs.asciidoc[]

0 commit comments

Comments
 (0)