Skip to content

Commit 7bf9651

Browse files
committed
[DOCS] Reformat update API reference. (#45423)
* [DOCS] Reformat update API reference.
1 parent 4ffb48d commit 7bf9651

File tree

2 files changed

+112
-104
lines changed

2 files changed

+112
-104
lines changed

docs/reference/docs/index_.asciidoc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ using a PUT request. Omit to automatically generate an ID when using a
3434
POST request.
3535

3636

37-
[[docs--api-query-params]]
37+
[[docs-index-api-query-params]]
3838
==== {api-query-parms-title}
3939

4040
include::{docdir}/rest-api/common-parms.asciidoc[tag=doc-seq-no]

docs/reference/docs/update.asciidoc

Lines changed: 111 additions & 103 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,86 @@
11
[[docs-update]]
22
=== Update API
3+
++++
4+
<titleabbrev>Update</titleabbrev>
5+
++++
36

4-
The update API allows to update a document based on a script provided.
5-
The operation gets the document (collocated with the shard) from the
6-
index, runs the script (with optional script language and parameters),
7-
and indexes back the result (also allows to delete, or ignore the
8-
operation).
7+
Updates a document using the specified script.
98

10-
Note, this operation still means full reindex of the document, it just
11-
removes some network roundtrips and reduces chances of version conflicts
12-
between the get and the index. The `_source` field needs to be enabled
13-
for this feature to work.
9+
[[docs-update-api-request]]
10+
==== {api-request-title}
1411

15-
For example, let's index a simple doc:
12+
`POST /<index/_update/<_id>`
13+
14+
[[update-api-desc]]
15+
==== {api-description-title}
16+
17+
Enables you script document updates. The script can update, delete, or skip
18+
modifying the document. The update API also supports passing a partial document,
19+
which is merged into the existing document. To fully replace an existing
20+
document, use the <<docs-index_,`index` API>>.
21+
22+
This operation:
23+
24+
. Gets the document (collocated with the shard) from the index.
25+
. Runs the specified script.
26+
. Indexes the result.
27+
28+
The document must still be reindexed, but using `update` removes some network
29+
roundtrips and reduces chances of version conflicts between the GET and the
30+
index operation.
31+
32+
The `_source` field must be enabled to use `update`. In addition to `_source`,
33+
you can access the following variables through the `ctx` map: `_index`,
34+
`_type`, `_id`, `_version`, `_routing`, and `_now` (the current timestamp).
35+
36+
[[docs-update-api-path-params]]
37+
==== {api-path-parms-title}
38+
39+
`<index>`::
40+
(Required, string) Name of the target index. By default, the index is created
41+
automatically if it doesn't exist. For more information, see <<index-creation>>.
42+
43+
`<_id>`::
44+
(Required, string) Unique identifier for the document to be updated.
45+
46+
[[docs-update-api-query-params]]
47+
==== {api-query-parms-title}
48+
49+
include::{docdir}/rest-api/common-parms.asciidoc[tag=doc-seq-no]
50+
51+
include::{docdir}/rest-api/common-parms.asciidoc[tag=doc-primary-term]
52+
53+
`lang`::
54+
(Optional, string) The script language. Default: `painless`.
55+
56+
include::{docdir}/rest-api/common-parms.asciidoc[tag=doc-refresh]
57+
58+
`retry_on_conflict`::
59+
(Optional, integer) Specify how many times should the operation be retried when
60+
a conflict occurs. Default: 0.
61+
62+
include::{docdir}/rest-api/common-parms.asciidoc[tag=doc-refresh]
63+
64+
include::{docdir}/rest-api/common-parms.asciidoc[tag=doc-routing]
65+
66+
`_source`::
67+
(Optional, list) Set to `false` to disable source retrieval (default: `true`).
68+
You can also specify a comma-separated list of the fields you want to retrieve.
69+
70+
`_source_excludes`::
71+
(Optional, list) Specify the source fields you want to exclude.
72+
73+
`_source_includes`::
74+
(Optional, list) Specify the source fields you want to retrieve.
75+
76+
include::{docdir}/rest-api/common-parms.asciidoc[tag=timeoutparms]
77+
78+
include::{docdir}/rest-api/common-parms.asciidoc[tag=doc-wait-for-active-shards]
79+
80+
[[update-api-example]]
81+
==== {api-examples-title}
82+
83+
First, let's index a simple doc:
1684

1785
[source,js]
1886
--------------------------------------------------
@@ -24,10 +92,8 @@ PUT test/_doc/1
2492
--------------------------------------------------
2593
// CONSOLE
2694

27-
[float]
28-
==== Scripted updates
29-
30-
Now, we can execute a script that would increment the counter:
95+
To increment the counter, you can submit an update request with the
96+
following script:
3197

3298
[source,js]
3399
--------------------------------------------------
@@ -45,8 +111,8 @@ POST test/_update/1
45111
// CONSOLE
46112
// TEST[continued]
47113

48-
We can add a tag to the list of tags (if the tag exists, it
49-
still gets added, since this is a list):
114+
Similarly, you could use and update script to add a tag to the list of tags
115+
(this is just a list, so the tag is added even it exists):
50116

51117
[source,js]
52118
--------------------------------------------------
@@ -64,11 +130,11 @@ POST test/_update/1
64130
// CONSOLE
65131
// TEST[continued]
66132

67-
We can remove a tag from the list of tags. Note that the Painless function to
68-
`remove` a tag takes as its parameter the array index of the element you wish
69-
to remove, so you need a bit more logic to locate it while avoiding a runtime
70-
error. Note that if the tag was present more than once in the list, this will
71-
remove only one occurrence of it:
133+
You could also remove a tag from the list of tags. The Painless
134+
function to `remove` a tag takes the array index of the element
135+
you want to remove. To avoid a possible runtime error, you first need to
136+
make sure the tag exists. If the list contains duplicates of the tag, this
137+
script just removes one occurrence.
72138

73139
[source,js]
74140
--------------------------------------------------
@@ -86,11 +152,8 @@ POST test/_update/1
86152
// CONSOLE
87153
// TEST[continued]
88154

89-
In addition to `_source`, the following variables are available through
90-
the `ctx` map: `_index`, `_type`, `_id`, `_version`, `_routing`,
91-
and `_now` (the current timestamp).
92-
93-
We can also add a new field to the document:
155+
You can also add and remove fields from a document. For example, this script
156+
adds the field `new_field`:
94157

95158
[source,js]
96159
--------------------------------------------------
@@ -102,7 +165,7 @@ POST test/_update/1
102165
// CONSOLE
103166
// TEST[continued]
104167

105-
Or remove a field from the document:
168+
Conversely, this script removes the field `new_field`:
106169

107170
[source,js]
108171
--------------------------------------------------
@@ -114,9 +177,9 @@ POST test/_update/1
114177
// CONSOLE
115178
// TEST[continued]
116179

117-
And, we can even change the operation that is executed. This example deletes
118-
the doc if the `tags` field contains `green`, otherwise it does nothing
119-
(`noop`):
180+
Instead of updating the document, you can also change the operation that is
181+
executed from within the script. For example, this request deletes the doc if
182+
the `tags` field contains `green`, otherwise it does nothing (`noop`):
120183

121184
[source,js]
122185
--------------------------------------------------
@@ -135,13 +198,8 @@ POST test/_update/1
135198
// TEST[continued]
136199

137200
[float]
138-
==== Updates with a partial document
201+
===== Update part of a document
139202

140-
The update API also supports passing a partial document,
141-
which will be merged into the existing document (simple recursive merge,
142-
inner merging of objects, replacing core "keys/values" and arrays).
143-
To fully replace the existing document, the <<docs-index_,`index` API>> should
144-
be used instead.
145203
The following partial update adds a new field to the
146204
existing document:
147205

@@ -157,14 +215,14 @@ POST test/_update/1
157215
// CONSOLE
158216
// TEST[continued]
159217

160-
If both `doc` and `script` are specified, then `doc` is ignored. Best is
161-
to put your field pairs of the partial document in the script itself.
218+
If both `doc` and `script` are specified, then `doc` is ignored. If you
219+
specify a scripted update, include the fields you want to update in the script.
162220

163221
[float]
164-
==== Detecting noop updates
222+
===== Detect noop updates
165223

166-
If `doc` is specified its value is merged with the existing `_source`.
167-
By default updates that don't change anything detect that they don't change anything and return `"result": "noop"` like this:
224+
By default updates that don't change anything detect that they don't change
225+
anything and return `"result": "noop"`:
168226

169227
[source,js]
170228
--------------------------------------------------
@@ -178,9 +236,8 @@ POST test/_update/1
178236
// CONSOLE
179237
// TEST[continued]
180238

181-
If `name` was `new_name` before the request was sent then the entire update
182-
request is ignored. The `result` element in the response returns `noop` if
183-
the request was ignored.
239+
If the value of `name` is already `new_name`, the update
240+
request is ignored and the `result` element in the response returns `noop`:
184241

185242
[source,js]
186243
--------------------------------------------------
@@ -201,7 +258,7 @@ the request was ignored.
201258
--------------------------------------------------
202259
// TESTRESPONSE
203260

204-
You can disable this behavior by setting `"detect_noop": false` like this:
261+
You can disable this behavior by setting `"detect_noop": false`:
205262

206263
[source,js]
207264
--------------------------------------------------
@@ -218,11 +275,11 @@ POST test/_update/1
218275

219276
[[upserts]]
220277
[float]
221-
==== Upserts
278+
===== Upsert
222279

223280
If the document does not already exist, the contents of the `upsert` element
224-
will be inserted as a new document. If the document does exist, then the
225-
`script` will be executed instead:
281+
are inserted as a new document. If the document exists, the
282+
`script` is executed:
226283

227284
[source,js]
228285
--------------------------------------------------
@@ -245,11 +302,10 @@ POST test/_update/1
245302

246303
[float]
247304
[[scripted_upsert]]
248-
===== `scripted_upsert`
305+
===== Scripted upsert
249306

250-
If you would like your script to run regardless of whether the document exists
251-
or not -- i.e. the script handles initializing the document instead of the
252-
`upsert` element -- then set `scripted_upsert` to `true`:
307+
To run the script whether or not the document exists, set `scripted_upsert` to
308+
`true`:
253309

254310
[source,js]
255311
--------------------------------------------------
@@ -275,10 +331,10 @@ POST sessions/_update/dh3sgudg8gsrgl
275331

276332
[float]
277333
[[doc_as_upsert]]
278-
===== `doc_as_upsert`
334+
===== Doc as upsert
279335

280-
Instead of sending a partial `doc` plus an `upsert` doc, setting
281-
`doc_as_upsert` to `true` will use the contents of `doc` as the `upsert`
336+
Instead of sending a partial `doc` plus an `upsert` doc, you can set
337+
`doc_as_upsert` to `true` to use the contents of `doc` as the `upsert`
282338
value:
283339

284340
[source,js]
@@ -293,51 +349,3 @@ POST test/_update/1
293349
--------------------------------------------------
294350
// CONSOLE
295351
// TEST[continued]
296-
297-
[float]
298-
==== Parameters
299-
300-
The update operation supports the following query-string parameters:
301-
302-
[horizontal]
303-
`retry_on_conflict`::
304-
305-
In between the get and indexing phases of the update, it is possible that
306-
another process might have already updated the same document. By default, the
307-
update will fail with a version conflict exception. The `retry_on_conflict`
308-
parameter controls how many times to retry the update before finally throwing
309-
an exception.
310-
311-
`routing`::
312-
313-
Routing is used to route the update request to the right shard and sets the
314-
routing for the upsert request if the document being updated doesn't exist.
315-
Can't be used to update the routing of an existing document.
316-
317-
`timeout`::
318-
319-
Timeout waiting for a shard to become available.
320-
321-
`wait_for_active_shards`::
322-
323-
The number of shard copies required to be active before proceeding with the update operation.
324-
See <<index-wait-for-active-shards,here>> for details.
325-
326-
`refresh`::
327-
328-
Control when the changes made by this request are visible to search. See
329-
<<docs-refresh, refresh>>.
330-
331-
`_source`::
332-
333-
Allows to control if and how the updated source should be returned in the response.
334-
By default the updated source is not returned.
335-
See <<request-body-search-source-filtering, Source filtering>> for details.
336-
337-
`if_seq_no` and `if_primary_term`::
338-
339-
Update operations can be made conditional and only be performed if the last
340-
modification to the document was assigned the sequence number and primary
341-
term specified by the `if_seq_no` and `if_primary_term` parameters. If a
342-
mismatch is detected, the operation will result in a `VersionConflictException`
343-
and a status code of 409. See <<optimistic-concurrency-control>> for more details.

0 commit comments

Comments
 (0)