Skip to content

Commit f053425

Browse files
committed
[DOCS] Reformat update API reference. (elastic#45423)
* [DOCS] Reformat update API reference.
1 parent 5d24c65 commit f053425

File tree

2 files changed

+112
-73
lines changed

2 files changed

+112
-73
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 & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +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). It uses versioning to make sure no updates have happened
9-
during the "get" and "reindex".
7+
Updates a document using the specified script.
108

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

16-
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:
1784

1885
[source,js]
1986
--------------------------------------------------
@@ -25,10 +92,8 @@ PUT test/_doc/1
2592
--------------------------------------------------
2693
// CONSOLE
2794

28-
[float]
29-
==== Scripted updates
30-
31-
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:
3297

3398
[source,js]
3499
--------------------------------------------------
@@ -46,8 +111,8 @@ POST test/_update/1
46111
// CONSOLE
47112
// TEST[continued]
48113

49-
We can add a tag to the list of tags (if the tag exists, it
50-
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):
51116

52117
[source,js]
53118
--------------------------------------------------
@@ -65,11 +130,11 @@ POST test/_update/1
65130
// CONSOLE
66131
// TEST[continued]
67132

68-
We can remove a tag from the list of tags. Note that the Painless function to
69-
`remove` a tag takes as its parameter the array index of the element you wish
70-
to remove, so you need a bit more logic to locate it while avoiding a runtime
71-
error. Note that if the tag was present more than once in the list, this will
72-
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.
73138

74139
[source,js]
75140
--------------------------------------------------
@@ -87,11 +152,8 @@ POST test/_update/1
87152
// CONSOLE
88153
// TEST[continued]
89154

90-
In addition to `_source`, the following variables are available through
91-
the `ctx` map: `_index`, `_type`, `_id`, `_version`, `_routing`,
92-
and `_now` (the current timestamp).
93-
94-
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`:
95157

96158
[source,js]
97159
--------------------------------------------------
@@ -103,7 +165,7 @@ POST test/_update/1
103165
// CONSOLE
104166
// TEST[continued]
105167

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

108170
[source,js]
109171
--------------------------------------------------
@@ -115,9 +177,9 @@ POST test/_update/1
115177
// CONSOLE
116178
// TEST[continued]
117179

118-
And, we can even change the operation that is executed. This example deletes
119-
the doc if the `tags` field contains `green`, otherwise it does nothing
120-
(`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`):
121183

122184
[source,js]
123185
--------------------------------------------------
@@ -136,13 +198,8 @@ POST test/_update/1
136198
// TEST[continued]
137199

138200
[float]
139-
==== Updates with a partial document
201+
===== Update part of a document
140202

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

@@ -158,14 +215,14 @@ POST test/_update/1
158215
// CONSOLE
159216
// TEST[continued]
160217

161-
If both `doc` and `script` are specified, then `doc` is ignored. Best is
162-
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.
163220

164221
[float]
165-
==== Detecting noop updates
222+
===== Detect noop updates
166223

167-
If `doc` is specified its value is merged with the existing `_source`.
168-
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"`:
169226

170227
[source,js]
171228
--------------------------------------------------
@@ -179,9 +236,8 @@ POST test/_update/1
179236
// CONSOLE
180237
// TEST[continued]
181238

182-
If `name` was `new_name` before the request was sent then the entire update
183-
request is ignored. The `result` element in the response returns `noop` if
184-
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`:
185241

186242
[source,js]
187243
--------------------------------------------------
@@ -200,7 +256,7 @@ the request was ignored.
200256
--------------------------------------------------
201257
// TESTRESPONSE
202258

203-
You can disable this behavior by setting `"detect_noop": false` like this:
259+
You can disable this behavior by setting `"detect_noop": false`:
204260

205261
[source,js]
206262
--------------------------------------------------
@@ -217,11 +273,11 @@ POST test/_update/1
217273

218274
[[upserts]]
219275
[float]
220-
==== Upserts
276+
===== Upsert
221277

222278
If the document does not already exist, the contents of the `upsert` element
223-
will be inserted as a new document. If the document does exist, then the
224-
`script` will be executed instead:
279+
are inserted as a new document. If the document exists, the
280+
`script` is executed:
225281

226282
[source,js]
227283
--------------------------------------------------
@@ -244,11 +300,10 @@ POST test/_update/1
244300

245301
[float]
246302
[[scripted_upsert]]
247-
===== `scripted_upsert`
303+
===== Scripted upsert
248304

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

253308
[source,js]
254309
--------------------------------------------------
@@ -274,10 +329,10 @@ POST sessions/_update/dh3sgudg8gsrgl
274329

275330
[float]
276331
[[doc_as_upsert]]
277-
===== `doc_as_upsert`
332+
===== Doc as upsert
278333

279-
Instead of sending a partial `doc` plus an `upsert` doc, setting
280-
`doc_as_upsert` to `true` will use the contents of `doc` as the `upsert`
334+
Instead of sending a partial `doc` plus an `upsert` doc, you can set
335+
`doc_as_upsert` to `true` to use the contents of `doc` as the `upsert`
281336
value:
282337

283338
[source,js]
@@ -343,19 +398,3 @@ matches the one specified.
343398

344399
[NOTE]
345400
.The update API does not support versioning other than internal
346-
=====================================================
347-
348-
External (version types `external` and `external_gte`) or forced (version type `force`)
349-
versioning is not supported by the update API as it would result in Elasticsearch
350-
version numbers being out of sync with the external system. Use the
351-
<<docs-index_,`index` API>> instead.
352-
353-
=====================================================
354-
355-
`if_seq_no` and `if_primary_term`::
356-
357-
Update operations can be made conditional and only be performed if the last
358-
modification to the document was assigned the sequence number and primary
359-
term specified by the `if_seq_no` and `if_primary_term` parameters. If a
360-
mismatch is detected, the operation will result in a `VersionConflictException`
361-
and a status code of 409. See <<optimistic-concurrency-control>> for more details.

0 commit comments

Comments
 (0)