update.asciidoc 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  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 it's 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. We can remove a tag from the list of tags. Note that the Painless function to
  59. `remove` a tag takes as its parameter the array index of the element you wish
  60. to remove, so you need a bit more logic to locate it while avoiding a runtime
  61. error. Note that if the tag was present more than once in the list, this will
  62. remove only one occurrence of it:
  63. [source,js]
  64. --------------------------------------------------
  65. POST test/_doc/1/_update
  66. {
  67. "script" : {
  68. "source": "if (ctx._source.tags.contains(params.tag)) { ctx._source.tags.remove(ctx._source.tags.indexOf(params.tag)) }",
  69. "lang": "painless",
  70. "params" : {
  71. "tag" : "blue"
  72. }
  73. }
  74. }
  75. --------------------------------------------------
  76. // CONSOLE
  77. // TEST[continued]
  78. In addition to `_source`, the following variables are available through
  79. the `ctx` map: `_index`, `_type`, `_id`, `_version`, `_routing`
  80. and `_now` (the current timestamp).
  81. We can also add a new field to the document:
  82. [source,js]
  83. --------------------------------------------------
  84. POST test/_doc/1/_update
  85. {
  86. "script" : "ctx._source.new_field = 'value_of_new_field'"
  87. }
  88. --------------------------------------------------
  89. // CONSOLE
  90. // TEST[continued]
  91. Or remove a field from the document:
  92. [source,js]
  93. --------------------------------------------------
  94. POST test/_doc/1/_update
  95. {
  96. "script" : "ctx._source.remove('new_field')"
  97. }
  98. --------------------------------------------------
  99. // CONSOLE
  100. // TEST[continued]
  101. And, we can even change the operation that is executed. This example deletes
  102. the doc if the `tags` field contain `green`, otherwise it does nothing
  103. (`noop`):
  104. [source,js]
  105. --------------------------------------------------
  106. POST test/_doc/1/_update
  107. {
  108. "script" : {
  109. "source": "if (ctx._source.tags.contains(params.tag)) { ctx.op = 'delete' } else { ctx.op = 'none' }",
  110. "lang": "painless",
  111. "params" : {
  112. "tag" : "green"
  113. }
  114. }
  115. }
  116. --------------------------------------------------
  117. // CONSOLE
  118. // TEST[continued]
  119. [float]
  120. === Updates with a partial document
  121. The update API also support passing a partial document,
  122. which will be merged into the existing document (simple recursive merge,
  123. inner merging of objects, replacing core "keys/values" and arrays).
  124. To fully replace the existing document, the <<docs-index_,`index` API>> should
  125. be used instead.
  126. The following partial update adds a new field to the
  127. existing document:
  128. [source,js]
  129. --------------------------------------------------
  130. POST test/_doc/1/_update
  131. {
  132. "doc" : {
  133. "name" : "new_name"
  134. }
  135. }
  136. --------------------------------------------------
  137. // CONSOLE
  138. // TEST[continued]
  139. If both `doc` and `script` are specified, then `doc` is ignored. Best is
  140. to put your field pairs of the partial document in the script itself.
  141. [float]
  142. === Detecting noop updates
  143. If `doc` is specified its value is merged with the existing `_source`.
  144. By default updates that don't change anything detect that they don't change anything and return "result": "noop" like this:
  145. [source,js]
  146. --------------------------------------------------
  147. POST test/_doc/1/_update
  148. {
  149. "doc" : {
  150. "name" : "new_name"
  151. }
  152. }
  153. --------------------------------------------------
  154. // CONSOLE
  155. // TEST[continued]
  156. If `name` was `new_name` before the request was sent then the entire update
  157. request is ignored. The `result` element in the response returns `noop` if
  158. the request was ignored.
  159. [source,js]
  160. --------------------------------------------------
  161. {
  162. "_shards": {
  163. "total": 0,
  164. "successful": 0,
  165. "failed": 0
  166. },
  167. "_index": "test",
  168. "_type": "_doc",
  169. "_id": "1",
  170. "_version": 7,
  171. "result": "noop"
  172. }
  173. --------------------------------------------------
  174. // TESTRESPONSE
  175. You can disable this behavior by setting "detect_noop": false like this:
  176. [source,js]
  177. --------------------------------------------------
  178. POST test/_doc/1/_update
  179. {
  180. "doc" : {
  181. "name" : "new_name"
  182. },
  183. "detect_noop": false
  184. }
  185. --------------------------------------------------
  186. // CONSOLE
  187. // TEST[continued]
  188. [[upserts]]
  189. [float]
  190. === Upserts
  191. If the document does not already exist, the contents of the `upsert` element
  192. will be inserted as a new document. If the document does exist, then the
  193. `script` will be executed instead:
  194. [source,js]
  195. --------------------------------------------------
  196. POST test/_doc/1/_update
  197. {
  198. "script" : {
  199. "source": "ctx._source.counter += params.count",
  200. "lang": "painless",
  201. "params" : {
  202. "count" : 4
  203. }
  204. },
  205. "upsert" : {
  206. "counter" : 1
  207. }
  208. }
  209. --------------------------------------------------
  210. // CONSOLE
  211. // TEST[continued]
  212. [float]
  213. ==== `scripted_upsert`
  214. If you would like your script to run regardless of whether the document exists
  215. or not -- i.e. the script handles initializing the document instead of the
  216. `upsert` element -- then set `scripted_upsert` to `true`:
  217. [source,js]
  218. --------------------------------------------------
  219. POST sessions/session/dh3sgudg8gsrgl/_update
  220. {
  221. "scripted_upsert":true,
  222. "script" : {
  223. "id": "my_web_session_summariser",
  224. "params" : {
  225. "pageViewEvent" : {
  226. "url":"foo.com/bar",
  227. "response":404,
  228. "time":"2014-01-01 12:32"
  229. }
  230. }
  231. },
  232. "upsert" : {}
  233. }
  234. --------------------------------------------------
  235. // CONSOLE
  236. // TEST[s/"id": "my_web_session_summariser"/"source": "ctx._source.page_view_event = params.pageViewEvent"/]
  237. // TEST[continued]
  238. [float]
  239. ==== `doc_as_upsert`
  240. Instead of sending a partial `doc` plus an `upsert` doc, setting
  241. `doc_as_upsert` to `true` will use the contents of `doc` as the `upsert`
  242. value:
  243. [source,js]
  244. --------------------------------------------------
  245. POST test/_doc/1/_update
  246. {
  247. "doc" : {
  248. "name" : "new_name"
  249. },
  250. "doc_as_upsert" : true
  251. }
  252. --------------------------------------------------
  253. // CONSOLE
  254. // TEST[continued]
  255. [float]
  256. === Parameters
  257. The update operation supports the following query-string parameters:
  258. [horizontal]
  259. `retry_on_conflict`::
  260. In between the get and indexing phases of the update, it is possible that
  261. another process might have already updated the same document. By default, the
  262. update will fail with a version conflict exception. The `retry_on_conflict`
  263. parameter controls how many times to retry the update before finally throwing
  264. an exception.
  265. `routing`::
  266. Routing is used to route the update request to the right shard and sets the
  267. routing for the upsert request if the document being updated doesn't exist.
  268. Can't be used to update the routing of an existing document.
  269. `timeout`::
  270. Timeout waiting for a shard to become available.
  271. `wait_for_active_shards`::
  272. The number of shard copies required to be active before proceeding with the update operation.
  273. See <<index-wait-for-active-shards,here>> for details.
  274. `refresh`::
  275. Control when the changes made by this request are visible to search. See
  276. <<docs-refresh>>.
  277. `_source`::
  278. Allows to control if and how the updated source should be returned in the response.
  279. By default the updated source is not returned.
  280. See <<search-request-source-filtering, `source filtering`>> for details.
  281. `version`::
  282. The update API uses the Elasticsearch's versioning support internally to make
  283. sure the document doesn't change during the update. You can use the `version`
  284. parameter to specify that the document should only be updated if its version
  285. matches the one specified.
  286. [NOTE]
  287. .The update API does not support versioning other than internal
  288. =====================================================
  289. External (version types `external` & `external_gte`) or forced (version type `force`)
  290. versioning is not supported by the update API as it would result in Elasticsearch
  291. version numbers being out of sync with the external system. Use the
  292. <<docs-index_,`index` API>> instead.
  293. =====================================================