from elasticsearch import Elasticsearch
from elasticsearch.helpers import scan, bulk
# 源 ES 连接信息
source_host = 'localhost'
source_port = 9200
source_index = 'source_index'
source_type = 'source_type'
# 目标 ES 连接信息
target_host = 'localhost'
target_port = 9200
target_index = 'target_index'
target_type = 'target_type'
# 创建源 ES 连接
source_es = Elasticsearch([{'host': source_host, 'port': source_port}])
# 创建目标 ES 连接
target_es = Elasticsearch([{'host': target_host, 'port': target_port}])
# 使用 Scroll API 批量检索源索引中的数据
scroll = scan(source_es, index=source_index, doc_type=source_type, query={"query": {"match_all": {}}})
# 批量迁移数据到目标索引
bulk_data = []
for doc in scroll:
bulk_data.append({
"_index": target_index,
"_type": target_type,
"_source": doc['_source']
})
# 每 1000 条数据执行一次批量插入操作
if len(bulk_data) >= 1000:
bulk(target_es, bulk_data)
bulk_data = []
# 处理剩余的数据
if len(bulk_data) > 0:
bulk(target_es, bulk_data)
print("数据迁移完成")