# -*- coding: UTF-8 -*-
from elasticsearch import Elasticsearch
es_old = Elasticsearch([{'host': '127.0.0.1', 'port': 9200}])
es_new = Elasticsearch([{'host': '127.0.0.1', 'port': 9201}])
indices_dict = es_old.indices.get('*')
sync_dict = {"source": {"remote": {"host": "http://127.0.0.1:9200"}, "index": ""},
"dest": {"index": ""}}
for index_name, index_content in indices_dict.items():
if index_name != '.tasks':
del index_content["settings"]["index"]["creation_date"]
del index_content["settings"]["index"]["uuid"]
del index_content["settings"]["index"]["version"]
del index_content["settings"]["index"]["provided_name"]
print(index_name)
if (index_content["mappings"]) == {}:
pass
else:
index_content["mappings"] = index_content["mappings"][(list(index_content["mappings"])[0])]
# es_new.indices.create(index=index_name, body=index_content, include_type_name=True, ignore=[400, 404])
es_new.indices.create(index=index_name, body=index_content)
# 400表示索引已存在,404表示索引没找到
sync_dict["source"]["index"] = index_name
# sync_dict["source"]["type"] = list(index_content["mappings"])[0]
sync_dict["dest"]["index"] = index_name
# sync_dict["dest"]["type"] = list(index_content["mappings"])[0]
es_new.reindex(body=sync_dict, wait_for_completion=False)
else:
print(index_name)
https://elasticsearch-py.readthedocs.io/en/master/api.html#indices