split-index.asciidoc 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  1. [[indices-split-index]]
  2. === Split index API
  3. ++++
  4. <titleabbrev>Split index</titleabbrev>
  5. ++++
  6. Splits an existing index into a new index with more primary shards.
  7. [source,console]
  8. ----
  9. POST /my-index-000001/_split/split-my-index-000001
  10. {
  11. "settings": {
  12. "index.number_of_shards": 2
  13. }
  14. }
  15. ----
  16. // TEST[s/^/PUT my-index-000001\n{"settings":{"blocks.write":true}}\n/]
  17. [[split-index-api-request]]
  18. ==== {api-request-title}
  19. `POST /<index>/_split/<target-index>`
  20. `PUT /<index>/_split/<target-index>`
  21. [[split-index-api-prereqs]]
  22. ==== {api-prereq-title}
  23. * If the {es} {security-features} are enabled, you must have the `manage`
  24. <<privileges-list-indices,index privilege>> for the index.
  25. * Before you can split an index:
  26. ** The index must be read-only.
  27. ** The <<cluster-health, cluster health>> status must be green.
  28. You can do make an index read-only
  29. with the following request:
  30. [source,console]
  31. --------------------------------------------------
  32. PUT /my_source_index/_settings
  33. {
  34. "settings": {
  35. "index.blocks.write": true <1>
  36. }
  37. }
  38. --------------------------------------------------
  39. // TEST[s/^/PUT my_source_index\n/]
  40. <1> Prevents write operations to this index while still allowing metadata
  41. changes like deleting the index.
  42. The current write index on a data stream cannot be split. In order to split
  43. the current write index, the data stream must first be
  44. <<rollover-data-stream-ex,rolled over>> so that a new write index is created
  45. and then the previous write index can be split.
  46. [[split-index-api-desc]]
  47. ==== {api-description-title}
  48. The split index API allows you to split an existing index into a new index,
  49. where each original primary shard is split into two or more primary shards in
  50. the new index.
  51. The number of times the index can be split (and the number of shards that each
  52. original shard can be split into) is determined by the
  53. `index.number_of_routing_shards` setting. The number of routing shards
  54. specifies the hashing space that is used internally to distribute documents
  55. across shards with consistent hashing. For instance, a 5 shard index with
  56. `number_of_routing_shards` set to `30` (`5 x 2 x 3`) could be split by a
  57. factor of `2` or `3`. In other words, it could be split as follows:
  58. * `5` -> `10` -> `30` (split by 2, then by 3)
  59. * `5` -> `15` -> `30` (split by 3, then by 2)
  60. * `5` -> `30` (split by 6)
  61. `index.number_of_routing_shards` is a <<index-modules-settings,static index
  62. setting>>. You can only set `index.number_of_routing_shards` at index creation
  63. time or on a <<indices-open-close,closed index>>.
  64. .*Index creation example*
  65. [%collapsible]
  66. ====
  67. The following <<indices-create-index,create index API>> creates the
  68. `my-index-000001` index with an `index.number_of_routing_shards` setting of `30`.
  69. [source,console]
  70. ----
  71. PUT /my-index-000001
  72. {
  73. "settings": {
  74. "index": {
  75. "number_of_routing_shards": 30
  76. }
  77. }
  78. }
  79. ----
  80. // TEST[continued]
  81. ====
  82. The `index.number_of_routing_shards` setting's default value depends
  83. on the number of primary shards in the original index.
  84. The default is designed to allow you to split
  85. by factors of 2 up to a maximum of 1024 shards. However, the original number
  86. of primary shards must taken into account. For instance, an index created
  87. with 5 primary shards could be split into 10, 20, 40, 80, 160, 320, or a
  88. maximum of 640 shards (with a single split action or multiple split actions).
  89. If the original index contains one primary shard (or a multi-shard index has
  90. been <<indices-shrink-index,shrunk>> down to a single primary shard), then the
  91. index may by split into an arbitrary number of shards greater than 1. The
  92. properties of the default number of routing shards will then apply to the
  93. newly split index.
  94. [[how-split-works]]
  95. ===== How splitting works
  96. A split operation:
  97. . Creates a new target index with the same definition as the source
  98. index, but with a larger number of primary shards.
  99. . Hard-links segments from the source index into the target index. (If
  100. the file system doesn't support hard-linking, then all segments are copied
  101. into the new index, which is a much more time consuming process.)
  102. . Hashes all documents again, after low level files are created, to delete
  103. documents that belong to a different shard.
  104. . Recovers the target index as though it were a closed index which
  105. had just been re-opened.
  106. [[incremental-resharding]]
  107. ===== Why doesn't Elasticsearch support incremental resharding?
  108. Going from `N` shards to `N+1` shards, aka. incremental resharding, is indeed a
  109. feature that is supported by many key-value stores. Adding a new shard and
  110. pushing new data to this new shard only is not an option: this would likely be
  111. an indexing bottleneck, and figuring out which shard a document belongs to
  112. given its `_id`, which is necessary for get, delete and update requests, would
  113. become quite complex. This means that we need to rebalance existing data using
  114. a different hashing scheme.
  115. The most common way that key-value stores do this efficiently is by using
  116. consistent hashing. Consistent hashing only requires `1/N`-th of the keys to
  117. be relocated when growing the number of shards from `N` to `N+1`. However
  118. Elasticsearch's unit of storage, shards, are Lucene indices. Because of their
  119. search-oriented data structure, taking a significant portion of a Lucene index,
  120. be it only 5% of documents, deleting them and indexing them on another shard
  121. typically comes with a much higher cost than with a key-value store. This cost
  122. is kept reasonable when growing the number of shards by a multiplicative factor
  123. as described in the above section: this allows Elasticsearch to perform the
  124. split locally, which in-turn allows to perform the split at the index level
  125. rather than reindexing documents that need to move, as well as using hard links
  126. for efficient file copying.
  127. In the case of append-only data, it is possible to get more flexibility by
  128. creating a new index and pushing new data to it, while adding an alias that
  129. covers both the old and the new index for read operations. Assuming that the
  130. old and new indices have respectively +M+ and +N+ shards, this has no overhead
  131. compared to searching an index that would have +M+N+ shards.
  132. [[split-index]]
  133. ===== Split an index
  134. To split `my_source_index` into a new index called `my_target_index`, issue
  135. the following request:
  136. [source,console]
  137. --------------------------------------------------
  138. POST /my_source_index/_split/my_target_index
  139. {
  140. "settings": {
  141. "index.number_of_shards": 2
  142. }
  143. }
  144. --------------------------------------------------
  145. // TEST[continued]
  146. The above request returns immediately once the target index has been added to
  147. the cluster state -- it doesn't wait for the split operation to start.
  148. [IMPORTANT]
  149. =====================================
  150. Indices can only be split if they satisfy the following requirements:
  151. * the target index must not exist
  152. * The source index must have fewer primary shards than the target index.
  153. * The number of primary shards in the target index must be a multiple of the
  154. number of primary shards in the source index.
  155. * The node handling the split process must have sufficient free disk space to
  156. accommodate a second copy of the existing index.
  157. =====================================
  158. The `_split` API is similar to the <<indices-create-index, `create index` API>>
  159. and accepts `settings` and `aliases` parameters for the target index:
  160. [source,console]
  161. --------------------------------------------------
  162. POST /my_source_index/_split/my_target_index
  163. {
  164. "settings": {
  165. "index.number_of_shards": 5 <1>
  166. },
  167. "aliases": {
  168. "my_search_indices": {}
  169. }
  170. }
  171. --------------------------------------------------
  172. // TEST[s/^/PUT my_source_index\n{"settings": {"index.blocks.write": true, "index.number_of_shards": "1"}}\n/]
  173. <1> The number of shards in the target index. This must be a multiple of the
  174. number of shards in the source index.
  175. NOTE: Mappings may not be specified in the `_split` request.
  176. [[monitor-split]]
  177. ===== Monitor the split process
  178. The split process can be monitored with the <<cat-recovery,`_cat recovery`
  179. API>>, or the <<cluster-health, `cluster health` API>> can be used to wait
  180. until all primary shards have been allocated by setting the `wait_for_status`
  181. parameter to `yellow`.
  182. The `_split` API returns as soon as the target index has been added to the
  183. cluster state, before any shards have been allocated. At this point, all
  184. shards are in the state `unassigned`. If, for any reason, the target index
  185. can't be allocated, its primary shard will remain `unassigned` until it
  186. can be allocated on that node.
  187. Once the primary shard is allocated, it moves to state `initializing`, and the
  188. split process begins. When the split operation completes, the shard will
  189. become `active`. At that point, Elasticsearch will try to allocate any
  190. replicas and may decide to relocate the primary shard to another node.
  191. [[split-wait-active-shards]]
  192. ===== Wait for active shards
  193. Because the split operation creates a new index to split the shards to,
  194. the <<create-index-wait-for-active-shards,wait for active shards>> setting
  195. on index creation applies to the split index action as well.
  196. [[split-index-api-path-params]]
  197. ==== {api-path-parms-title}
  198. `<index>`::
  199. (Required, string)
  200. Name of the source index to split.
  201. include::{es-repo-dir}/rest-api/common-parms.asciidoc[tag=target-index]
  202. [[split-index-api-query-params]]
  203. ==== {api-query-parms-title}
  204. include::{es-repo-dir}/rest-api/common-parms.asciidoc[tag=wait_for_active_shards]
  205. include::{es-repo-dir}/rest-api/common-parms.asciidoc[tag=timeoutparms]
  206. [[split-index-api-request-body]]
  207. ==== {api-request-body-title}
  208. include::{es-repo-dir}/rest-api/common-parms.asciidoc[tag=target-index-aliases]
  209. include::{es-repo-dir}/rest-api/common-parms.asciidoc[tag=target-index-settings]