💎一站式轻松地调用各大LLM模型接口,支持GPT4、智谱、星火、月之暗面及文生图 广告
# Shrink Index /缩小索引 注意:涉及翻译内容: Index-索引;types-类型;mapping-映射;aliases-别名;shards-分片;replicas-副本 缩小索引API可以允许你将存在的索引转变为一个只包含主要分片的新索引。目标索引中请求的主要分片数量必须要为原索引中的因子(即原分片数量是新分片倍数),例如8个分片可以缩小到4、2、1个分片。如果原分片数量为素数则只能缩小到一个分片。在缩小开始时,每个分片的复制都必须在同一节点(node)存在。 缩小工作如下: * 首先,以相同配置创建目标索引,但是主分片数量减少。 * 然后硬链接( hard-linking ) 各部分自原索引到目标索引。(如果系统不支持硬链接,那么索引的所有部分都将复制迁移到新索引,将会花费大量时间) * 最终,将会恢复目标索引,因为目标索引刚被重新打开就会被关闭。 * Finally, it recovers the target index as though it were a closed index which had just been re-opened. # Preparing an index for shrinking /准备缩小索引 为了缩小索引,索引必须被标记为只读,所有分片都会复制到一个相同的节点并且节点健康为绿色。 这两个条件可以通过下列请求实现: | `curl -XPUT ``'localhost:9200/my_source_index/_settings?pretty'` `-d'` `{` `"settings"``: {` `"index.routing.allocation.require._name"``: ``"shrink_node_name"``,` `"index.blocks.write"``: ``true` `}` `}'` | | [![](https://www.elastic.co/guide/en/elasticsearch/reference/current/images/icons/callouts/1.png)](https://www.elastic.co/guide/en/elasticsearch/reference/current/indices-shrink-index.html#CO120-1) | 强制所有分片都复制迁移到名字为 `shrink_node_name 的节点上,通过` [Shard Allocation Filtering](https://www.elastic.co/guide/en/elasticsearch/reference/current/shard-allocation-filtering.html "Shard Allocation Filtering") 获取更多设置信息。 | | [![](https://www.elastic.co/guide/en/elasticsearch/reference/current/images/icons/callouts/2.png)](https://www.elastic.co/guide/en/elasticsearch/reference/current/indices-shrink-index.html#CO120-2) | 避免索引的所有写操作,但是依然可以修改索引的基本信息,例如删除索引。 | 从原索引迁移将会花费一定时间。进度信息可以在所有分片都迁移完成前通过 [`_cat recovery`API](https://www.elastic.co/guide/en/elasticsearch/reference/current/cat-recovery.html "cat recovery")  或者  [`cluster health` API](https://www.elastic.co/guide/en/elasticsearch/reference/current/cluster-health.html "Cluster Health") 使用 `wait_for_no_relocating_shards` 参数获得。 # Shrinking an index /缩小索引 缩小索引 `my_source_index` 到 新的索引 my_target_index ,可以用下列请求: | `curl -XPOST ``'localhost:9200/my_source_index/_shrink/my_target_index?pretty'` | 上述请求将会在目标索引信息加入到集群时立即返回——他不会等到缩小操作开始。   ![Important](https://www.elastic.co/guide/en/elasticsearch/reference/current/images/icons/important.png)缩小索引需要满足下列要求: * 目标索引存在 * 原索引主分片数量比目标索引多 * 原索引主分片数量是目标索引倍数 * 索引中的所有文档在目标索引将会被缩小到一个分片的数量不会超过 `2,147,483,519` ,因为这是一个分片的承受的最大文档数量。 * 执行缩小进程的节点必须要有足够的空闲磁盘空间满足原索引的分片能够全部复制迁徙到该节点。 缩小索引API和创建索引 [`(create index)` API](https://www.elastic.co/guide/en/elasticsearch/reference/current/indices-create-index.html "Create Index") 相似,并且对于目标索引接受 `settings` 和 `aliases 参数` | `curl -XPOST ``'localhost:9200/my_source_index/_shrink/my_target_index?pretty'` `-d'` `{` `"settings"``: {` `"index.number_of_replicas"``: ``1``,` `"index.number_of_shards"``: ``1``,` `"index.codec"``: ``"best_compression"` `},` `"aliases"``: {` `"my_search_indices"``: {}` `}` `}'` | | [![](https://www.elastic.co/guide/en/elasticsearch/reference/current/images/icons/callouts/1.png)](https://www.elastic.co/guide/en/elasticsearch/reference/current/indices-shrink-index.html#CO121-1) | 注意目标分片数量为原分片数量的因子 | | [![](https://www.elastic.co/guide/en/elasticsearch/reference/current/images/icons/callouts/2.png)](https://www.elastic.co/guide/en/elasticsearch/reference/current/indices-shrink-index.html#CO121-2) |  best_compression 最佳压缩时只会影响新写到索引的数据,例如当 force-merging 分片。 |   映射(Mappings) 不应该在缩小API中制定,因为所有的 `index.analysis.* `和 `index.similarity.*` 设置都会被原索引配置覆盖。 # Monitoring the shrink process /监测缩小进程 The shrink process can be monitored with the [`_cat recovery` API](https://www.elastic.co/guide/en/elasticsearch/reference/current/cat-recovery.html "cat recovery"), or the [`cluster health` API](https://www.elastic.co/guide/en/elasticsearch/reference/current/cluster-health.html "Cluster Health") can be used to wait until all primary shards have been allocated by setting the `wait_for_status`parameter to `yellow`. 缩小进度可以被 [`_cat recovery` API](https://www.elastic.co/guide/en/elasticsearch/reference/current/cat-recovery.html "cat recovery") 或者 [`cluster health` API](https://www.elastic.co/guide/en/elasticsearch/reference/current/cluster-health.html "Cluster Health") 所监控。通过设置 wait_for_status 为 yellow ,可以等到所有的主分片都已经分配。 缩小API会在目标索引被创建时马上返回,这发生在所有分片被分配之前。这点说明这时所有分片都处于 `unassigned 状态,如果出于任何原因目标索引分片无法分配到执行缩小的节点上,那么主分片状态会一直停留在 `unassigned ,直到分片被分配。`` 一旦主节点分配成功,会转化状态会 initializing ,缩小进程就开始执行。当缩小操作结束的时候,这些分片会被激活。在这之后,Elasticsearch将试图分配副本,甚至可能决定主分片迁至另一个节点。 # Wait For Active Shards /等待激活分片 因为缩小操作会创建新的索引,所以 [wait for active shards](https://www.elastic.co/guide/en/elasticsearch/reference/current/indices-create-index.html#create-index-wait-for-active-shards "Wait For Active Shardsedit") 设置依然有效。