要增加现有索引的主分片数量,直接修改是不可能的。因此,如果你想增加主分片的数量,必须重新创建索引。通常有两种方法:_reindex API 和 _split API。
在这两种方法中,_split API 通常比 _reindex API 更快。在操作之前,必须停止索引操作,否则源索引和目标索引的文档数量会不一致。

通过 split API 可以复制现有索引的设置和映射,创建一个具有所需主分片数量的新索引。在实施 split API 之前,需要检查以下设置:
注意:如果仅需更改主分片数量,split API 是首选,因为它比 Reindex API 快得多。
创建一个测试索引:
POST test_split_source/_doc
{
"test": "test"
}将源索引设置为只读:
PUT test_split_source/_settings
{
"index.blocks.write": true
}设置和映射将自动从源索引复制:
POST /test_split_source/_split/test_split_target
{
"settings": {
"index.number_of_shards": 3
}
}你可以通过以下命令检查进度:
GET _cat/recovery/test_split_target?v&h=index,shard,time,stage,files_percent,files_total由于设置和映射是从源索引复制的,目标索引是只读的。让我们启用目标索引的写操作:
PUT test_split_target/_settings
{
"index.blocks.write": null
}在删除原始索引之前,检查源和目标索引的文档数量:
GET _cat/indices/test_split*?v&h=index,pri,rep,docs.count索引名和别名不能相同。你需要删除源索引,并将源索引名作为别名添加到目标索引:
DELETE test_split_source
PUT /test_split_target/_alias/test_split_source添加test_split_source别名到test_split_target索引后,你应该测试它:
GET test_split_source
POST test_split_source/_doc
{
"test": "test"
}通过 reindex API 创建一个新索引时,可以指定任意数量的主分片。在创建具有预期主分片数量的新索引后,可以将源索引中的所有数据重新索引到这个新索引中。
除了 split API 的功能外,reindex API 允许使用 ingest_pipeline 操作数据。在 reindex API 中,通过 ingest 管道,只有符合过滤条件的指定字段会被索引到目标索引。数据内容可以通过 painless 脚本进行更改,并且多个索引可以合并为一个索引。
创建一个测试索引:
POST test_reindex_source/_doc
{
"test": "test"
}复制源索引的设置和映射:
GET test_reindex_source创建一个具有设置、映射和所需分片数量的目标索引:
PUT test_reindex_target
{
"mappings": {},
"settings": {
"number_of_shards": 10,
"number_of_replicas": 0,
"refresh_interval": -1
}
}注意:设置 number_of_replicas: 0 和 refresh_interval: -1 将提高重新索引速度。
开始重新索引过程。设置 requests_per_second=-1 和 slices=auto 可以调整重新索引速度。
POST _reindex?requests_per_second=-1&slices=auto&wait_for_completion=false
{
"source": {
"index": "test_reindex_source"
},
"dest": {
"index": "test_reindex_target"
}
}运行 reindex API 时,你会看到 task_id。复制该 ID 并通过 _tasks API 检查:
GET _tasks/<task_id>在重新索引完成后更新设置:
PUT test_reindex_target/_settings
{
"number_of_replicas": 1,
"refresh_interval": "1s"
}在删除原始索引之前,检查源和目标索引的文档数量,它们应该相同:
GET _cat/indices/test_reindex_*?v&h=index,pri,rep,docs.count索引名和别名不能相同。删除源索引,并将源索引名作为别名添加到目标索引:
DELETE test_reindex_source
PUT /test_reindex_target/_alias/test_reindex_source在将 test_reindex_source 别名添加到 test_reindex_target 索引后,使用以下命令测试:
GET test_reindex_source如果你想增加现有索引的主分片数量,必须将设置和映射重新创建到新索引中。有两种主要方法可以实现这一点:reindex API 和 split API。在使用任何一种方法之前,必须停止活动索引。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。