reindex.asciidoc 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. --
  2. :api: reindex
  3. :request: ReindexRequest
  4. :response: BulkByScrollResponse
  5. --
  6. [id="{upid}-{api}"]
  7. === Reindex API
  8. [id="{upid}-{api}-request"]
  9. ==== Reindex Request
  10. A +{request}+ can be used to copy documents from one or more indexes into a
  11. destination index.
  12. It requires an existing source index and a target index which may or may not exist pre-request. Reindex does not attempt
  13. to set up the destination index. It does not copy the settings of the source index. You should set up the destination
  14. index prior to running a _reindex action, including setting up mappings, shard counts, replicas, etc.
  15. The simplest form of a +{request}+ looks like this:
  16. ["source","java",subs="attributes,callouts,macros"]
  17. --------------------------------------------------
  18. include-tagged::{doc-tests-file}[{api}-request]
  19. --------------------------------------------------
  20. <1> Creates the +{request}+
  21. <2> Adds a list of sources to copy from
  22. <3> Adds the destination index
  23. The `dest` element can be configured like the index API to control optimistic concurrency control. Just leaving out
  24. `versionType` (as above) or setting it to internal will cause Elasticsearch to blindly dump documents into the target.
  25. Setting `versionType` to external will cause Elasticsearch to preserve the version from the source, create any documents
  26. that are missing, and update any documents that have an older version in the destination index than they do in the
  27. source index.
  28. ["source","java",subs="attributes,callouts,macros"]
  29. --------------------------------------------------
  30. include-tagged::{doc-tests-file}[{api}-request-versionType]
  31. --------------------------------------------------
  32. <1> Set the versionType to `EXTERNAL`
  33. Setting `opType` to `create` will cause `_reindex` to only create missing documents in the target index. All existing
  34. documents will cause a version conflict. The default `opType` is `index`.
  35. ["source","java",subs="attributes,callouts,macros"]
  36. --------------------------------------------------
  37. include-tagged::{doc-tests-file}[{api}-request-opType]
  38. --------------------------------------------------
  39. <1> Set the opType to `create`
  40. By default version conflicts abort the `_reindex` process but you can just count
  41. them instead with:
  42. ["source","java",subs="attributes,callouts,macros"]
  43. --------------------------------------------------
  44. include-tagged::{doc-tests-file}[{api}-request-conflicts]
  45. --------------------------------------------------
  46. <1> Set `proceed` on version conflict
  47. You can limit the documents by adding a query.
  48. ["source","java",subs="attributes,callouts,macros"]
  49. --------------------------------------------------
  50. include-tagged::{doc-tests-file}[{api}-request-query]
  51. --------------------------------------------------
  52. <1> Only copy documents which have field `user` set to `kimchy`
  53. It’s also possible to limit the number of processed documents by setting `maxDocs`.
  54. ["source","java",subs="attributes,callouts,macros"]
  55. --------------------------------------------------
  56. include-tagged::{doc-tests-file}[{api}-request-maxDocs]
  57. --------------------------------------------------
  58. <1> Only copy 10 documents
  59. By default `_reindex` uses batches of 1000. You can change the batch size with `sourceBatchSize`.
  60. ["source","java",subs="attributes,callouts,macros"]
  61. --------------------------------------------------
  62. include-tagged::{doc-tests-file}[{api}-request-sourceSize]
  63. --------------------------------------------------
  64. <1> Use batches of 100 documents
  65. Reindex can also use the ingest feature by specifying a `pipeline`.
  66. ["source","java",subs="attributes,callouts,macros"]
  67. --------------------------------------------------
  68. include-tagged::{doc-tests-file}[{api}-request-pipeline]
  69. --------------------------------------------------
  70. <1> set pipeline to `my_pipeline`
  71. +{request}+ also supports a `script` that modifies the document. It allows you to
  72. also change the document's metadata. The following example illustrates that.
  73. ["source","java",subs="attributes,callouts,macros"]
  74. --------------------------------------------------
  75. include-tagged::{doc-tests-file}[{api}-request-script]
  76. --------------------------------------------------
  77. <1> `setScript` to increment the `likes` field on all documents with user `kimchy`.
  78. +{request}+ supports reindexing from a remote Elasticsearch cluster. When using a remote cluster the query should be
  79. specified inside the `RemoteInfo` object and not using `setSourceQuery`. If both the remote info and the source query are
  80. set it results in a validation error during the request. The reason for this is that the remote Elasticsearch may not
  81. understand queries built by the modern query builders. The remote cluster support works all the way back to Elasticsearch
  82. 0.90 and the query language has changed since then. When reaching older versions, it is safer to write the query by hand
  83. in JSON.
  84. ["source","java",subs="attributes,callouts,macros"]
  85. --------------------------------------------------
  86. include-tagged::{doc-tests-file}[{api}-request-remote]
  87. --------------------------------------------------
  88. <1> set remote elastic cluster
  89. +{request}+ also helps in automatically parallelizing using `sliced-scroll` to
  90. slice on `_id`. Use `setSlices` to specify the number of slices to use.
  91. ["source","java",subs="attributes,callouts,macros"]
  92. --------------------------------------------------
  93. include-tagged::{doc-tests-file}[{api}-request-slices]
  94. --------------------------------------------------
  95. <1> set number of slices to use
  96. +{request}+ uses the `scroll` parameter to control how long it keeps the
  97. "search context" alive.
  98. ["source","java",subs="attributes,callouts,macros"]
  99. --------------------------------------------------
  100. include-tagged::{doc-tests-file}[{api}-request-scroll]
  101. --------------------------------------------------
  102. <1> set scroll time
  103. ==== Optional arguments
  104. In addition to the options above the following arguments can optionally be also provided:
  105. ["source","java",subs="attributes,callouts,macros"]
  106. --------------------------------------------------
  107. include-tagged::{doc-tests-file}[{api}-request-timeout]
  108. --------------------------------------------------
  109. <1> Timeout to wait for the reindex request to be performed as a `TimeValue`
  110. ["source","java",subs="attributes,callouts,macros"]
  111. --------------------------------------------------
  112. include-tagged::{doc-tests-file}[{api}-request-refresh]
  113. --------------------------------------------------
  114. <1> Refresh index after calling reindex
  115. include::../execution.asciidoc[]
  116. [id="{upid}-{api}-task-submission"]
  117. ==== Reindex task submission
  118. It is also possible to submit a +{request}+ and not wait for it completion with the use of Task API. This is an equivalent of a REST request
  119. with wait_for_completion flag set to false.
  120. ["source","java",subs="attributes,callouts,macros"]
  121. --------------------------------------------------
  122. include-tagged::{hlrc-tests}/ReindexIT.java[submit-reindex-task]
  123. --------------------------------------------------
  124. <1> A +{request}+ is constructed the same way as for the synchronous method
  125. <2> A submit method returns a `TaskSubmissionResponse` which contains a task identifier.
  126. <3> The task identifier can be used to get `response` from a completed task.
  127. [id="{upid}-{api}-response"]
  128. ==== Reindex Response
  129. The returned +{response}+ contains information about the executed operations and
  130. allows to iterate over each result as follows:
  131. ["source","java",subs="attributes,callouts,macros"]
  132. --------------------------------------------------
  133. include-tagged::{doc-tests-file}[{api}-response]
  134. --------------------------------------------------
  135. <1> Get total time taken
  136. <2> Check if the request timed out
  137. <3> Get total number of docs processed
  138. <4> Number of docs that were updated
  139. <5> Number of docs that were created
  140. <6> Number of docs that were deleted
  141. <7> Number of batches that were executed
  142. <8> Number of skipped docs
  143. <9> Number of version conflicts
  144. <10> Number of times request had to retry bulk index operations
  145. <11> Number of times request had to retry search operations
  146. <12> The total time this request has throttled itself not including the current throttle time if it is currently sleeping
  147. <13> Remaining delay of any current throttle sleep or 0 if not sleeping
  148. <14> Failures during search phase
  149. <15> Failures during bulk index operation