一、同节点
from elasticsearch import Elasticsearch
# 连接到源Elasticsearch集群
source_es = Elasticsearch('http://127.0.0.1:9201')
# 连接到目标Elasticsearch集群
target_es = Elasticsearch('http://127.0.0.1:9200')
# 定义源索引和目标索引
source_index = 't1'
target_index = 't2'
# 使用_reindex API从源索引导入数据到目标索引
reindex_body = {
"source": {
"index": source_index
},
"dest": {
"index":target_index
}
}
response = target_es.reindex(body=reindex_body)
# 打印结果
print(response)
二、跨节点
from elasticsearch import Elasticsearch
from elasticsearch.helpers import scan, bulk
source_index = 't1'
target_index = 't1'
# 连接到源Elasticsearch集群
source_client = Elasticsearch('http://127.0.0.1:9201')
# 连接到目标Elasticsearch集群
target_client = Elasticsearch('http://127.0.0.1:9200')
def scan_index(client,index):
scan_results = scan(client, query={"query": {"match_all": {}}}, index=index)
for result in scan_results:
yield result
# 执行批量索引操作
def bulk_index_documents(client,index,documents):
actions = []
for document in documents:
action = {
"_op_type": "index",
"_index": target_index,
"_id": document["_id"],
"_source": document["_source"]
}
actions.append(action)
bulk(client, actions)
def main():
documents = scan_index(source_client,source_index)
# 批量索引到目标索引
bulk_index_documents(target_client,target_index,documents)
print('ok')
main()
本文含有隐藏内容,请 开通VIP 后查看