shrink-index.asciidoc 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  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. [[how-shrink-works]]
  53. ===== How shrinking works
  54. A shrink operation:
  55. . Creates a new target index with the same definition as the source
  56. index, but with a smaller number of primary shards.
  57. . Hard-links segments from the source index into the target index. (If
  58. the file system doesn't support hard-linking, then all segments are copied
  59. into the new index, which is a much more time consuming process. Also if using
  60. multiple data paths, shards on different data paths require a full copy of
  61. segment files if they are not on the same disk since hardlinks don’t work across
  62. disks)
  63. . Recovers the target index as though it were a closed index which
  64. had just been re-opened.
  65. [[shrink-index]]
  66. ===== Shrink an index
  67. To shrink `my_source_index` into a new index called `my_target_index`, issue
  68. the following request:
  69. [source,console]
  70. --------------------------------------------------
  71. POST /my_source_index/_shrink/my_target_index
  72. {
  73. "settings": {
  74. "index.routing.allocation.require._name": null, <1>
  75. "index.blocks.write": null <2>
  76. }
  77. }
  78. --------------------------------------------------
  79. // TEST[continued]
  80. <1> Clear the allocation requirement copied from the source index.
  81. <2> Clear the index write block copied from the source index.
  82. The above request returns immediately once the target index has been added to
  83. the cluster state -- it doesn't wait for the shrink operation to start.
  84. [IMPORTANT]
  85. =====================================
  86. Indices can only be shrunk if they satisfy the following requirements:
  87. * the target index must not exist
  88. * The index must have more primary shards than the target index.
  89. * The number of primary shards in the target index must be a factor of the
  90. number of primary shards in the source index. The source index must have
  91. more primary shards than the target index.
  92. * The index must not contain more than `2,147,483,519` documents in total
  93. across all shards that will be shrunk into a single shard on the target index
  94. as this is the maximum number of docs that can fit into a single shard.
  95. * The node handling the shrink process must have sufficient free disk space to
  96. accommodate a second copy of the existing index.
  97. =====================================
  98. The `_shrink` API is similar to the <<indices-create-index, `create index` API>>
  99. and accepts `settings` and `aliases` parameters for the target index:
  100. [source,console]
  101. --------------------------------------------------
  102. POST /my_source_index/_shrink/my_target_index
  103. {
  104. "settings": {
  105. "index.number_of_replicas": 1,
  106. "index.number_of_shards": 1, <1>
  107. "index.codec": "best_compression" <2>
  108. },
  109. "aliases": {
  110. "my_search_indices": {}
  111. }
  112. }
  113. --------------------------------------------------
  114. // TEST[s/^/PUT my_source_index\n{"settings": {"index.number_of_shards":5,"index.blocks.write": true}}\n/]
  115. <1> The number of shards in the target index. This must be a factor of the
  116. number of shards in the source index.
  117. <2> Best compression will only take affect when new writes are made to the
  118. index, such as when <<indices-forcemerge,force-merging>> the shard to a single
  119. segment.
  120. NOTE: Mappings may not be specified in the `_shrink` request.
  121. [[monitor-shrink]]
  122. ===== Monitor the shrink process
  123. The shrink process can be monitored with the <<cat-recovery,`_cat recovery`
  124. API>>, or the <<cluster-health, `cluster health` API>> can be used to wait
  125. until all primary shards have been allocated by setting the `wait_for_status`
  126. parameter to `yellow`.
  127. The `_shrink` API returns as soon as the target index has been added to the
  128. cluster state, before any shards have been allocated. At this point, all
  129. shards are in the state `unassigned`. If, for any reason, the target index
  130. can't be allocated on the shrink node, its primary shard will remain
  131. `unassigned` until it can be allocated on that node.
  132. Once the primary shard is allocated, it moves to state `initializing`, and the
  133. shrink process begins. When the shrink operation completes, the shard will
  134. become `active`. At that point, Elasticsearch will try to allocate any
  135. replicas and may decide to relocate the primary shard to another node.
  136. [[shrink-wait-active-shards]]
  137. ===== Wait for active shards
  138. Because the shrink operation creates a new index to shrink the shards to,
  139. the <<create-index-wait-for-active-shards,wait for active shards>> setting
  140. on index creation applies to the shrink index action as well.
  141. [[shrink-index-api-path-params]]
  142. ==== {api-path-parms-title}
  143. `<index>`::
  144. (Required, string)
  145. Name of the source index to shrink.
  146. include::{es-repo-dir}/rest-api/common-parms.asciidoc[tag=target-index]
  147. [[shrink-index-api-query-params]]
  148. ==== {api-query-parms-title}
  149. include::{es-repo-dir}/rest-api/common-parms.asciidoc[tag=wait_for_active_shards]
  150. include::{es-repo-dir}/rest-api/common-parms.asciidoc[tag=timeoutparms]
  151. [[shrink-index-api-request-body]]
  152. ==== {api-request-body-title}
  153. include::{es-repo-dir}/rest-api/common-parms.asciidoc[tag=target-index-aliases]
  154. include::{es-repo-dir}/rest-api/common-parms.asciidoc[tag=target-index-settings]