update.asciidoc 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  1. [[docs-update]]
  2. === Update API
  3. ++++
  4. <titleabbrev>Update</titleabbrev>
  5. ++++
  6. Updates a document using the specified script.
  7. [[docs-update-api-request]]
  8. ==== {api-request-title}
  9. `POST /<index>/_update/<_id>`
  10. [[update-api-desc]]
  11. ==== {api-description-title}
  12. Enables you to script document updates. The script can update, delete, or skip
  13. modifying the document. The update API also supports passing a partial document,
  14. which is merged into the existing document. To fully replace an existing
  15. document, use the <<docs-index_,`index` API>>.
  16. This operation:
  17. . Gets the document (collocated with the shard) from the index.
  18. . Runs the specified script.
  19. . Indexes the result.
  20. The document must still be reindexed, but using `update` removes some network
  21. roundtrips and reduces chances of version conflicts between the GET and the
  22. index operation.
  23. The `_source` field must be enabled to use `update`. In addition to `_source`,
  24. you can access the following variables through the `ctx` map: `_index`,
  25. `_type`, `_id`, `_version`, `_routing`, and `_now` (the current timestamp).
  26. [[docs-update-api-path-params]]
  27. ==== {api-path-parms-title}
  28. `<index>`::
  29. (Required, string) Name of the target index. By default, the index is created
  30. automatically if it doesn't exist. For more information, see <<index-creation>>.
  31. `<_id>`::
  32. (Required, string) Unique identifier for the document to be updated.
  33. [[docs-update-api-query-params]]
  34. ==== {api-query-parms-title}
  35. include::{es-repo-dir}/rest-api/common-parms.asciidoc[tag=if_seq_no]
  36. include::{es-repo-dir}/rest-api/common-parms.asciidoc[tag=if_primary_term]
  37. `lang`::
  38. (Optional, string) The script language. Default: `painless`.
  39. include::{es-repo-dir}/rest-api/common-parms.asciidoc[tag=refresh]
  40. `retry_on_conflict`::
  41. (Optional, integer) Specify how many times should the operation be retried when
  42. a conflict occurs. Default: 0.
  43. include::{es-repo-dir}/rest-api/common-parms.asciidoc[tag=routing]
  44. `_source`::
  45. (Optional, list) Set to `false` to disable source retrieval (default: `true`).
  46. You can also specify a comma-separated list of the fields you want to retrieve.
  47. `_source_excludes`::
  48. (Optional, list) Specify the source fields you want to exclude.
  49. `_source_includes`::
  50. (Optional, list) Specify the source fields you want to retrieve.
  51. include::{es-repo-dir}/rest-api/common-parms.asciidoc[tag=timeoutparms]
  52. include::{es-repo-dir}/rest-api/common-parms.asciidoc[tag=wait_for_active_shards]
  53. [[update-api-example]]
  54. ==== {api-examples-title}
  55. First, let's index a simple doc:
  56. [source,console]
  57. --------------------------------------------------
  58. PUT test/_doc/1
  59. {
  60. "counter" : 1,
  61. "tags" : ["red"]
  62. }
  63. --------------------------------------------------
  64. To increment the counter, you can submit an update request with the
  65. following script:
  66. [source,console]
  67. --------------------------------------------------
  68. POST test/_update/1
  69. {
  70. "script" : {
  71. "source": "ctx._source.counter += params.count",
  72. "lang": "painless",
  73. "params" : {
  74. "count" : 4
  75. }
  76. }
  77. }
  78. --------------------------------------------------
  79. // TEST[continued]
  80. Similarly, you could use and update script to add a tag to the list of tags
  81. (this is just a list, so the tag is added even it exists):
  82. [source,console]
  83. --------------------------------------------------
  84. POST test/_update/1
  85. {
  86. "script" : {
  87. "source": "ctx._source.tags.add(params.tag)",
  88. "lang": "painless",
  89. "params" : {
  90. "tag" : "blue"
  91. }
  92. }
  93. }
  94. --------------------------------------------------
  95. // TEST[continued]
  96. You could also remove a tag from the list of tags. The Painless
  97. function to `remove` a tag takes the array index of the element
  98. you want to remove. To avoid a possible runtime error, you first need to
  99. make sure the tag exists. If the list contains duplicates of the tag, this
  100. script just removes one occurrence.
  101. [source,console]
  102. --------------------------------------------------
  103. POST test/_update/1
  104. {
  105. "script" : {
  106. "source": "if (ctx._source.tags.contains(params.tag)) { ctx._source.tags.remove(ctx._source.tags.indexOf(params.tag)) }",
  107. "lang": "painless",
  108. "params" : {
  109. "tag" : "blue"
  110. }
  111. }
  112. }
  113. --------------------------------------------------
  114. // TEST[continued]
  115. You can also add and remove fields from a document. For example, this script
  116. adds the field `new_field`:
  117. [source,console]
  118. --------------------------------------------------
  119. POST test/_update/1
  120. {
  121. "script" : "ctx._source.new_field = 'value_of_new_field'"
  122. }
  123. --------------------------------------------------
  124. // TEST[continued]
  125. Conversely, this script removes the field `new_field`:
  126. [source,console]
  127. --------------------------------------------------
  128. POST test/_update/1
  129. {
  130. "script" : "ctx._source.remove('new_field')"
  131. }
  132. --------------------------------------------------
  133. // TEST[continued]
  134. Instead of updating the document, you can also change the operation that is
  135. executed from within the script. For example, this request deletes the doc if
  136. the `tags` field contains `green`, otherwise it does nothing (`noop`):
  137. [source,console]
  138. --------------------------------------------------
  139. POST test/_update/1
  140. {
  141. "script" : {
  142. "source": "if (ctx._source.tags.contains(params.tag)) { ctx.op = 'delete' } else { ctx.op = 'none' }",
  143. "lang": "painless",
  144. "params" : {
  145. "tag" : "green"
  146. }
  147. }
  148. }
  149. --------------------------------------------------
  150. // TEST[continued]
  151. [float]
  152. ===== Update part of a document
  153. The following partial update adds a new field to the
  154. existing document:
  155. [source,console]
  156. --------------------------------------------------
  157. POST test/_update/1
  158. {
  159. "doc" : {
  160. "name" : "new_name"
  161. }
  162. }
  163. --------------------------------------------------
  164. // TEST[continued]
  165. If both `doc` and `script` are specified, then `doc` is ignored. If you
  166. specify a scripted update, include the fields you want to update in the script.
  167. [float]
  168. ===== Detect noop updates
  169. By default updates that don't change anything detect that they don't change
  170. anything and return `"result": "noop"`:
  171. [source,console]
  172. --------------------------------------------------
  173. POST test/_update/1
  174. {
  175. "doc" : {
  176. "name" : "new_name"
  177. }
  178. }
  179. --------------------------------------------------
  180. // TEST[continued]
  181. If the value of `name` is already `new_name`, the update
  182. request is ignored and the `result` element in the response returns `noop`:
  183. [source,console-result]
  184. --------------------------------------------------
  185. {
  186. "_shards": {
  187. "total": 0,
  188. "successful": 0,
  189. "failed": 0
  190. },
  191. "_index": "test",
  192. "_id": "1",
  193. "_version": 7,
  194. "_primary_term": 1,
  195. "_seq_no": 6,
  196. "result": "noop"
  197. }
  198. --------------------------------------------------
  199. You can disable this behavior by setting `"detect_noop": false`:
  200. [source,console]
  201. --------------------------------------------------
  202. POST test/_update/1
  203. {
  204. "doc" : {
  205. "name" : "new_name"
  206. },
  207. "detect_noop": false
  208. }
  209. --------------------------------------------------
  210. // TEST[continued]
  211. [[upserts]]
  212. [float]
  213. ===== Upsert
  214. If the document does not already exist, the contents of the `upsert` element
  215. are inserted as a new document. If the document exists, the
  216. `script` is executed:
  217. [source,console]
  218. --------------------------------------------------
  219. POST test/_update/1
  220. {
  221. "script" : {
  222. "source": "ctx._source.counter += params.count",
  223. "lang": "painless",
  224. "params" : {
  225. "count" : 4
  226. }
  227. },
  228. "upsert" : {
  229. "counter" : 1
  230. }
  231. }
  232. --------------------------------------------------
  233. // TEST[continued]
  234. [float]
  235. [[scripted_upsert]]
  236. ===== Scripted upsert
  237. To run the script whether or not the document exists, set `scripted_upsert` to
  238. `true`:
  239. [source,console]
  240. --------------------------------------------------
  241. POST sessions/_update/dh3sgudg8gsrgl
  242. {
  243. "scripted_upsert":true,
  244. "script" : {
  245. "id": "my_web_session_summariser",
  246. "params" : {
  247. "pageViewEvent" : {
  248. "url":"foo.com/bar",
  249. "response":404,
  250. "time":"2014-01-01 12:32"
  251. }
  252. }
  253. },
  254. "upsert" : {}
  255. }
  256. --------------------------------------------------
  257. // TEST[s/"id": "my_web_session_summariser"/"source": "ctx._source.page_view_event = params.pageViewEvent"/]
  258. // TEST[continued]
  259. [float]
  260. [[doc_as_upsert]]
  261. ===== Doc as upsert
  262. Instead of sending a partial `doc` plus an `upsert` doc, you can set
  263. `doc_as_upsert` to `true` to use the contents of `doc` as the `upsert`
  264. value:
  265. [source,console]
  266. --------------------------------------------------
  267. POST test/_update/1
  268. {
  269. "doc" : {
  270. "name" : "new_name"
  271. },
  272. "doc_as_upsert" : true
  273. }
  274. --------------------------------------------------
  275. // TEST[continued]
  276. [NOTE]
  277. ====
  278. Using <<ingest,ingest pipelines>> with `doc_as_upsert` is not supported.
  279. ====