1
1
[[docs-update]]
2
2
=== Update API
3
+ ++++
4
+ <titleabbrev>Update</titleabbrev>
5
+ ++++
3
6
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.
10
8
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}
15
11
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:
17
84
18
85
[source,js]
19
86
--------------------------------------------------
@@ -25,10 +92,8 @@ PUT test/_doc/1
25
92
--------------------------------------------------
26
93
// CONSOLE
27
94
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:
32
97
33
98
[source,js]
34
99
--------------------------------------------------
@@ -46,8 +111,8 @@ POST test/_update/1
46
111
// CONSOLE
47
112
// TEST[continued]
48
113
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 ):
51
116
52
117
[source,js]
53
118
--------------------------------------------------
@@ -65,11 +130,11 @@ POST test/_update/1
65
130
// CONSOLE
66
131
// TEST[continued]
67
132
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.
73
138
74
139
[source,js]
75
140
--------------------------------------------------
@@ -87,11 +152,8 @@ POST test/_update/1
87
152
// CONSOLE
88
153
// TEST[continued]
89
154
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`:
95
157
96
158
[source,js]
97
159
--------------------------------------------------
@@ -103,7 +165,7 @@ POST test/_update/1
103
165
// CONSOLE
104
166
// TEST[continued]
105
167
106
- Or remove a field from the document :
168
+ Conversely, this script removes the field `new_field` :
107
169
108
170
[source,js]
109
171
--------------------------------------------------
@@ -115,9 +177,9 @@ POST test/_update/1
115
177
// CONSOLE
116
178
// TEST[continued]
117
179
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`):
121
183
122
184
[source,js]
123
185
--------------------------------------------------
@@ -136,13 +198,8 @@ POST test/_update/1
136
198
// TEST[continued]
137
199
138
200
[float]
139
- ==== Updates with a partial document
201
+ ===== Update part of a document
140
202
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.
146
203
The following partial update adds a new field to the
147
204
existing document:
148
205
@@ -158,14 +215,14 @@ POST test/_update/1
158
215
// CONSOLE
159
216
// TEST[continued]
160
217
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.
163
220
164
221
[float]
165
- ==== Detecting noop updates
222
+ ===== Detect noop updates
166
223
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"`:
169
226
170
227
[source,js]
171
228
--------------------------------------------------
@@ -179,9 +236,8 @@ POST test/_update/1
179
236
// CONSOLE
180
237
// TEST[continued]
181
238
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`:
185
241
186
242
[source,js]
187
243
--------------------------------------------------
@@ -200,7 +256,7 @@ the request was ignored.
200
256
--------------------------------------------------
201
257
// TESTRESPONSE
202
258
203
- You can disable this behavior by setting `"detect_noop": false` like this :
259
+ You can disable this behavior by setting `"detect_noop": false`:
204
260
205
261
[source,js]
206
262
--------------------------------------------------
@@ -217,11 +273,11 @@ POST test/_update/1
217
273
218
274
[[upserts]]
219
275
[float]
220
- ==== Upserts
276
+ ===== Upsert
221
277
222
278
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:
225
281
226
282
[source,js]
227
283
--------------------------------------------------
@@ -244,11 +300,10 @@ POST test/_update/1
244
300
245
301
[float]
246
302
[[scripted_upsert]]
247
- ===== `scripted_upsert`
303
+ ===== Scripted upsert
248
304
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`:
252
307
253
308
[source,js]
254
309
--------------------------------------------------
@@ -274,10 +329,10 @@ POST sessions/_update/dh3sgudg8gsrgl
274
329
275
330
[float]
276
331
[[doc_as_upsert]]
277
- ===== `doc_as_upsert`
332
+ ===== Doc as upsert
278
333
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`
281
336
value:
282
337
283
338
[source,js]
@@ -343,19 +398,3 @@ matches the one specified.
343
398
344
399
[NOTE]
345
400
.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