update.asciidoc 12 KB

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