shrink-index.asciidoc 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. [[indices-shrink-index]]
  2. === Shrink index API
  3. ++++
  4. <titleabbrev>Shrink index</titleabbrev>
  5. ++++
  6. Shrinks an existing index into a new index with fewer primary shards.
  7. [source,console]
  8. ----
  9. POST /my-index-000001/_shrink/shrunk-my-index-000001
  10. ----
  11. // TEST[s/^/PUT my-index-000001\n{"settings":{"index.number_of_shards":2,"blocks.write":true}}\n/]
  12. [[shrink-index-api-request]]
  13. ==== {api-request-title}
  14. `POST /<index>/_shrink/<target-index>`
  15. `PUT /<index>/_shrink/<target-index>`
  16. [[shrink-index-api-prereqs]]
  17. ==== {api-prereq-title}
  18. Before you can shrink an index:
  19. * The index must be read-only.
  20. * All primary shards for the index must reside on the same node.
  21. * The index must have a `green` <<cluster-health,health status>>.
  22. To make shard allocation easier, we recommend you also remove the index's
  23. replica shards. You can later re-add replica shards as part of the shrink
  24. operation.
  25. You can use the following <<indices-update-settings,update index settings API>>
  26. request to remove an index's replica shards, relocates the index's remaining
  27. shards to the same node, and make the index read-only.
  28. [source,console]
  29. --------------------------------------------------
  30. PUT /my_source_index/_settings
  31. {
  32. "settings": {
  33. "index.number_of_replicas": 0, <1>
  34. "index.routing.allocation.require._name": "shrink_node_name", <2>
  35. "index.blocks.write": true <3>
  36. }
  37. }
  38. --------------------------------------------------
  39. // TEST[s/^/PUT my_source_index\n{"settings":{"index.number_of_shards":2}}\n/]
  40. <1> Removes replica shards for the index.
  41. <2> Relocates the index's shards to the `shrink_node_name` node.
  42. See <<shard-allocation-filtering>>.
  43. <3> Prevents write operations to this index. Metadata changes, such as deleting
  44. the index, are still allowed.
  45. It can take a while to relocate the source index. Progress can be tracked
  46. with the <<cat-recovery,`_cat recovery` API>>, or the <<cluster-health,
  47. `cluster health` API>> can be used to wait until all shards have relocated
  48. with the `wait_for_no_relocating_shards` parameter.
  49. [[shrink-index-api-desc]]
  50. ==== {api-description-title}
  51. The shrink index API allows you to shrink an existing index into a new index
  52. with fewer primary shards. The requested number of primary shards in the target index
  53. must be a factor of the number of shards in the source index. For example an index with
  54. `8` primary shards can be shrunk into `4`, `2` or `1` primary shards or an index
  55. with `15` primary shards can be shrunk into `5`, `3` or `1`. If the number
  56. of shards in the index is a prime number it can only be shrunk into a single
  57. primary shard. Before shrinking, a (primary or replica) copy of every shard
  58. in the index must be present on the same node.
  59. The current write index on a data stream cannot be shrunk. In order to shrink
  60. the current write index, the data stream must first be
  61. <<rollover-data-stream-ex,rolled over>> so that a new write index is created
  62. and then the previous write index can be shrunk.
  63. [[how-shrink-works]]
  64. ===== How shrinking works
  65. A shrink operation:
  66. . Creates a new target index with the same definition as the source
  67. index, but with a smaller number of primary shards.
  68. . Hard-links segments from the source index into the target index. (If
  69. the file system doesn't support hard-linking, then all segments are copied
  70. into the new index, which is a much more time consuming process. Also if using
  71. multiple data paths, shards on different data paths require a full copy of
  72. segment files if they are not on the same disk since hardlinks don’t work across
  73. disks)
  74. . Recovers the target index as though it were a closed index which
  75. had just been re-opened.
  76. [[shrink-index]]
  77. ===== Shrink an index
  78. To shrink `my_source_index` into a new index called `my_target_index`, issue
  79. the following request:
  80. [source,console]
  81. --------------------------------------------------
  82. POST /my_source_index/_shrink/my_target_index
  83. {
  84. "settings": {
  85. "index.routing.allocation.require._name": null, <1>
  86. "index.blocks.write": null <2>
  87. }
  88. }
  89. --------------------------------------------------
  90. // TEST[continued]
  91. <1> Clear the allocation requirement copied from the source index.
  92. <2> Clear the index write block copied from the source index.
  93. The above request returns immediately once the target index has been added to
  94. the cluster state -- it doesn't wait for the shrink operation to start.
  95. [IMPORTANT]
  96. =====================================
  97. Indices can only be shrunk if they satisfy the following requirements:
  98. * the target index must not exist
  99. * The index must have more primary shards than the target index.
  100. * The number of primary shards in the target index must be a factor of the
  101. number of primary shards in the source index. The source index must have
  102. more primary shards than the target index.
  103. * The index must not contain more than `2,147,483,519` documents in total
  104. across all shards that will be shrunk into a single shard on the target index
  105. as this is the maximum number of docs that can fit into a single shard.
  106. * The node handling the shrink process must have sufficient free disk space to
  107. accommodate a second copy of the existing index.
  108. =====================================
  109. The `_shrink` API is similar to the <<indices-create-index, `create index` API>>
  110. and accepts `settings` and `aliases` parameters for the target index:
  111. [source,console]
  112. --------------------------------------------------
  113. POST /my_source_index/_shrink/my_target_index
  114. {
  115. "settings": {
  116. "index.number_of_replicas": 1,
  117. "index.number_of_shards": 1, <1>
  118. "index.codec": "best_compression" <2>
  119. },
  120. "aliases": {
  121. "my_search_indices": {}
  122. }
  123. }
  124. --------------------------------------------------
  125. // TEST[s/^/PUT my_source_index\n{"settings": {"index.number_of_shards":5,"index.blocks.write": true}}\n/]
  126. <1> The number of shards in the target index. This must be a factor of the
  127. number of shards in the source index.
  128. <2> Best compression will only take affect when new writes are made to the
  129. index, such as when <<indices-forcemerge,force-merging>> the shard to a single
  130. segment.
  131. NOTE: Mappings may not be specified in the `_shrink` request.
  132. [[monitor-shrink]]
  133. ===== Monitor the shrink process
  134. The shrink process can be monitored with the <<cat-recovery,`_cat recovery`
  135. API>>, or the <<cluster-health, `cluster health` API>> can be used to wait
  136. until all primary shards have been allocated by setting the `wait_for_status`
  137. parameter to `yellow`.
  138. The `_shrink` API returns as soon as the target index has been added to the
  139. cluster state, before any shards have been allocated. At this point, all
  140. shards are in the state `unassigned`. If, for any reason, the target index
  141. can't be allocated on the shrink node, its primary shard will remain
  142. `unassigned` until it can be allocated on that node.
  143. Once the primary shard is allocated, it moves to state `initializing`, and the
  144. shrink process begins. When the shrink operation completes, the shard will
  145. become `active`. At that point, Elasticsearch will try to allocate any
  146. replicas and may decide to relocate the primary shard to another node.
  147. [[shrink-wait-active-shards]]
  148. ===== Wait for active shards
  149. Because the shrink operation creates a new index to shrink the shards to,
  150. the <<create-index-wait-for-active-shards,wait for active shards>> setting
  151. on index creation applies to the shrink index action as well.
  152. [[shrink-index-api-path-params]]
  153. ==== {api-path-parms-title}
  154. `<index>`::
  155. (Required, string)
  156. Name of the source index to shrink.
  157. include::{es-repo-dir}/rest-api/common-parms.asciidoc[tag=target-index]
  158. [[shrink-index-api-query-params]]
  159. ==== {api-query-parms-title}
  160. include::{es-repo-dir}/rest-api/common-parms.asciidoc[tag=wait_for_active_shards]
  161. include::{es-repo-dir}/rest-api/common-parms.asciidoc[tag=timeoutparms]
  162. [[shrink-index-api-request-body]]
  163. ==== {api-request-body-title}
  164. include::{es-repo-dir}/rest-api/common-parms.asciidoc[tag=target-index-aliases]
  165. include::{es-repo-dir}/rest-api/common-parms.asciidoc[tag=target-index-settings]