es库数据迁移

查询es中grade索引的数据量: 

es库数据迁移_第1张图片

新建一个索引,将另外一个索引中的数据迁移到新索引中,并添加新的字段。

示例代码:

from elasticsearch import Elasticsearch
import time

es = Elasticsearch(hosts='http://192.168.124.49:9200')
print(es)

# 新建新的索引mapping,新增加了source字段
mapping = {
    "mappings": {
        "properties": {
            "age": {
                "type": "long"
            },
            "character": {
                "type": "text",
                "fields": {
                    "keyword": {
                        "type": "keyword",
                        "ignore_above": 256
                    }
                }
            },
            "create_time": {
                "type": "text",
                "fields": {
                    "keyword": {
                        "type": "keyword",
                        "ignore_above": 256
                    }
                }
            },
            "grade": {
                "type": "long"
            },
            "id": {
                "type": "long"
            },
            "name": {
                "type": "text",
                "fields": {
                    "keyword": {
                        "type": "keyword",
                        "ignore_above": 256
                    }
                }
            },
            "sex": {
                "type": "text",
                "fields": {
                    "keyword": {
                        "type": "keyword",
                        "ignore_above": 256
                    }
                }
            },
            "subject": {
                "type": "text",
                "fields": {
                    "keyword": {
                        "type": "keyword",
                        "ignore_above": 256
                    }
                }
            },
            "source": {
                "type": "text",
                "fields": {
                    "keyword": {
                        "type": "keyword",
                        "ignore_above": 256
                    }
                }
            },
        }
    }
}

# 新建索引
new_index = es.indices.create(index='new_grade', body=mapping)
print(new_index)

# old索引中查询数据
query = {
    "query": {
        "match_all": {}
    }
}
res = es.search(index='grade', body=query, size=10000)
# print(res)
print(res['hits']['hits'])

start = time.time()
#  向新索引中添加数据
count = 0
for hit in res['hits']['hits']:
    data = hit["_source"]
    data.update({"source": "Grade index"})
    es.index(index="new_grade", id=data['id'], document=data)
    print(count)
    count += 1
print("耗时:", time.time() - start)

运行结果:

es库数据迁移_第2张图片

你可能感兴趣的:(ElasticSearch,elasticsearch,数据迁移)