收缩分片过多的索引(shrink)
Elasticsearch索引生命周期命令
# 1) Block writes on the source and gather all shard copies on one node:
PUT log-2026.09/_settings
{
"index.number_of_replicas": 0,
"index.routing.allocation.require._name": "node-1",
"index.blocks.write": true
}
GET _cluster/health/log-2026.09?wait_for_no_relocating_shards=true&timeout=30m
# 2) Create a new index with fewer shards (the target must be a factor of the source):
POST log-2026.09/_shrink/log-2026.09-shrunk
{
"settings": {
"index.number_of_shards": 1,
"index.number_of_replicas": 1,
"index.routing.allocation.require._name": null,
"index.blocks.write": null,
"index.codec": "best_compression"
}
}作用
通过将段硬链接到新索引,减少已不再写入的索引的主分片数;降低 heap 和分片数开销。
适用场景
每日索引创建时分片数多于实际需要、接近分片数上限,或在将旧索引移到 warm 层之前。
注意
shrink 期间源索引禁止写入;仅对不再写入的索引执行。目标分片数必须是源分片数的因数(8 -> 4、2 或 1)。汇集所有副本的节点需要与源索引同等大小的空闲磁盘。replicas 为 0 时源分片只有一个副本;在操作完成且新索引变为 green 之前,不要删除源索引。