shrink-index.asciidoc 7.5 KB

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