update.asciidoc 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  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-parent]
  79. --------------------------------------------------
  80. <1> Parent value
  81. ["source","java",subs="attributes,callouts,macros"]
  82. --------------------------------------------------
  83. include-tagged::{doc-tests}/CRUDDocumentationIT.java[update-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}/CRUDDocumentationIT.java[update-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}/CRUDDocumentationIT.java[update-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}/CRUDDocumentationIT.java[update-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}/CRUDDocumentationIT.java[update-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}/CRUDDocumentationIT.java[update-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}/CRUDDocumentationIT.java[update-request-version]
  118. --------------------------------------------------
  119. <1> Version
  120. ["source","java",subs="attributes,callouts,macros"]
  121. --------------------------------------------------
  122. include-tagged::{doc-tests}/CRUDDocumentationIT.java[update-request-detect-noop]
  123. --------------------------------------------------
  124. <1> Disable the noop detection
  125. ["source","java",subs="attributes,callouts,macros"]
  126. --------------------------------------------------
  127. include-tagged::{doc-tests}/CRUDDocumentationIT.java[update-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}/CRUDDocumentationIT.java[update-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}/CRUDDocumentationIT.java[update-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. [[java-rest-high-document-update-sync]]
  146. ==== Synchronous Execution
  147. ["source","java",subs="attributes,callouts,macros"]
  148. --------------------------------------------------
  149. include-tagged::{doc-tests}/CRUDDocumentationIT.java[update-execute]
  150. --------------------------------------------------
  151. [[java-rest-high-document-update-async]]
  152. ==== Asynchronous Execution
  153. The asynchronous execution of an update request requires both the `UpdateRequest`
  154. instance and an `ActionListener` instance to be passed to the asynchronous
  155. method:
  156. ["source","java",subs="attributes,callouts,macros"]
  157. --------------------------------------------------
  158. include-tagged::{doc-tests}/CRUDDocumentationIT.java[update-execute-async]
  159. --------------------------------------------------
  160. <1> The `UpdateRequest` to execute and the `ActionListener` to use when
  161. the execution completes
  162. The asynchronous method does not block and returns immediately. Once it is
  163. completed the `ActionListener` is called back using the `onResponse` method
  164. if the execution successfully completed or using the `onFailure` method if
  165. it failed.
  166. A typical listener for `UpdateResponse` looks like:
  167. ["source","java",subs="attributes,callouts,macros"]
  168. --------------------------------------------------
  169. include-tagged::{doc-tests}/CRUDDocumentationIT.java[update-execute-listener]
  170. --------------------------------------------------
  171. <1> Called when the execution is successfully completed. The response is
  172. provided as an argument.
  173. <2> Called in case of failure. The raised exception is provided as an argument.
  174. [[java-rest-high-document-update-response]]
  175. ==== Update Response
  176. The returned `UpdateResponse` allows to retrieve information about the executed
  177. operation as follows:
  178. ["source","java",subs="attributes,callouts,macros"]
  179. --------------------------------------------------
  180. include-tagged::{doc-tests}/CRUDDocumentationIT.java[update-response]
  181. --------------------------------------------------
  182. <1> Handle the case where the document was created for the first time (upsert)
  183. <2> Handle the case where the document was updated
  184. <3> Handle the case where the document was deleted
  185. <4> Handle the case where the document was not impacted by the update,
  186. ie no operation (noop) was executed on the document
  187. When the source retrieval is enabled in the `UpdateRequest`
  188. through the fetchSource method, the response contains the
  189. source of the updated document:
  190. ["source","java",subs="attributes,callouts,macros"]
  191. --------------------------------------------------
  192. include-tagged::{doc-tests}/CRUDDocumentationIT.java[update-getresult]
  193. --------------------------------------------------
  194. <1> Retrieve the updated document as a `GetResult`
  195. <2> Retrieve the source of the updated document as a `String`
  196. <3> Retrieve the source of the updated document as a `Map<String, Object>`
  197. <4> Retrieve the source of the updated document as a `byte[]`
  198. <5> Handle the scenario where the source of the document is not present in
  199. the response (this is the case by default)
  200. It is also possible to check for shard failures:
  201. ["source","java",subs="attributes,callouts,macros"]
  202. --------------------------------------------------
  203. include-tagged::{doc-tests}/CRUDDocumentationIT.java[update-failure]
  204. --------------------------------------------------
  205. <1> Handle the situation where number of successful shards is less than
  206. total shards
  207. <2> Handle the potential failures
  208. When a `UpdateRequest` is performed against a document that does not exist,
  209. the response has `404` status code, an `ElasticsearchException` gets thrown
  210. which needs to be handled as follows:
  211. ["source","java",subs="attributes,callouts,macros"]
  212. --------------------------------------------------
  213. include-tagged::{doc-tests}/CRUDDocumentationIT.java[update-docnotfound]
  214. --------------------------------------------------
  215. <1> Handle the exception thrown because the document not exist
  216. If there is a version conflict, an `ElasticsearchException` will
  217. be thrown:
  218. ["source","java",subs="attributes,callouts,macros"]
  219. --------------------------------------------------
  220. include-tagged::{doc-tests}/CRUDDocumentationIT.java[update-conflict]
  221. --------------------------------------------------
  222. <1> The raised exception indicates that a version conflict error was returned.