shrink-index.asciidoc 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. [[indices-shrink-index]]
  2. == Shrink Index
  3. The shrink index API allows you to shrink an existing index into a new index
  4. with fewer primary shards. The requested number of primary shards in the target index
  5. must be a factor of the number of shards in the source index. For example an index with
  6. `8` primary shards can be shrunk into `4`, `2` or `1` primary shards or an index
  7. with `15` primary shards can be shrunk into `5`, `3` or `1`. If the number
  8. of shards in the index is a prime number it can only be shrunk into a single
  9. primary shard. Before shrinking, a (primary or replica) copy of every shard
  10. in the index must be present on the same node.
  11. Shrinking works as follows:
  12. * First, it creates a new target index with the same definition as the source
  13. index, but with a smaller number of primary shards.
  14. * Then it hard-links segments from the source index into the target index. (If
  15. the file system doesn't support hard-linking, then all segments are copied
  16. into the new index, which is a much more time consuming process.)
  17. * Finally, it recovers the target index as though it were a closed index which
  18. had just been re-opened.
  19. [float]
  20. === Preparing an index for shrinking
  21. In order to shrink an index, the index must be marked as read-only, and a
  22. (primary or replica) copy of every shard in the index must be relocated to the
  23. same node and have <<cluster-health,health>> `green`.
  24. These two conditions can be achieved with the following request:
  25. [source,js]
  26. --------------------------------------------------
  27. PUT /my_source_index/_settings
  28. {
  29. "settings": {
  30. "index.routing.allocation.require._name": "shrink_node_name", <1>
  31. "index.blocks.write": true <2>
  32. }
  33. }
  34. --------------------------------------------------
  35. // CONSOLE
  36. // TEST[s/^/PUT my_source_index\n/]
  37. <1> Forces the relocation of a copy of each shard to the node with name
  38. `shrink_node_name`. See <<shard-allocation-filtering>> for more options.
  39. <2> Prevents write operations to this index while still allowing metadata
  40. changes like deleting the index.
  41. It can take a while to relocate the source index. Progress can be tracked
  42. with the <<cat-recovery,`_cat recovery` API>>, or the <<cluster-health,
  43. `cluster health` API>> can be used to wait until all shards have relocated
  44. with the `wait_for_no_relocating_shards` parameter.
  45. [float]
  46. === Shrinking an index
  47. To shrink `my_source_index` into a new index called `my_target_index`, issue
  48. the following request:
  49. [source,js]
  50. --------------------------------------------------
  51. POST my_source_index/_shrink/my_target_index
  52. --------------------------------------------------
  53. // CONSOLE
  54. // TEST[continued]
  55. The above request returns immediately once the target index has been added to
  56. the cluster state -- it doesn't wait for the shrink operation to start.
  57. [IMPORTANT]
  58. =====================================
  59. Indices can only be shrunk if they satisfy the following requirements:
  60. * the target index must not exist
  61. * The index must have more primary shards than the target index.
  62. * The number of primary shards in the target index must be a factor of the
  63. number of primary shards in the source index. The source index must have
  64. more primary shards than the target index.
  65. * The index must not contain more than `2,147,483,519` documents in total
  66. across all shards that will be shrunk into a single shard on the target index
  67. as this is the maximum number of docs that can fit into a single shard.
  68. * The node handling the shrink process must have sufficient free disk space to
  69. accommodate a second copy of the existing index.
  70. =====================================
  71. The `_shrink` API is similar to the <<indices-create-index, `create index` API>>
  72. and accepts `settings` and `aliases` parameters for the target index:
  73. [source,js]
  74. --------------------------------------------------
  75. POST my_source_index/_shrink/my_target_index
  76. {
  77. "settings": {
  78. "index.number_of_replicas": 1,
  79. "index.number_of_shards": 1, <1>
  80. "index.codec": "best_compression" <2>
  81. },
  82. "aliases": {
  83. "my_search_indices": {}
  84. }
  85. }
  86. --------------------------------------------------
  87. // CONSOLE
  88. // TEST[s/^/PUT my_source_index\n{"settings": {"index.blocks.write": true}}\n/]
  89. <1> The number of shards in the target index. This must be a factor of the
  90. number of shards in the source index.
  91. <2> Best compression will only take affect when new writes are made to the
  92. index, such as when <<indices-forcemerge,force-merging>> the shard to a single
  93. segment.
  94. NOTE: Mappings may not be specified in the `_shrink` request, and all
  95. `index.analysis.*` and `index.similarity.*` settings will be overwritten with
  96. the settings from the source index.
  97. [float]
  98. === Monitoring the shrink process
  99. The shrink process can be monitored with the <<cat-recovery,`_cat recovery`
  100. API>>, or the <<cluster-health, `cluster health` API>> can be used to wait
  101. until all primary shards have been allocated by setting the `wait_for_status`
  102. parameter to `yellow`.
  103. The `_shrink` API returns as soon as the target index has been added to the
  104. cluster state, before any shards have been allocated. At this point, all
  105. shards are in the state `unassigned`. If, for any reason, the target index
  106. can't be allocated on the shrink node, its primary shard will remain
  107. `unassigned` until it can be allocated on that node.
  108. Once the primary shard is allocated, it moves to state `initializing`, and the
  109. shrink process begins. When the shrink operation completes, the shard will
  110. become `active`. At that point, Elasticsearch will try to allocate any
  111. replicas and may decide to relocate the primary shard to another node.
  112. [float]
  113. === Wait For Active Shards
  114. Because the shrink operation creates a new index to shrink the shards to,
  115. the <<create-index-wait-for-active-shards,wait for active shards>> setting
  116. on index creation applies to the shrink index action as well.