update.asciidoc 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. --
  2. :api: update
  3. :request: UpdateRequest
  4. :response: UpdateResponse
  5. --
  6. [id="{upid}-{api}"]
  7. === Update API
  8. [id="{upid}-{api}-request"]
  9. ==== Update Request
  10. An +{request}+ requires the following arguments:
  11. ["source","java",subs="attributes,callouts,macros"]
  12. --------------------------------------------------
  13. include-tagged::{doc-tests-file}[{api}-request]
  14. --------------------------------------------------
  15. <1> Index
  16. <2> Type
  17. <3> Document id
  18. The Update API allows to update an existing document by using a script
  19. or by passing a partial document.
  20. ==== Updates with a script
  21. The script can be provided as an inline script:
  22. ["source","java",subs="attributes,callouts,macros"]
  23. --------------------------------------------------
  24. include-tagged::{doc-tests-file}[{api}-request-with-inline-script]
  25. --------------------------------------------------
  26. <1> Script parameters provided as a `Map` of objects
  27. <2> Create an inline script using the `painless` language and the previous parameters
  28. <3> Sets the script to the update request
  29. Or as a stored script:
  30. ["source","java",subs="attributes,callouts,macros"]
  31. --------------------------------------------------
  32. include-tagged::{doc-tests-file}[{api}-request-with-stored-script]
  33. --------------------------------------------------
  34. <1> Reference to a script stored under the name `increment-field` in the `painless` language
  35. <2> Sets the script in the update request
  36. ==== Updates with a partial document
  37. When using updates with a partial document, the partial document will be merged with the
  38. existing document.
  39. The partial document can be provided in different ways:
  40. ["source","java",subs="attributes,callouts,macros"]
  41. --------------------------------------------------
  42. include-tagged::{doc-tests-file}[{api}-request-with-doc-as-string]
  43. --------------------------------------------------
  44. <1> Partial document source provided as a `String` in JSON format
  45. ["source","java",subs="attributes,callouts,macros"]
  46. --------------------------------------------------
  47. include-tagged::{doc-tests-file}[{api}-request-with-doc-as-map]
  48. --------------------------------------------------
  49. <1> Partial document source provided as a `Map` which gets automatically converted
  50. to JSON format
  51. ["source","java",subs="attributes,callouts,macros"]
  52. --------------------------------------------------
  53. include-tagged::{doc-tests-file}[{api}-request-with-doc-as-xcontent]
  54. --------------------------------------------------
  55. <1> Partial document source provided as an `XContentBuilder` object, the Elasticsearch
  56. built-in helpers to generate JSON content
  57. ["source","java",subs="attributes,callouts,macros"]
  58. --------------------------------------------------
  59. include-tagged::{doc-tests-file}[{api}-request-shortcut]
  60. --------------------------------------------------
  61. <1> Partial document source provided as `Object` key-pairs, which gets converted to
  62. JSON format
  63. ==== Upserts
  64. If the document does not already exist, it is possible to define some content that
  65. will be inserted as a new document using the `upsert` method:
  66. ["source","java",subs="attributes,callouts,macros"]
  67. --------------------------------------------------
  68. include-tagged::{doc-tests-file}[{api}-request-upsert]
  69. --------------------------------------------------
  70. <1> Upsert document source provided as a `String`
  71. Similarly to the partial document updates, the content of the `upsert` document
  72. can be defined using methods that accept `String`, `Map`, `XContentBuilder` or
  73. `Object` key-pairs.
  74. ==== Optional arguments
  75. The following arguments can optionally be provided:
  76. ["source","java",subs="attributes,callouts,macros"]
  77. --------------------------------------------------
  78. include-tagged::{doc-tests-file}[{api}-request-routing]
  79. --------------------------------------------------
  80. <1> Routing value
  81. ["source","java",subs="attributes,callouts,macros"]
  82. --------------------------------------------------
  83. include-tagged::{doc-tests-file}[{api}-request-timeout]
  84. --------------------------------------------------
  85. <1> Timeout to wait for primary shard to become available as a `TimeValue`
  86. <2> Timeout to wait for primary shard to become available as a `String`
  87. ["source","java",subs="attributes,callouts,macros"]
  88. --------------------------------------------------
  89. include-tagged::{doc-tests-file}[{api}-request-refresh]
  90. --------------------------------------------------
  91. <1> Refresh policy as a `WriteRequest.RefreshPolicy` instance
  92. <2> Refresh policy as a `String`
  93. ["source","java",subs="attributes,callouts,macros"]
  94. --------------------------------------------------
  95. include-tagged::{doc-tests-file}[{api}-request-retry]
  96. --------------------------------------------------
  97. <1> How many times to retry the update operation if the document to update has
  98. been changed by another operation between the get and indexing phases of the
  99. update operation
  100. ["source","java",subs="attributes,callouts,macros"]
  101. --------------------------------------------------
  102. include-tagged::{doc-tests-file}[{api}-request-no-source]
  103. --------------------------------------------------
  104. <1> Enable source retrieval, disabled by default
  105. ["source","java",subs="attributes,callouts,macros"]
  106. --------------------------------------------------
  107. include-tagged::{doc-tests-file}[{api}-request-source-include]
  108. --------------------------------------------------
  109. <1> Configure source inclusion for specific fields
  110. ["source","java",subs="attributes,callouts,macros"]
  111. --------------------------------------------------
  112. include-tagged::{doc-tests-file}[{api}-request-source-exclude]
  113. --------------------------------------------------
  114. <1> Configure source exclusion for specific fields
  115. ["source","java",subs="attributes,callouts,macros"]
  116. --------------------------------------------------
  117. include-tagged::{doc-tests-file}[{api}-request-version]
  118. --------------------------------------------------
  119. <1> Version
  120. ["source","java",subs="attributes,callouts,macros"]
  121. --------------------------------------------------
  122. include-tagged::{doc-tests-file}[{api}-request-detect-noop]
  123. --------------------------------------------------
  124. <1> Disable the noop detection
  125. ["source","java",subs="attributes,callouts,macros"]
  126. --------------------------------------------------
  127. include-tagged::{doc-tests-file}[{api}-request-scripted-upsert]
  128. --------------------------------------------------
  129. <1> Indicate that the script must run regardless of whether the document exists or not,
  130. ie the script takes care of creating the document if it does not already exist.
  131. ["source","java",subs="attributes,callouts,macros"]
  132. --------------------------------------------------
  133. include-tagged::{doc-tests-file}[{api}-request-doc-upsert]
  134. --------------------------------------------------
  135. <1> Indicate that the partial document must be used as the upsert document if it
  136. does not exist yet.
  137. ["source","java",subs="attributes,callouts,macros"]
  138. --------------------------------------------------
  139. include-tagged::{doc-tests-file}[{api}-request-active-shards]
  140. --------------------------------------------------
  141. <1> Sets the number of shard copies that must be active before proceeding with
  142. the update operation.
  143. <2> Number of shard copies provided as a `ActiveShardCount`: can be `ActiveShardCount.ALL`,
  144. `ActiveShardCount.ONE` or `ActiveShardCount.DEFAULT` (default)
  145. include::../execution.asciidoc[]
  146. [id="{upid}-{api}-response"]
  147. ==== Update Response
  148. The returned +{response}+ allows to retrieve information about the executed
  149. operation as follows:
  150. ["source","java",subs="attributes,callouts,macros"]
  151. --------------------------------------------------
  152. include-tagged::{doc-tests-file}[{api}-response]
  153. --------------------------------------------------
  154. <1> Handle the case where the document was created for the first time (upsert)
  155. <2> Handle the case where the document was updated
  156. <3> Handle the case where the document was deleted
  157. <4> Handle the case where the document was not impacted by the update,
  158. ie no operation (noop) was executed on the document
  159. When the source retrieval is enabled in the `UpdateRequest`
  160. through the fetchSource method, the response contains the
  161. source of the updated document:
  162. ["source","java",subs="attributes,callouts,macros"]
  163. --------------------------------------------------
  164. include-tagged::{doc-tests-file}[{api}-getresult]
  165. --------------------------------------------------
  166. <1> Retrieve the updated document as a `GetResult`
  167. <2> Retrieve the source of the updated document as a `String`
  168. <3> Retrieve the source of the updated document as a `Map<String, Object>`
  169. <4> Retrieve the source of the updated document as a `byte[]`
  170. <5> Handle the scenario where the source of the document is not present in
  171. the response (this is the case by default)
  172. It is also possible to check for shard failures:
  173. ["source","java",subs="attributes,callouts,macros"]
  174. --------------------------------------------------
  175. include-tagged::{doc-tests-file}[{api}-failure]
  176. --------------------------------------------------
  177. <1> Handle the situation where number of successful shards is less than
  178. total shards
  179. <2> Handle the potential failures
  180. When a `UpdateRequest` is performed against a document that does not exist,
  181. the response has `404` status code, an `ElasticsearchException` gets thrown
  182. which needs to be handled as follows:
  183. ["source","java",subs="attributes,callouts,macros"]
  184. --------------------------------------------------
  185. include-tagged::{doc-tests-file}[{api}-docnotfound]
  186. --------------------------------------------------
  187. <1> Handle the exception thrown because the document not exist
  188. If there is a version conflict, an `ElasticsearchException` will
  189. be thrown:
  190. ["source","java",subs="attributes,callouts,macros"]
  191. --------------------------------------------------
  192. include-tagged::{doc-tests-file}[{api}-conflict]
  193. --------------------------------------------------
  194. <1> The raised exception indicates that a version conflict error was returned.