update.asciidoc 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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. There is also support for `upsert`. If the document does
  93. not already exists, the content of the `upsert` element will be used to
  94. index the fresh doc:
  95. [source,js]
  96. --------------------------------------------------
  97. curl -XPOST 'localhost:9200/test/type1/1/_update' -d '{
  98. "script" : "ctx._source.counter += count",
  99. "params" : {
  100. "count" : 4
  101. },
  102. "upsert" : {
  103. "counter" : 1
  104. }
  105. }'
  106. --------------------------------------------------
  107. Last it also supports `doc_as_upsert`. So that the
  108. provided document will be inserted if the document does not already
  109. exist. This will reduce the amount of data that needs to be sent to
  110. elasticsearch.
  111. [source,js]
  112. --------------------------------------------------
  113. curl -XPOST 'localhost:9200/test/type1/1/_update' -d '{
  114. "doc" : {
  115. "name" : "new_name"
  116. },
  117. "doc_as_upsert" : true
  118. }'
  119. --------------------------------------------------
  120. The update operation supports similar parameters as the index API,
  121. including:
  122. [horizontal]
  123. `routing`:: Sets the routing that will be used to route the
  124. document to the relevant shard.
  125. `parent`:: Simply sets the routing.
  126. `timeout`:: Timeout waiting for a shard to become available.
  127. `replication`:: The replication type for the delete/index operation
  128. (sync or async).
  129. `consistency`:: The write consistency of the index/delete operation.
  130. `refresh`:: Refresh the index immediately after the operation occurs,
  131. so that the updated document appears in search results
  132. immediately.
  133. `fields`:: return the relevant fields from the updated document.
  134. Support `_source` to return the full updated
  135. source.
  136. And also support `retry_on_conflict` which controls how many times to
  137. retry if there is a version conflict between getting the document and
  138. indexing / deleting it. Defaults to `0`.
  139. It also allows to update the `ttl` of a document using `ctx._ttl` and
  140. timestamp using `ctx._timestamp`. Note that if the timestamp is not
  141. updated and not extracted from the `_source` it will be set to the
  142. update date.