shrink-index.asciidoc 9.2 KB

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