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).
7
+ Updates a document using the specified script.
9
8
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}
14
11
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:
16
84
17
85
[source,js]
18
86
--------------------------------------------------
@@ -24,10 +92,8 @@ PUT test/_doc/1
24
92
--------------------------------------------------
25
93
// CONSOLE
26
94
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:
31
97
32
98
[source,js]
33
99
--------------------------------------------------
@@ -45,8 +111,8 @@ POST test/_update/1
45
111
// CONSOLE
46
112
// TEST[continued]
47
113
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 ):
50
116
51
117
[source,js]
52
118
--------------------------------------------------
@@ -64,11 +130,11 @@ POST test/_update/1
64
130
// CONSOLE
65
131
// TEST[continued]
66
132
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.
72
138
73
139
[source,js]
74
140
--------------------------------------------------
@@ -86,11 +152,8 @@ POST test/_update/1
86
152
// CONSOLE
87
153
// TEST[continued]
88
154
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`:
94
157
95
158
[source,js]
96
159
--------------------------------------------------
@@ -102,7 +165,7 @@ POST test/_update/1
102
165
// CONSOLE
103
166
// TEST[continued]
104
167
105
- Or remove a field from the document :
168
+ Conversely, this script removes the field `new_field` :
106
169
107
170
[source,js]
108
171
--------------------------------------------------
@@ -114,9 +177,9 @@ POST test/_update/1
114
177
// CONSOLE
115
178
// TEST[continued]
116
179
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`):
120
183
121
184
[source,js]
122
185
--------------------------------------------------
@@ -135,13 +198,8 @@ POST test/_update/1
135
198
// TEST[continued]
136
199
137
200
[float]
138
- ==== Updates with a partial document
201
+ ===== Update part of a document
139
202
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.
145
203
The following partial update adds a new field to the
146
204
existing document:
147
205
@@ -157,14 +215,14 @@ POST test/_update/1
157
215
// CONSOLE
158
216
// TEST[continued]
159
217
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.
162
220
163
221
[float]
164
- ==== Detecting noop updates
222
+ ===== Detect noop updates
165
223
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"`:
168
226
169
227
[source,js]
170
228
--------------------------------------------------
@@ -178,9 +236,8 @@ POST test/_update/1
178
236
// CONSOLE
179
237
// TEST[continued]
180
238
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`:
184
241
185
242
[source,js]
186
243
--------------------------------------------------
@@ -201,7 +258,7 @@ the request was ignored.
201
258
--------------------------------------------------
202
259
// TESTRESPONSE
203
260
204
- You can disable this behavior by setting `"detect_noop": false` like this :
261
+ You can disable this behavior by setting `"detect_noop": false`:
205
262
206
263
[source,js]
207
264
--------------------------------------------------
@@ -218,11 +275,11 @@ POST test/_update/1
218
275
219
276
[[upserts]]
220
277
[float]
221
- ==== Upserts
278
+ ===== Upsert
222
279
223
280
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:
226
283
227
284
[source,js]
228
285
--------------------------------------------------
@@ -245,11 +302,10 @@ POST test/_update/1
245
302
246
303
[float]
247
304
[[scripted_upsert]]
248
- ===== `scripted_upsert`
305
+ ===== Scripted upsert
249
306
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`:
253
309
254
310
[source,js]
255
311
--------------------------------------------------
@@ -275,10 +331,10 @@ POST sessions/_update/dh3sgudg8gsrgl
275
331
276
332
[float]
277
333
[[doc_as_upsert]]
278
- ===== `doc_as_upsert`
334
+ ===== Doc as upsert
279
335
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`
282
338
value:
283
339
284
340
[source,js]
@@ -293,51 +349,3 @@ POST test/_update/1
293
349
--------------------------------------------------
294
350
// CONSOLE
295
351
// 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