From 106ff63663c2b2c6f334882206c95beb873ee72a Mon Sep 17 00:00:00 2001 From: Kofi B Date: Wed, 22 Jan 2025 15:10:02 -0800 Subject: [PATCH 1/2] [DOCS] Upsert documentation clarification --- docs/reference/docs/update.asciidoc | 18 +++++++----------- 1 file changed, 7 insertions(+), 11 deletions(-) diff --git a/docs/reference/docs/update.asciidoc b/docs/reference/docs/update.asciidoc index ae9ae8fe73fc6..08b8549b5533f 100644 --- a/docs/reference/docs/update.asciidoc +++ b/docs/reference/docs/update.asciidoc @@ -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 } } ---- From f7bb1bfa7672d3549f714f334d27759a550cce06 Mon Sep 17 00:00:00 2001 From: Kofi B Date: Tue, 28 Jan 2025 10:46:01 -0800 Subject: [PATCH 2/2] Update docs/reference/docs/update.asciidoc Co-authored-by: Liam Thompson <32779855+leemthompo@users.noreply.github.com> --- docs/reference/docs/update.asciidoc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/reference/docs/update.asciidoc b/docs/reference/docs/update.asciidoc index 08b8549b5533f..62201f5748b7d 100644 --- a/docs/reference/docs/update.asciidoc +++ b/docs/reference/docs/update.asciidoc @@ -307,7 +307,7 @@ POST test/_update/1 [discrete] ===== Upsert -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. +An upsert operation lets you update an existing document or insert a new one if it doesn't exist, in a single request. 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.