Skip to content

Commit 808d165

Browse files
Specify usage of upsert key in Bulk operations (#11506) (#11567)
1 parent 2443fde commit 808d165

File tree

1 file changed

+32
-12
lines changed
  • _api-reference/document-apis

1 file changed

+32
-12
lines changed

_api-reference/document-apis/bulk.md

Lines changed: 32 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -98,12 +98,22 @@ By default, this action updates existing documents and returns an error if the d
9898

9999
### Upsert
100100

101-
To upsert a document, specify `doc_as_upsert` as `true`. If a document exists, it is updated; if it does not exist, a new document is indexed with the parameters specified in the `doc` field:
101+
To upsert a document, use one of the following options:
102102

103-
```json
104-
{ "update": { "_index": "movies", "_id": "tt0816711" } }
105-
{ "doc" : { "title": "World War Z" }, "doc_as_upsert": true }
106-
```
103+
1. Specify the document in the `doc` field and set `doc_as_upsert=true`. If the document exists, it is updated with the contents of the `doc` field. If the document does not exist, a new document is indexed with the parameters specified in the `doc` field:
104+
105+
```json
106+
{ "update": { "_index": "movies", "_id": "tt0816711" } }
107+
{ "doc" : { "title": "World War Z" }, "doc_as_upsert": true }
108+
```
109+
1. Specify the document to update (when it exists) in the `doc` field, the document to insert (when it doesn't exist) in the `upsert` field, and leave `doc_as_update` set to `false`:
110+
111+
```json
112+
{ "update": { "_index": "products", "_id": "widget-123" } }
113+
{ "doc": { "stock": 75, "updated_at": "2025-01-15T10:30:00Z" }, "upsert": { "name": "Widget", "price": 39.99, "stock": 100, "created_at": "2025-01-15T10:30:00Z" }}
114+
```
115+
116+
Use this option when you want to only update specific fields when a document exists but insert a complete document when it doesn't exist.
107117

108118
### Script
109119

@@ -116,15 +126,25 @@ You can specify a script for more complex document updates by defining the scrip
116126

117127
### Scripted upsert
118128

119-
You can use a script to insert or update a document in one operation by setting `scripted_upsert` to `true`. This ensures that the script runs whether or not the document exists. If the document does not exist, the script initializes its content from scratch.
129+
You can use a script to update or upsert a document in the following ways:
120130

121-
```json
122-
POST _bulk
123-
{ "update": { "_index": "movies", "_id": "tt0816711" } }
124-
{ "script": { "source": "ctx._source.title = params.title; ctx._source.genre = params.genre;", "params": { "title": "World War Z", "genre": "Action" } }, "upsert": {}, "scripted_upsert": true }
125-
```
131+
1. Script + upsert (`scripted_upsert=false`, default): If the document exists, the document is updated using the `script`. If the document does not exist, the document in the `upsert` field is inserted without running the script:
132+
133+
```json
134+
POST _bulk
135+
{ "update": { "_index": "movies", "_id": "tt0816711" } }
136+
{ "script": { "source": "ctx._source.title = params.title; ctx._source.genre = params.genre;", "params": { "title": "World War Z", "genre": "Action" } }, "upsert": { "title": "World War Z", "genre": "Action", "author": "Tom Smith" } }
137+
```
138+
{% include copy-curl.html %}
139+
140+
1. Script + upsert + `scripted_upsert=true`. If the document exists, the document is updated using the `script`. If the document does not exist, the script runs on the `upsert` field and the resulting document is inserted:
126141

127-
This operation creates a new document if one with ID `tt0816711` does not exist, using the logic in the script. If the document does exist, the same script is applied to update its fields.
142+
```json
143+
POST _bulk
144+
{ "update": { "_index": "movies", "_id": "tt0816711" } }
145+
{ "script": { "source": "ctx._source.title = params.title; ctx._source.genre = params.genre;", "params": { "title": "World War Z", "genre": "Action" } }, "scripted_upsert": true }
146+
```
147+
{% include copy-curl.html %}
128148

129149
## Example request
130150

0 commit comments

Comments
 (0)