shrink-index.asciidoc 6.1 KB

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