1

I have an index users. These are my documents:

doc1 = {"user_id":1, "name":"first1 last1"}
doc2 = {"user_id":2, "name":"first2 last2"}

I'm trying to do a bulk insert using Python's requests

data_as_str = ""
data_as_str += json.dumps({ "_index": "users"}) + "\n"
data_as_str += json.dumps(doc1) + "\n"
data_as_str += json.dumps({ "_index": "users"}) + "\n"
data_as_str += json.dumps(doc2) + "\n"

headers = {'Content-type': 'application/json', 'Accept': 'text/plain'}

r = requests.post("https://ES_HOST/_bulk", auth=awsauth, headers=headers, data=data_as_str)

The error I get is illegal_argument_exception:

Malformed action/metadata line [1], expected START_OBJECT but found [START_ARRAY]

I've tried putting it inside a list and adding extra newlines, etc.

EDIT:

If I send json instead of data:

r = requests.post(bulkurl, auth=awsauth, headers=headers, json=data_as_str)

then the error is The bulk request must be terminated by a newline [\n]

But I do end it with a newline.

1

1 Answer 1

1

This works:

I had the format of the first dict wrong. I also changed the header, but it seems to work with the other one, as well. Send data, not json on the post request.

data_as_str = ""
data_as_str += json.dumps({"index": {"_index": "users","_id":1}}) + "\n"
data_as_str += json.dumps(doc1) + "\n"
data_as_str += json.dumps({"index": {"_index": "users","_id":2}}) + "\n"
data_as_str += json.dumps(doc2) + "\n"

headers = {'Content-Type': 'application/x-ndjson'}

r = requests.post("https://ES_HOST/_bulk", auth=awsauth, headers=headers, data=data_as_str)
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.