elasticsearch reindex 索引。
背景:
索引test1 reindex到test2 修改sharding数量
程序是通过别名test1_alias访问索引
1、创建目标索引test2
索引需要手动提前创建自动创建可能会有mapping 不一致性的风险。
The destination should be configured as wanted before calling _reindex. Reindex does not copy the settings from the source or its associated template.
Mappings, shard counts, replicas, and so on must be configured ahead of time.
复制test1的创建索引语句,修改sharding数量,
GET test1
查看索引
去掉下面test1属性的部分。
"test1" : {
"aliases" : {
"test1_alias" : { }
},
"provided_name" : "test1",
"creation_date" : "1742388051938",
, #注意前面的","号也要去掉,因为处于底部
"uuid" : "LG30E6iGSPu-09Ca5nGbPQ",
"version" : {
"created" : "6070099"
}
PUT test2
创建索引
修改后的json文件
“number_of_shards” : 修改到目标数量
执行命令返回的数据
{
"acknowledged" : true,
"shards_acknowledged" : true,
"index" : "test2"
}
2、禁止源索引test1 写
防止reindex期间写入数据,无法同步到目标索引
PUT /test1/_settings
{
"index.blocks.write": true
}
对应放开写的命令
PUT /test1/_settings
{
"index.blocks.write": false
}
3、 执行reindex 命令
POST _reindex?wait_for_completion=false&requests_per_second=1000
{
"source": {
"index": "test1"
},
"dest": {
"index": "test2"
}
}
返回信息
{
"task" : "KsO_WIvXRC2ccMCCxbuF3w:28779984"
}
wait_for_completion=false 后台执行命令,requests_per_second=500限制速度500
调整同步速度命令:
POST _reindex/KsO_WIvXRC2ccMCCxbuF3w:28779984/_rethrottle?requests_per_second=500
取消任务命令
POST _tasks/KsO_WIvXRC2ccMCCxbuF3w:28779984/_cancel
查看reindex任务
GET _tasks?actions=*reindex&detailed
4、 查看任务是否完成
GET _tasks/KsO_WIvXRC2ccMCCxbuF3w:28779984
返回 true的时候就是完成了reindx
“completed” : true
5、修改别名指向新的索引别名
test1_alias指向test2
POST _aliases
{
"actions": [
{ "remove": { "index": "test1", "alias": "test1_alias" } },
{ "add": { "index": "test2", "alias": "test1_alias" } }
]
}
6、查看别名
GET test2
删除原来的索引(可以保留归档)
DELETE test1
加速执行速度的方法
1、增加size 默认1000
POST _reindex?wait_for_completion=false
{
"source": {
"index": "test1",
"size": 5000
},
"dest": {
"index": "test2"
}
}
2、副本改成0
PUT /test2/_settings
{
"number_of_replicas": 0
}
3、禁止自动刷新
PUT /test2/_settings
{ "refresh_interval": -1 }
4、提高scroll的并行度
POST _reindex?slices=5&refresh
参考文档:
https://www.elastic.co/guide/en/elasticsearch/reference/current/docs-reindex.html
https://blog.csdn.net/wwd0501/article/details/132192692