|
| 1 | +--- |
| 2 | +mapped_pages: |
| 3 | + - https://www.elastic.co/guide/en/elasticsearch/reference/8.18/docs-update.html |
| 4 | +applies_to: |
| 5 | + stack: all |
| 6 | +navigation_title: Update a document |
| 7 | +--- |
| 8 | + |
| 9 | +# Update a document [update-document] |
| 10 | + |
| 11 | +The [Update API](https://www.elastic.co/docs/api/doc/elasticsearch/operation/operation-update) enables you to script document updates, allowing you to update, delete, or skip modifying a document. |
| 12 | + |
| 13 | +The following examples show common use cases, such as incrementing a counter and adding or removing elements from a list, as well as how to: |
| 14 | + |
| 15 | +- [Update part of a document](#update-part-document) |
| 16 | +- [Detect noop updates](#detect-noop-updates) |
| 17 | +- [Insert or update documents with upsert](#upsert) |
| 18 | +- [Simplify upsert with doc_as_upsert](#doc-as-upsert) |
| 19 | + |
| 20 | +First, let's index a simple doc: |
| 21 | + |
| 22 | +```console |
| 23 | +PUT test/_doc/1 |
| 24 | +{ |
| 25 | + "counter" : 1, |
| 26 | + "tags" : ["red"] |
| 27 | +} |
| 28 | +``` |
| 29 | +% TESTSETUP |
| 30 | + |
| 31 | +To increment the counter, you can submit an update request with the |
| 32 | +following script: |
| 33 | + |
| 34 | +```console |
| 35 | +POST test/_update/1 |
| 36 | +{ |
| 37 | + "script" : { |
| 38 | + "source": "ctx._source.counter += params.count", |
| 39 | + "lang": "painless", |
| 40 | + "params" : { |
| 41 | + "count" : 4 |
| 42 | + } |
| 43 | + } |
| 44 | +} |
| 45 | +``` |
| 46 | + |
| 47 | +Similarly, you could use and update script to add a tag to the list of tags |
| 48 | +(this is just a list, so the tag is added even it exists): |
| 49 | + |
| 50 | +```console |
| 51 | +POST test/_update/1 |
| 52 | +{ |
| 53 | + "script": { |
| 54 | + "source": "ctx._source.tags.add(params.tag)", |
| 55 | + "lang": "painless", |
| 56 | + "params": { |
| 57 | + "tag": "blue" |
| 58 | + } |
| 59 | + } |
| 60 | +} |
| 61 | +``` |
| 62 | + |
| 63 | +You could also remove a tag from the list of tags. The Painless |
| 64 | +function to `remove` a tag takes the array index of the element |
| 65 | +you want to remove. To avoid a possible runtime error, you first need to |
| 66 | +make sure the tag exists. If the list contains duplicates of the tag, this |
| 67 | +script just removes one occurrence. |
| 68 | + |
| 69 | +```console |
| 70 | +POST test/_update/1 |
| 71 | +{ |
| 72 | + "script": { |
| 73 | + "source": "if (ctx._source.tags.contains(params.tag)) { ctx._source.tags.remove(ctx._source.tags.indexOf(params.tag)) }", |
| 74 | + "lang": "painless", |
| 75 | + "params": { |
| 76 | + "tag": "blue" |
| 77 | + } |
| 78 | + } |
| 79 | +} |
| 80 | +``` |
| 81 | + |
| 82 | +You can also add and remove fields from a document. For example, this script |
| 83 | +adds the field `new_field`: |
| 84 | + |
| 85 | +```console |
| 86 | +POST test/_update/1 |
| 87 | +{ |
| 88 | + "script" : "ctx._source.new_field = 'value_of_new_field'" |
| 89 | +} |
| 90 | +``` |
| 91 | + |
| 92 | +Conversely, this script removes the field `new_field`: |
| 93 | + |
| 94 | +```console |
| 95 | +POST test/_update/1 |
| 96 | +{ |
| 97 | + "script" : "ctx._source.remove('new_field')" |
| 98 | +} |
| 99 | +``` |
| 100 | +% TEST[continued] |
| 101 | + |
| 102 | +The following script removes a subfield from an object field: |
| 103 | + |
| 104 | +```console |
| 105 | +PUT test/_doc/1?refresh |
| 106 | +{ |
| 107 | + "my-object": { |
| 108 | + "my-subfield": true |
| 109 | + } |
| 110 | +} |
| 111 | +``` |
| 112 | + |
| 113 | +```console |
| 114 | +POST test/_update/1 |
| 115 | +{ |
| 116 | + "script": "ctx._source['my-object'].remove('my-subfield')" |
| 117 | +} |
| 118 | +``` |
| 119 | +% TEST[continued] |
| 120 | + |
| 121 | +Instead of updating the document, you can also change the operation that is |
| 122 | +executed from within the script. For example, this request deletes the doc if |
| 123 | +the `tags` field contains `green`, otherwise it does nothing (`noop`): |
| 124 | + |
| 125 | +```console |
| 126 | +POST test/_update/1 |
| 127 | +{ |
| 128 | + "script": { |
| 129 | + "source": "if (ctx._source.tags.contains(params.tag)) { ctx.op = 'delete' } else { ctx.op = 'noop' }", |
| 130 | + "lang": "painless", |
| 131 | + "params": { |
| 132 | + "tag": "green" |
| 133 | + } |
| 134 | + } |
| 135 | +} |
| 136 | +``` |
| 137 | + |
| 138 | +## Update part of a document [update-part-document] |
| 139 | + |
| 140 | +The following partial update adds a new field to the |
| 141 | +existing document: |
| 142 | + |
| 143 | +```console |
| 144 | +POST test/_update/1 |
| 145 | +{ |
| 146 | + "doc": { |
| 147 | + "name": "new_name" |
| 148 | + } |
| 149 | +} |
| 150 | +``` |
| 151 | + |
| 152 | +If both `doc` and `script` are specified, then `doc` is ignored. If you |
| 153 | +specify a scripted update, include the fields you want to update in the script. |
| 154 | + |
| 155 | + |
| 156 | +## Detect noop updates [detect-noop-updates] |
| 157 | + |
| 158 | +By default updates that don't change anything detect that they don't change |
| 159 | +anything and return `"result": "noop"`: |
| 160 | + |
| 161 | +```console |
| 162 | +POST test/_update/1 |
| 163 | +{ |
| 164 | + "doc": { |
| 165 | + "name": "new_name" |
| 166 | + } |
| 167 | +} |
| 168 | +``` |
| 169 | +% TEST[continued] |
| 170 | + |
| 171 | +If the value of `name` is already `new_name`, the update |
| 172 | +request is ignored and the `result` element in the response returns `noop`: |
| 173 | + |
| 174 | +```console |
| 175 | + |
| 176 | +{ |
| 177 | + "_shards": { |
| 178 | + "total": 0, |
| 179 | + "successful": 0, |
| 180 | + "failed": 0 |
| 181 | + }, |
| 182 | + "_index": "test", |
| 183 | + "_id": "1", |
| 184 | + "_version": 2, |
| 185 | + "_primary_term": 1, |
| 186 | + "_seq_no": 1, |
| 187 | + "result": "noop" |
| 188 | +} |
| 189 | +``` |
| 190 | + |
| 191 | +You can disable this behavior by setting `"detect_noop": false`: |
| 192 | + |
| 193 | +```console |
| 194 | +POST test/_update/1 |
| 195 | +{ |
| 196 | + "doc": { |
| 197 | + "name": "new_name" |
| 198 | + }, |
| 199 | + "detect_noop": false |
| 200 | +} |
| 201 | +``` |
| 202 | + |
| 203 | +## Insert or update documents with upsert [upsert] |
| 204 | + |
| 205 | +An upsert operation lets you update an existing document or insert a new one if it doesn't exist, in a single request. |
| 206 | + |
| 207 | +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. |
| 208 | + |
| 209 | +```console |
| 210 | +POST /test/_update/1 |
| 211 | +{ |
| 212 | + "doc": { |
| 213 | + "product_price": 100 |
| 214 | + }, |
| 215 | + "upsert": { |
| 216 | + "product_price": 50 |
| 217 | + } |
| 218 | +} |
| 219 | +``` |
| 220 | + |
| 221 | +## Run a scripted upsert [scripted-upsert] |
| 222 | + |
| 223 | +To run the script whether or not the document exists, set `scripted_upsert` to |
| 224 | +`true`: |
| 225 | + |
| 226 | +```console |
| 227 | +POST test/_update/1 |
| 228 | +{ |
| 229 | + "scripted_upsert": true, |
| 230 | + "script": { |
| 231 | + "source": """ |
| 232 | + if ( ctx.op == 'create' ) { |
| 233 | + ctx._source.counter = params.count |
| 234 | + } else { |
| 235 | + ctx._source.counter += params.count |
| 236 | + } |
| 237 | + """, |
| 238 | + "params": { |
| 239 | + "count": 4 |
| 240 | + } |
| 241 | + }, |
| 242 | + "upsert": {} |
| 243 | +} |
| 244 | +``` |
| 245 | + |
| 246 | +## Simplify upsert with doc_as_upsert [doc-as-upsert] |
| 247 | + |
| 248 | +Instead of sending a partial `doc` plus an `upsert` doc, you can set |
| 249 | +`doc_as_upsert` to `true` to use the contents of `doc` as the `upsert` |
| 250 | +value: |
| 251 | + |
| 252 | +```console |
| 253 | +POST test/_update/1 |
| 254 | +{ |
| 255 | + "doc": { |
| 256 | + "name": "new_name" |
| 257 | + }, |
| 258 | + "doc_as_upsert": true |
| 259 | +} |
| 260 | +``` |
| 261 | + |
| 262 | +::::{note} |
| 263 | +Using [ingest pipelines](https://www.elastic.co/guide/en/elasticsearch/reference/8.18/ingest.html) with `doc_as_upsert` is not supported. |
| 264 | +:::: |
0 commit comments