3

I have a directory full of JSON files that I want to index within elastic search. I've looked into pyelastic, yet I'm quite new to both python and elastic search. I've pasted some code below.

from pyelasticsearch import ElasticSearch

# ElasticSearch settings
ES_CLUSTER = 'http://localhost:9200/'
ES_INDEX = 'test'
ES_TYPE = 'doc'
es = ElasticSearch(ES_CLUSTER)

es.bulk_index(ES_INDEX, ES_TYPE, ???)

1 Answer 1

2

The function is called bulk to bulk index documents.

Loading them into an array then using bulk index will work. Alternatively index them one at a time if there is a very large amount of docs ( > 1000 )

from pyelasticsearch import ElasticSearch
import json
import os

es = ElasticSearch(ES_CLUSTER)

json_docs = []
for filename in os.listdir(os.getcwd()):
    if filename.endswith('.json'):
        with open(filename) as open_file:
            json_docs.append(json.load(open_file))

es.bulk(ES_INDEX, ES_TYPE, json_docs) 
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.