bulk批量建立索引python

# encoding=utf-8
import elasticsearch.helpers
from elasticsearch import Elasticsearch

path = '/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))

你可能感兴趣的:(python)