shrink-index.asciidoc 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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{"settings":{"index.number_of_shards":2}}\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. "settings": {
  54. "index.routing.allocation.require._name": null, <1>
  55. "index.blocks.write": null <2>
  56. }
  57. }
  58. --------------------------------------------------
  59. // CONSOLE
  60. // TEST[continued]
  61. <1> Clear the allocation requirement copied from the source index.
  62. <2> Clear the index write block copied from the source index.
  63. The above request returns immediately once the target index has been added to
  64. the cluster state -- it doesn't wait for the shrink operation to start.
  65. [IMPORTANT]
  66. =====================================
  67. Indices can only be shrunk if they satisfy the following requirements:
  68. * the target index must not exist
  69. * The index must have more primary shards than the target index.
  70. * The number of primary shards in the target index must be a factor of the
  71. number of primary shards in the source index. The source index must have
  72. more primary shards than the target index.
  73. * The index must not contain more than `2,147,483,519` documents in total
  74. across all shards that will be shrunk into a single shard on the target index
  75. as this is the maximum number of docs that can fit into a single shard.
  76. * The node handling the shrink process must have sufficient free disk space to
  77. accommodate a second copy of the existing index.
  78. =====================================
  79. The `_shrink` API is similar to the <<indices-create-index, `create index` API>>
  80. and accepts `settings` and `aliases` parameters for the target index:
  81. [source,js]
  82. --------------------------------------------------
  83. POST my_source_index/_shrink/my_target_index
  84. {
  85. "settings": {
  86. "index.number_of_replicas": 1,
  87. "index.number_of_shards": 1, <1>
  88. "index.codec": "best_compression" <2>
  89. },
  90. "aliases": {
  91. "my_search_indices": {}
  92. }
  93. }
  94. --------------------------------------------------
  95. // CONSOLE
  96. // TEST[s/^/PUT my_source_index\n{"settings": {"index.number_of_shards":5,"index.blocks.write": true}}\n/]
  97. <1> The number of shards in the target index. This must be a factor of the
  98. number of shards in the source index.
  99. <2> Best compression will only take affect when new writes are made to the
  100. index, such as when <<indices-forcemerge,force-merging>> the shard to a single
  101. segment.
  102. NOTE: Mappings may not be specified in the `_shrink` request.
  103. [float]
  104. === Monitoring the shrink process
  105. The shrink process can be monitored with the <<cat-recovery,`_cat recovery`
  106. API>>, or the <<cluster-health, `cluster health` API>> can be used to wait
  107. until all primary shards have been allocated by setting the `wait_for_status`
  108. parameter to `yellow`.
  109. The `_shrink` API returns as soon as the target index has been added to the
  110. cluster state, before any shards have been allocated. At this point, all
  111. shards are in the state `unassigned`. If, for any reason, the target index
  112. can't be allocated on the shrink node, its primary shard will remain
  113. `unassigned` until it can be allocated on that node.
  114. Once the primary shard is allocated, it moves to state `initializing`, and the
  115. shrink process begins. When the shrink operation completes, the shard will
  116. become `active`. At that point, Elasticsearch will try to allocate any
  117. replicas and may decide to relocate the primary shard to another node.
  118. [float]
  119. === Wait For Active Shards
  120. Because the shrink operation creates a new index to shrink the shards to,
  121. the <<create-index-wait-for-active-shards,wait for active shards>> setting
  122. on index creation applies to the shrink index action as well.