update.asciidoc 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  1. [[docs-update]]
  2. == Update API
  3. The update API allows to update a document based on a script provided.
  4. The operation gets the document (collocated with the shard) from the
  5. index, runs the script (with optional script language and parameters),
  6. and index back the result (also allows to delete, or ignore the
  7. operation). It uses versioning to make sure no updates have happened
  8. during the "get" and "reindex".
  9. Note, this operation still means full reindex of the document, it just
  10. removes some network roundtrips and reduces chances of version conflicts
  11. between the get and the index. The `_source` field needs to be enabled
  12. for this feature to work.
  13. For example, let's index a simple doc:
  14. [source,js]
  15. --------------------------------------------------
  16. PUT test/_doc/1
  17. {
  18. "counter" : 1,
  19. "tags" : ["red"]
  20. }
  21. --------------------------------------------------
  22. // CONSOLE
  23. [float]
  24. === Scripted updates
  25. Now, we can execute a script that would increment the counter:
  26. [source,js]
  27. --------------------------------------------------
  28. POST test/_doc/1/_update
  29. {
  30. "script" : {
  31. "source": "ctx._source.counter += params.count",
  32. "lang": "painless",
  33. "params" : {
  34. "count" : 4
  35. }
  36. }
  37. }
  38. --------------------------------------------------
  39. // CONSOLE
  40. // TEST[continued]
  41. We can add a tag to the list of tags (note, if the tag exists, it
  42. will still add it, since its a list):
  43. [source,js]
  44. --------------------------------------------------
  45. POST test/_doc/1/_update
  46. {
  47. "script" : {
  48. "source": "ctx._source.tags.add(params.tag)",
  49. "lang": "painless",
  50. "params" : {
  51. "tag" : "blue"
  52. }
  53. }
  54. }
  55. --------------------------------------------------
  56. // CONSOLE
  57. // TEST[continued]
  58. In addition to `_source`, the following variables are available through
  59. the `ctx` map: `_index`, `_type`, `_id`, `_version`, `_routing`
  60. and `_now` (the current timestamp).
  61. We can also add a new field to the document:
  62. [source,js]
  63. --------------------------------------------------
  64. POST test/_doc/1/_update
  65. {
  66. "script" : "ctx._source.new_field = 'value_of_new_field'"
  67. }
  68. --------------------------------------------------
  69. // CONSOLE
  70. // TEST[continued]
  71. Or remove a field from the document:
  72. [source,js]
  73. --------------------------------------------------
  74. POST test/_doc/1/_update
  75. {
  76. "script" : "ctx._source.remove('new_field')"
  77. }
  78. --------------------------------------------------
  79. // CONSOLE
  80. // TEST[continued]
  81. And, we can even change the operation that is executed. This example deletes
  82. the doc if the `tags` field contain `green`, otherwise it does nothing
  83. (`noop`):
  84. [source,js]
  85. --------------------------------------------------
  86. POST test/_doc/1/_update
  87. {
  88. "script" : {
  89. "source": "if (ctx._source.tags.contains(params.tag)) { ctx.op = 'delete' } else { ctx.op = 'none' }",
  90. "lang": "painless",
  91. "params" : {
  92. "tag" : "green"
  93. }
  94. }
  95. }
  96. --------------------------------------------------
  97. // CONSOLE
  98. // TEST[continued]
  99. [float]
  100. === Updates with a partial document
  101. The update API also support passing a partial document,
  102. which will be merged into the existing document (simple recursive merge,
  103. inner merging of objects, replacing core "keys/values" and arrays). For
  104. example:
  105. [source,js]
  106. --------------------------------------------------
  107. POST test/_doc/1/_update
  108. {
  109. "doc" : {
  110. "name" : "new_name"
  111. }
  112. }
  113. --------------------------------------------------
  114. // CONSOLE
  115. // TEST[continued]
  116. If both `doc` and `script` are specified, then `doc` is ignored. Best is
  117. to put your field pairs of the partial document in the script itself.
  118. [float]
  119. === Detecting noop updates
  120. If `doc` is specified its value is merged with the existing `_source`.
  121. By default updates that don't change anything detect that they don't change anything and return "result": "noop" like this:
  122. [source,js]
  123. --------------------------------------------------
  124. POST test/_doc/1/_update
  125. {
  126. "doc" : {
  127. "name" : "new_name"
  128. }
  129. }
  130. --------------------------------------------------
  131. // CONSOLE
  132. // TEST[continued]
  133. If `name` was `new_name` before the request was sent then the entire update
  134. request is ignored. The `result` element in the response returns `noop` if
  135. the request was ignored.
  136. [source,js]
  137. --------------------------------------------------
  138. {
  139. "_shards": {
  140. "total": 0,
  141. "successful": 0,
  142. "failed": 0
  143. },
  144. "_index": "test",
  145. "_type": "_doc",
  146. "_id": "1",
  147. "_version": 6,
  148. "result": "noop"
  149. }
  150. --------------------------------------------------
  151. // TESTRESPONSE
  152. You can disable this behavior by setting "detect_noop": false like this:
  153. [source,js]
  154. --------------------------------------------------
  155. POST test/_doc/1/_update
  156. {
  157. "doc" : {
  158. "name" : "new_name"
  159. },
  160. "detect_noop": false
  161. }
  162. --------------------------------------------------
  163. // CONSOLE
  164. // TEST[continued]
  165. [[upserts]]
  166. [float]
  167. === Upserts
  168. If the document does not already exist, the contents of the `upsert` element
  169. will be inserted as a new document. If the document does exist, then the
  170. `script` will be executed instead:
  171. [source,js]
  172. --------------------------------------------------
  173. POST test/_doc/1/_update
  174. {
  175. "script" : {
  176. "source": "ctx._source.counter += params.count",
  177. "lang": "painless",
  178. "params" : {
  179. "count" : 4
  180. }
  181. },
  182. "upsert" : {
  183. "counter" : 1
  184. }
  185. }
  186. --------------------------------------------------
  187. // CONSOLE
  188. // TEST[continued]
  189. [float]
  190. ==== `scripted_upsert`
  191. If you would like your script to run regardless of whether the document exists
  192. or not -- i.e. the script handles initializing the document instead of the
  193. `upsert` element -- then set `scripted_upsert` to `true`:
  194. [source,js]
  195. --------------------------------------------------
  196. POST sessions/session/dh3sgudg8gsrgl/_update
  197. {
  198. "scripted_upsert":true,
  199. "script" : {
  200. "id": "my_web_session_summariser",
  201. "params" : {
  202. "pageViewEvent" : {
  203. "url":"foo.com/bar",
  204. "response":404,
  205. "time":"2014-01-01 12:32"
  206. }
  207. }
  208. },
  209. "upsert" : {}
  210. }
  211. --------------------------------------------------
  212. // CONSOLE
  213. // TEST[s/"id": "my_web_session_summariser"/"source": "ctx._source.page_view_event = params.pageViewEvent"/]
  214. // TEST[continued]
  215. [float]
  216. ==== `doc_as_upsert`
  217. Instead of sending a partial `doc` plus an `upsert` doc, setting
  218. `doc_as_upsert` to `true` will use the contents of `doc` as the `upsert`
  219. value:
  220. [source,js]
  221. --------------------------------------------------
  222. POST test/_doc/1/_update
  223. {
  224. "doc" : {
  225. "name" : "new_name"
  226. },
  227. "doc_as_upsert" : true
  228. }
  229. --------------------------------------------------
  230. // CONSOLE
  231. // TEST[continued]
  232. [float]
  233. === Parameters
  234. The update operation supports the following query-string parameters:
  235. [horizontal]
  236. `retry_on_conflict`::
  237. In between the get and indexing phases of the update, it is possible that
  238. another process might have already updated the same document. By default, the
  239. update will fail with a version conflict exception. The `retry_on_conflict`
  240. parameter controls how many times to retry the update before finally throwing
  241. an exception.
  242. `routing`::
  243. Routing is used to route the update request to the right shard and sets the
  244. routing for the upsert request if the document being updated doesn't exist.
  245. Can't be used to update the routing of an existing document.
  246. `timeout`::
  247. Timeout waiting for a shard to become available.
  248. `wait_for_active_shards`::
  249. The number of shard copies required to be active before proceeding with the update operation.
  250. See <<index-wait-for-active-shards,here>> for details.
  251. `refresh`::
  252. Control when the changes made by this request are visible to search. See
  253. <<docs-refresh>>.
  254. `_source`::
  255. Allows to control if and how the updated source should be returned in the response.
  256. By default the updated source is not returned.
  257. See <<search-request-source-filtering, `source filtering`>> for details.
  258. `version`::
  259. The update API uses the Elasticsearch's versioning support internally to make
  260. sure the document doesn't change during the update. You can use the `version`
  261. parameter to specify that the document should only be updated if its version
  262. matches the one specified.
  263. [NOTE]
  264. .The update API does not support versioning other than internal
  265. =====================================================
  266. External (version types `external` & `external_gte`) or forced (version type `force`)
  267. versioning is not supported by the update API as it would result in Elasticsearch
  268. version numbers being out of sync with the external system. Use the
  269. <<docs-index_,`index` API>> instead.
  270. =====================================================