1

I have a smallish (~50,00) array of json dictionaries that I want to store/index in ES. My preference is to use python, since the data I want to index is coming from a csv file, loaded and converted to json via python. Alternatively, I would like to skip the step of converting to json, and simply use the array of python dictionaries I have. Anyway, a quick search revealed the bulk indexing functionality of ES. I want to do something like this:

post_url = 'http://localhost:9202/_bulk'
request.post(post_url, data = acc )    # acc a python array of dictionaries

or

post_url = 'http://localhost:9202/_bulk'
request.post(post_url, params = acc )    # acc a python array of dictionaries

both request give a [HTTP 500 error]

3 Answers 3

4

My understanding is that you have to have one "command" per line (index, create, delete...) and then some of them (like index) takes a row of data on the next line like so

{'index': ''}\n
{'your': 'data'}\n
{'index': ''}\n
{'other': 'data'}\n

NB the new-lines, even on the last row.

Empty index objects like above works if you POST to ../index/type/_bulk or else you need to specify index and type I think, have not tried that.

Sign up to request clarification or add additional context in comments.

Comments

3

You the following function will do it:

def post_request(self, endpoint, data):
   endpoint = 'localhost:9200/_bulk'
   response = requests.post(endpoint, data=data, headers={'content-type':'application/json', 'charset':'UTF-8'})

   return response

As data you need to pass a String such:

{ "index" : { "_index" : "test-index", "_type" : "_doc", "_id" : "1681", "routing" : 0 }}
{ "field1" : ... , ..., "fieldN" : ... }
{ "index" : { "_index" : "test-index", "_type" : "_doc", "_id" : "1684", "routing" : 1 }}
{ "field1" : ... , ..., "fieldN" : ... }

Make sure you add a "\n" at the end of each line.

1 Comment

what is the datatype of your data, shouldn't it be list of dicts?
0

I don't know much about Python, but did you look at Pyes? Bulk is supported in Pyes.

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.