bulk批量建立索引python

来源:互联网 发布:西西软件手机版 编辑:程序博客网 时间:2024/06/06 08:39
# encoding=utf-8import elasticsearch.helpersfrom elasticsearch import Elasticsearchpath = '/home/fhqplzj/data/orion/news.json'es = Elasticsearch('localhost:9200')my_index, my_type = 'test_index', 'test_type'if es.indices.exists(index=my_index):    es.indices.delete(index=my_index)es.indices.create(index=my_index)def map_to_actions(lines):    lines = filter(lambda x: len(x) > 0, map(lambda x: x.strip(), lines))    for idx, line in enumerate(lines):        yield {            '_op_type': 'index',            '_index': my_index,            '_type': my_type,            '_id': idx + 1,            '_source': line        }with open(path) as f:    contents = f.readlines()elasticsearch.helpers.bulk(es, actions=map_to_actions(contents))

0 0