Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 7 additions & 11 deletions docs/reference/docs/update.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -307,23 +307,19 @@ POST test/_update/1
[discrete]
===== Upsert

If the document does not already exist, the contents of the `upsert` element
are inserted as a new document. If the document exists, the
`script` is executed:
Upserts are a powerful operation that allows you to perform both update and insert actions in a single request. This is particularly useful when you want to update a document if it exists, or insert a new document if it does not.

In this example, if the product with ID `1` exists, its price will be updated to `100`. If the product does not exist, a new document with ID `1` and a price of `50` will be inserted.

[source,console]
----
POST test/_update/1
POST /test/_update/1
{
"script": {
"source": "ctx._source.counter += params.count",
"lang": "painless",
"params": {
"count": 4
}
"doc": {
"product_price": 100
},
"upsert": {
"counter": 1
"product_price": 50
}
}
----
Expand Down
Loading