1
0

update.asciidoc 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  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 need to be enabled
  12. for this feature to work.
  13. For example, lets index a simple doc:
  14. [source,js]
  15. --------------------------------------------------
  16. curl -XPUT localhost:9200/test/type1/1 -d '{
  17. "counter" : 1,
  18. "tags" : ["red"]
  19. }'
  20. --------------------------------------------------
  21. [float]
  22. === Scripted updates
  23. Now, we can execute a script that would increment the counter:
  24. [source,js]
  25. --------------------------------------------------
  26. curl -XPOST 'localhost:9200/test/type1/1/_update' -d '{
  27. "script" : {
  28. "inline": "ctx._source.counter += count",
  29. "params" : {
  30. "count" : 4
  31. }
  32. }
  33. }'
  34. --------------------------------------------------
  35. We can add a tag to the list of tags (note, if the tag exists, it
  36. will still add it, since its a list):
  37. [source,js]
  38. --------------------------------------------------
  39. curl -XPOST 'localhost:9200/test/type1/1/_update' -d '{
  40. "script" : {
  41. "inline": "ctx._source.tags += tag",
  42. "params" : {
  43. "tag" : "blue"
  44. }
  45. }
  46. }'
  47. --------------------------------------------------
  48. In addition to `_source`, the following variables are available through
  49. the `ctx` map: `_index`, `_type`, `_id`, `_version`, `_routing`,
  50. `_parent`, `_timestamp`, `_ttl`.
  51. We can also add a new field to the document:
  52. [source,js]
  53. --------------------------------------------------
  54. curl -XPOST 'localhost:9200/test/type1/1/_update' -d '{
  55. "script" : "ctx._source.name_of_new_field = \"value_of_new_field\""
  56. }'
  57. --------------------------------------------------
  58. Or remove a field from the document:
  59. [source,js]
  60. --------------------------------------------------
  61. curl -XPOST 'localhost:9200/test/type1/1/_update' -d '{
  62. "script" : "ctx._source.remove(\"name_of_field\")"
  63. }'
  64. --------------------------------------------------
  65. And, we can even change the operation that is executed. This example deletes
  66. the doc if the `tags` field contain `blue`, otherwise it does nothing
  67. (`noop`):
  68. [source,js]
  69. --------------------------------------------------
  70. curl -XPOST 'localhost:9200/test/type1/1/_update' -d '{
  71. "script" : {
  72. "inline": "ctx._source.tags.contains(tag) ? ctx.op = \"delete\" : ctx.op = \"none\"",
  73. "params" : {
  74. "tag" : "blue"
  75. }
  76. }
  77. }'
  78. --------------------------------------------------
  79. [float]
  80. === Updates with a partial document
  81. The update API also support passing a partial document,
  82. which will be merged into the existing document (simple recursive merge,
  83. inner merging of objects, replacing core "keys/values" and arrays). For
  84. example:
  85. [source,js]
  86. --------------------------------------------------
  87. curl -XPOST 'localhost:9200/test/type1/1/_update' -d '{
  88. "doc" : {
  89. "name" : "new_name"
  90. }
  91. }'
  92. --------------------------------------------------
  93. If both `doc` and `script` is specified, then `doc` is ignored. Best is
  94. to put your field pairs of the partial document in the script itself.
  95. [float]
  96. === Detecting noop updates
  97. If `doc` is specified its value is merged with the existing `_source`. By
  98. default the document is only reindexed if the new `_source` field differs from
  99. the old. Setting `detect_noop` to `false` will cause Elasticsearch to always
  100. update the document even if it hasn't changed. For example:
  101. [source,js]
  102. --------------------------------------------------
  103. curl -XPOST 'localhost:9200/test/type1/1/_update' -d '{
  104. "doc" : {
  105. "name" : "new_name"
  106. },
  107. "detect_noop": false
  108. }'
  109. --------------------------------------------------
  110. If `name` was `new_name` before the request was sent then document is still
  111. reindexed.
  112. [[upserts]]
  113. [float]
  114. === Upserts
  115. If the document does not already exist, the contents of the `upsert` element
  116. will be inserted as a new document. If the document does exist, then the
  117. `script` will be executed instead:
  118. [source,js]
  119. --------------------------------------------------
  120. curl -XPOST 'localhost:9200/test/type1/1/_update' -d '{
  121. "script" : {
  122. "inline": "ctx._source.counter += count",
  123. "params" : {
  124. "count" : 4
  125. }
  126. },
  127. "upsert" : {
  128. "counter" : 1
  129. }
  130. }'
  131. --------------------------------------------------
  132. [float]
  133. ==== `scripted_upsert`
  134. If you would like your script to run regardless of whether the document exists
  135. or not -- i.e. the script handles initializing the document instead of the
  136. `upsert` element -- then set `scripted_upsert` to `true`:
  137. [source,js]
  138. --------------------------------------------------
  139. curl -XPOST 'localhost:9200/sessions/session/dh3sgudg8gsrgl/_update' -d '{
  140. "scripted_upsert":true,
  141. "script" : {
  142. "id": "my_web_session_summariser",
  143. "params" : {
  144. "pageViewEvent" : {
  145. "url":"foo.com/bar",
  146. "response":404,
  147. "time":"2014-01-01 12:32"
  148. }
  149. }
  150. },
  151. "upsert" : {}
  152. }'
  153. --------------------------------------------------
  154. [float]
  155. ==== `doc_as_upsert`
  156. Instead of sending a partial `doc` plus an `upsert` doc, setting
  157. `doc_as_upsert` to `true` will use the contents of `doc` as the `upsert`
  158. value:
  159. [source,js]
  160. --------------------------------------------------
  161. curl -XPOST 'localhost:9200/test/type1/1/_update' -d '{
  162. "doc" : {
  163. "name" : "new_name"
  164. },
  165. "doc_as_upsert" : true
  166. }'
  167. --------------------------------------------------
  168. [float]
  169. === Parameters
  170. The update operation supports the following query-string parameters:
  171. [horizontal]
  172. `retry_on_conflict`::
  173. In between the get and indexing phases of the update, it is possible that
  174. another process might have already updated the same document. By default, the
  175. update will fail with a version conflict exception. The `retry_on_conflict`
  176. parameter controls how many times to retry the update before finally throwing
  177. an exception.
  178. `routing`::
  179. Routing is used to route the update request to the right shard and sets the
  180. routing for the upsert request if the document being updated doesn't exist.
  181. Can't be used to update the routing of an existing document.
  182. `parent`::
  183. Parent is used to route the update request to the right shard and sets the
  184. parent for the upsert request if the document being updated doesn't exist.
  185. Can't be used to update the `parent` of an existing document.
  186. If an alias index routing is specified then it overrides the parent routing and it is used to route the request.
  187. `timeout`::
  188. Timeout waiting for a shard to become available.
  189. `consistency`::
  190. The write consistency of the index/delete operation.
  191. `refresh`::
  192. Refresh the relevant primary and replica shards (not the whole index)
  193. immediately after the operation occurs, so that the updated document appears
  194. in search results immediately.
  195. `fields`::
  196. Return the relevant fields from the updated document. Specify `_source` to
  197. return the full updated source.
  198. `version` & `version_type`::
  199. The update API uses the Elasticsearch's versioning support internally to make
  200. sure the document doesn't change during the update. You can use the `version`
  201. parameter to specify that the document should only be updated if its version
  202. matches the one specified. By setting version type to `force` you can force
  203. the new version of the document after update (use with care! with `force`
  204. there is no guarantee the document didn't change).
  205. [NOTE]
  206. .The update API does not support external versioning
  207. =====================================================
  208. External versioning (version types `external` & `external_gte`) is not
  209. supported by the update API as it would result in Elasticsearch version
  210. numbers being out of sync with the external system. Use the
  211. <<docs-index,`index` API>> instead.
  212. =====================================================