Elasticsearch索引数据迁移
Elasticsearch索引数据迁移
背景
业务发现Elasticsearch的某一个索引数据未迁移,需要迁移最新数据到业务系统中。需要由旧的Elasticsearch,迁移到新的Elasticsearch。Elasticsearch版本为6.5.4
Reindex API
Reindex API是Elasticsearch提供的用于将一个或多个索引的数据复制到另一个索引的官方工具。它是6.x版本中最推荐的数据迁移方式。
reindex的核心特性:
- 零依赖:无需额外工具或插件
- 跨集群支持:支持本地和远程集群建的数据迁移
- 灵活过滤:可选择性迁移特定文档
- 数据转换:支持脚本对文档进行修改
- 任务管理:支持异步执行和进度监控
迁移过程
1、清空新的Elasticsearch索引数据
curl -X POST "http://10.8.6.252:9200/202408081153/_delete_by_query" -H 'Content-Type: application/json' -d \
'{
"query": {
"match_all": {}
}
}'
2、新的Elasticsearch新增配置并重启
# 编辑目标集群的 elasticsearch.yml
reindex.remote.whitelist: "源集群IP:9200"
# 重启目标集群节点
systemctl restart elasticsearch
修改后配置如下图所示:

3、建立reindex任务
# 3.建立reindex任务
curl -X POST "http://10.8.6.252:9200/_reindex?wait_for_completion=false" -H 'Content-Type: application/json' -d \
'{
"source": {
"remote": {
"host": "http://10.7.6.127:9200"
},
"index": "202408081153",
"size": 1000, # 每批迁移的数据量
"query": {
"match_all": {} # 迁移全部数据,可以加过滤条件
}
},
"dest": {
"index": "202408081153", # 可以与源索引同名,也可以不同名
"op_type": "create" # 避免文档冲突
},
"conflicts": "proceed" # 冲突时继续
}'
4、查看异步任务执行情况
获取上一步的task任务id
curl -X GET "http://10.8.6.252:9200/_tasks/PXxqiF7sSWuyeCxbxvjjzA:2624"
5、验证数据一致性
验证文档数据是否一致,这里采取验证索引文档量的方式
curl -s "$SOURCE_ES/索引名/_count" | jq .count
curl -s "$TARGET_ES/索引名/_count" | jq .count
curl -s "10.8.6.252:9200/202408081153/_count" | jq .count
-s 静默模式,只输出内容实际相应内容,没有进度信息
jq命令, Linux 中一个强大的命令行 JSON 处理器,用于解析、过滤、转换和格式化 JSON 数据。

浙公网安备 33010602011771号