update.asciidoc 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  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. Now, we can execute a script that would increment the counter:
  22. [source,js]
  23. --------------------------------------------------
  24. curl -XPOST 'localhost:9200/test/type1/1/_update' -d '{
  25. "script" : "ctx._source.counter += count",
  26. "params" : {
  27. "count" : 4
  28. }
  29. }'
  30. --------------------------------------------------
  31. We can also add a tag to the list of tags (note, if the tag exists, it
  32. will still add it, since its a list):
  33. [source,js]
  34. --------------------------------------------------
  35. curl -XPOST 'localhost:9200/test/type1/1/_update' -d '{
  36. "script" : "ctx._source.tags += tag",
  37. "params" : {
  38. "tag" : "blue"
  39. }
  40. }'
  41. --------------------------------------------------
  42. We can also add a new field to the document:
  43. [source,js]
  44. --------------------------------------------------
  45. curl -XPOST 'localhost:9200/test/type1/1/_update' -d '{
  46. "script" : "ctx._source.text = \"some text\""
  47. }'
  48. --------------------------------------------------
  49. We can also remove a field from the document:
  50. [source,js]
  51. --------------------------------------------------
  52. curl -XPOST 'localhost:9200/test/type1/1/_update' -d '{
  53. "script" : "ctx._source.remove(\"text\")"
  54. }'
  55. --------------------------------------------------
  56. And, we can delete the doc if the tags contain blue, or ignore (noop):
  57. [source,js]
  58. --------------------------------------------------
  59. curl -XPOST 'localhost:9200/test/type1/1/_update' -d '{
  60. "script" : "ctx._source.tags.contains(tag) ? ctx.op = \"delete\" : ctx.op = \"none\"",
  61. "params" : {
  62. "tag" : "blue"
  63. }
  64. }'
  65. --------------------------------------------------
  66. *Note*: Be aware of MVEL and handling of ternary operators and
  67. assignments. Assignment operations have lower precedence than the
  68. ternary operator. Compare the following statements:
  69. [source,js]
  70. --------------------------------------------------
  71. // Will NOT update the tags array
  72. ctx._source.tags.contains(tag) ? ctx.op = \"none\" : ctx._source.tags += tag
  73. // Will update
  74. ctx._source.tags.contains(tag) ? (ctx.op = \"none\") : ctx._source.tags += tag
  75. // Also works
  76. if (ctx._source.tags.contains(tag)) { ctx.op = \"none\" } else { ctx._source.tags += tag }
  77. --------------------------------------------------
  78. The update API also support passing a partial document,
  79. which will be merged into the existing document (simple recursive merge,
  80. inner merging of objects, replacing core "keys/values" and arrays). For
  81. example:
  82. [source,js]
  83. --------------------------------------------------
  84. curl -XPOST 'localhost:9200/test/type1/1/_update' -d '{
  85. "doc" : {
  86. "name" : "new_name"
  87. }
  88. }'
  89. --------------------------------------------------
  90. If both `doc` and `script` is specified, then `doc` is ignored. Best is
  91. to put your field pairs of the partial document in the script itself.
  92. By default if `doc` is specified then the document is always updated even
  93. if the merging process doesn't cause any changes. Specifying `detect_noop`
  94. as `true` will cause Elasticsearch to check if there are changes and, if
  95. there aren't, turn the update request into a noop. For example:
  96. [source,js]
  97. --------------------------------------------------
  98. curl -XPOST 'localhost:9200/test/type1/1/_update' -d '{
  99. "doc" : {
  100. "name" : "new_name"
  101. },
  102. "detect_noop": true
  103. }'
  104. --------------------------------------------------
  105. If `name` was `new_name` before the request was sent then the entire update
  106. request is ignored.
  107. === Upserts
  108. There is also support for `upsert`. If the document does
  109. not already exists, the content of the `upsert` element will be used to
  110. index the fresh doc:
  111. [source,js]
  112. --------------------------------------------------
  113. curl -XPOST 'localhost:9200/test/type1/1/_update' -d '{
  114. "script" : "ctx._source.counter += count",
  115. "params" : {
  116. "count" : 4
  117. },
  118. "upsert" : {
  119. "counter" : 1
  120. }
  121. }'
  122. --------------------------------------------------
  123. coming[1.4.0]
  124. If the document does not exist you may want your update script to
  125. run anyway in order to initialize the document contents using
  126. business logic unknown to the client. In this case pass the
  127. new `scripted_upsert` parameter with the value `true`.
  128. [source,js]
  129. --------------------------------------------------
  130. curl -XPOST 'localhost:9200/sessions/session/dh3sgudg8gsrgl/_update' -d '{
  131. "script_id" : "my_web_session_summariser",
  132. "scripted_upsert":true,
  133. "params" : {
  134. "pageViewEvent" : {
  135. "url":"foo.com/bar",
  136. "response":404,
  137. "time":"2014-01-01 12:32"
  138. }
  139. },
  140. "upsert" : {
  141. }
  142. }'
  143. --------------------------------------------------
  144. The default `scripted_upsert` setting is `false` meaning the script is not executed for inserts.
  145. However, in scenarios like the one above we may be using a non-trivial script stored
  146. using the new "indexed scripts" feature. The script may be deriving properties
  147. like the duration of our web session based on observing multiple page view events so the
  148. client can supply a blank "upsert" document and allow the script to fill in most of the details
  149. using the events passed in the `params` element.
  150. Last, the upsert facility also supports `doc_as_upsert`. So that the
  151. provided document will be inserted if the document does not already
  152. exist. This will reduce the amount of data that needs to be sent to
  153. elasticsearch.
  154. [source,js]
  155. --------------------------------------------------
  156. curl -XPOST 'localhost:9200/test/type1/1/_update' -d '{
  157. "doc" : {
  158. "name" : "new_name"
  159. },
  160. "doc_as_upsert" : true
  161. }'
  162. --------------------------------------------------
  163. === Parameters
  164. The update operation supports similar parameters as the index API,
  165. including:
  166. [horizontal]
  167. `routing`:: Sets the routing that will be used to route the
  168. document to the relevant shard.
  169. `parent`:: Simply sets the routing.
  170. `timeout`:: Timeout waiting for a shard to become available.
  171. `replication`:: The replication type for the delete/index operation
  172. (sync or async).
  173. `consistency`:: The write consistency of the index/delete operation.
  174. `refresh`:: Refresh the relevant primary and replica shards (not the whole
  175. index) immediately after the operation occurs, so that the
  176. updated document appears in search results immediately.
  177. `fields`:: return the relevant fields from the updated document.
  178. Support `_source` to return the full updated
  179. source.
  180. `version` & `version_type`:: the Update API uses the Elasticsearch's versioning
  181. support internally to make sure the document doesn't change
  182. during the update. You can use the `version` parameter to specify that the
  183. document should only be updated if it's version matches the one specified.
  184. By setting version type to `force` you can force the new version of the document
  185. after update (use with care! with `force` there is no guarantee the document
  186. didn't change).Version types `external` & `external_gte` are not supported.
  187. And also support `retry_on_conflict` which controls how many times to
  188. retry if there is a version conflict between getting the document and
  189. indexing / deleting it. Defaults to `0`.
  190. It also allows to update the `ttl` of a document using `ctx._ttl` and
  191. timestamp using `ctx._timestamp`. Note that if the timestamp is not
  192. updated and not extracted from the `_source` it will be set to the
  193. update date.