Skip to content Skip to sidebar Skip to footer

Index Json Files In Elasticsearch Using Python?

I have a bunch of JSON files(100), which are named as merged_file 1.json, merged_file 2. json and so on. How do I index all these files into elasticsearch using python(elasticsearc

Solution 1:

For this task you should be using elasticsearch-py (pip install elasticsearch):

from elasticsearch import Elasticsearch, helpers
import sys, json

es = Elasticsearch()

def load_json(directory):
    " Use a generator, no need to load all in memory"
    for filename in os.listdir(directory):
        if filename.endswith('.json'):
            with open(filename,'r') as open_file:
                yield json.load(open_file)

helpers.bulk(es, load_json(sys.argv[1]), index='my-index', doc_type='my-type')

Post a Comment for "Index Json Files In Elasticsearch Using Python?"