update-by-query.asciidoc 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. --
  2. :api: update-by-query
  3. :request: UpdateByQueryRequest
  4. :response: UpdateByQueryResponse
  5. --
  6. [id="{upid}-{api}"]
  7. === Update By Query API
  8. [id="{upid}-{api}-request"]
  9. ==== Update By Query Request
  10. A +{request}+ can be used to update documents in an index.
  11. It requires an existing index (or a set of indices) on which the update is to
  12. be performed.
  13. The simplest form of a +{request}+ looks like this:
  14. ["source","java",subs="attributes,callouts,macros"]
  15. --------------------------------------------------
  16. include-tagged::{doc-tests-file}[{api}-request]
  17. --------------------------------------------------
  18. <1> Creates the +{request}+ on a set of indices.
  19. By default version conflicts abort the +{request}+ process but you can just
  20. count them instead with:
  21. ["source","java",subs="attributes,callouts,macros"]
  22. --------------------------------------------------
  23. include-tagged::{doc-tests-file}[{api}-request-conflicts]
  24. --------------------------------------------------
  25. <1> Set `proceed` on version conflict
  26. You can limit the documents by adding a query.
  27. ["source","java",subs="attributes,callouts,macros"]
  28. --------------------------------------------------
  29. include-tagged::{doc-tests-file}[{api}-request-query]
  30. --------------------------------------------------
  31. <1> Only copy documents which have field `user` set to `kimchy`
  32. It’s also possible to limit the number of processed documents by setting size.
  33. ["source","java",subs="attributes,callouts,macros"]
  34. --------------------------------------------------
  35. include-tagged::{doc-tests-file}[{api}-request-size]
  36. --------------------------------------------------
  37. <1> Only copy 10 documents
  38. By default +{request}+ uses batches of 1000. You can change the batch size with
  39. `setBatchSize`.
  40. ["source","java",subs="attributes,callouts,macros"]
  41. --------------------------------------------------
  42. include-tagged::{doc-tests-file}[{api}-request-scrollSize]
  43. --------------------------------------------------
  44. <1> Use batches of 100 documents
  45. Update by query can also use the ingest feature by specifying a `pipeline`.
  46. ["source","java",subs="attributes,callouts,macros"]
  47. --------------------------------------------------
  48. include-tagged::{doc-tests-file}[{api}-request-pipeline]
  49. --------------------------------------------------
  50. <1> set pipeline to `my_pipeline`
  51. +{request}+ also supports a `script` that modifies the document:
  52. ["source","java",subs="attributes,callouts,macros"]
  53. --------------------------------------------------
  54. include-tagged::{doc-tests-file}[{api}-request-script]
  55. --------------------------------------------------
  56. <1> `setScript` to increment the `likes` field on all documents with user `kimchy`.
  57. +{request}+ can be parallelized using `sliced-scroll` with `setSlices`:
  58. ["source","java",subs="attributes,callouts,macros"]
  59. --------------------------------------------------
  60. include-tagged::{doc-tests-file}[{api}-request-slices]
  61. --------------------------------------------------
  62. <1> set number of slices to use
  63. `UpdateByQueryRequest` uses the `scroll` parameter to control how long it keeps the "search context" alive.
  64. ["source","java",subs="attributes,callouts,macros"]
  65. --------------------------------------------------
  66. include-tagged::{doc-tests-file}[{api}-request-scroll]
  67. --------------------------------------------------
  68. <1> set scroll time
  69. If you provide routing then the routing is copied to the scroll query, limiting the process to the shards that match
  70. that routing value.
  71. ["source","java",subs="attributes,callouts,macros"]
  72. --------------------------------------------------
  73. include-tagged::{doc-tests-file}[{api}-request-routing]
  74. --------------------------------------------------
  75. <1> set routing
  76. ==== Optional arguments
  77. In addition to the options above the following arguments can optionally be also provided:
  78. ["source","java",subs="attributes,callouts,macros"]
  79. --------------------------------------------------
  80. include-tagged::{doc-tests-file}[{api}-request-timeout]
  81. --------------------------------------------------
  82. <1> Timeout to wait for the update by query request to be performed as a `TimeValue`
  83. ["source","java",subs="attributes,callouts,macros"]
  84. --------------------------------------------------
  85. include-tagged::{doc-tests-file}[{api}-request-refresh]
  86. --------------------------------------------------
  87. <1> Refresh index after calling update by query
  88. ["source","java",subs="attributes,callouts,macros"]
  89. --------------------------------------------------
  90. include-tagged::{doc-tests-file}[{api}-request-indicesOptions]
  91. --------------------------------------------------
  92. <1> Set indices options
  93. include::../execution.asciidoc[]
  94. [id="{upid}-{api}-response"]
  95. ==== Update By Query Response
  96. The returned +{response}+ contains information about the executed operations and
  97. allows to iterate over each result as follows:
  98. ["source","java",subs="attributes,callouts,macros"]
  99. --------------------------------------------------
  100. include-tagged::{doc-tests-file}[{api}-response]
  101. --------------------------------------------------
  102. <1> Get total time taken
  103. <2> Check if the request timed out
  104. <3> Get total number of docs processed
  105. <4> Number of docs that were updated
  106. <5> Number of docs that were deleted
  107. <6> Number of batches that were executed
  108. <7> Number of skipped docs
  109. <8> Number of version conflicts
  110. <9> Number of times request had to retry bulk index operations
  111. <10> Number of times request had to retry search operations
  112. <11> The total time this request has throttled itself not including the current throttle time if it is currently sleeping
  113. <12> Remaining delay of any current throttle sleep or 0 if not sleeping
  114. <13> Failures during search phase
  115. <14> Failures during bulk index operation