Python3连接Elasticsearch远程同步索引和数据

# -*- 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

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容