Elasticsearch Remote Index

Date: 2019/10/25 Categories: 工作 Tags: elasticsearch


需要从旧es集群迁移数据时使用, 需要的配置

  1. 再启动新集群时建旧集群的ip:port写入配置文件
  2. 调用reindex接口

参考了Reindex from a remote cluster

import requests
from elasticsearch import Elasticsearch
import fire

def main(source_host, dest_host, index):
    # source_host = '9.25.15.95'
    # dest_host = '100.115.135.106'
    # index = 'esindex_test__20190914_2135_mixedindex'
    print locals()
    source, dest = Elasticsearch(source_host), Elasticsearch(dest_host)
    def get_body():
        obj = source.indices.get(index).values()[0]
        del obj['settings']
        obj['settings'] = {
            'number_of_shards': '4',
            'number_of_replicas': '0',
            'refresh_interval': '60s',
        }
        return obj
    index_meta = get_body()
    if dest.indices.exists(index):
        print 'delete_index in dest: {}'.format(index)
        dest.indices.delete(index)
    print 'create_index in dest: {}'.format(index)
    dest.indices.create(index, index_meta)

    print 'import_index in dest: {}'.format(index)
    requests.post('http://{}:9200/_reindex'.format(dest_host), json={
        'source': {
            'remote': {'host': 'http://{}:9200'.format(source_host)},
            'index': index,
            'size': 1000,
        },
        'dest': {
            'index': index
        },
    })
    print 'create_alias in dest: {}'.format(index)
    dest.indices.put_alias(index=index, name=index.rstrip('_mixedindex'))


if __name__ == "__main__":
    fire.Fire(main)