一、同节点
from elasticsearch import Elasticsearch
source_es = Elasticsearch('http://127.0.0.1:9201')
target_es = Elasticsearch('http://127.0.0.1:9200')
source_index = 't1'
target_index = 't2'
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'
source_client = Elasticsearch('http://127.0.0.1:9201')
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()