0
def update(index):
    index = "twitter"
    list = ['0oPwSm4BxbPrifDrF7C1', 'r4MOWm4BxbPrifDrjbgR', 'y4NbWm4BxbPrifDrLLhh']
    data  = []
    for i in list:
        d = { "update" : {"_id" : i, "_index" : index, "retry_on_conflict" : 3} }
        e = { "doc" : {"answer_1" : "test"} }
        data.append(json.dumps(d))
        data.append(json.dumps(e))
    v = "\n".join(data)
    response = requests.post('https://url/_bulk', headers='application/x-ndjson', 
    data=json.loads(v)

I want to bulk update the answer field for different documents. Unable to send request in a proper format i guess.

1

2 Answers 2

1

The bulk data set should be like this,

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

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

Your existing data seems OK to me,

{"update": {"_id": "0oPwSm4BxbPrifDrF7C1", "retry_on_conflict": 3, "_index": "twitter"}}
{"doc": {"answer_1": "test"}}
{"update": {"_id": "r4MOWm4BxbPrifDrjbgR", "retry_on_conflict": 3, "_index": "twitter"}}
{"doc": {"answer_1": "test"}}
{"update": {"_id": "y4NbWm4BxbPrifDrLLhh", "retry_on_conflict": 3, "_index": "twitter"}}
{"doc": {"answer_1": "test"}}

You have got a syntax error on the request.post() where you missed ending parenthesis ) and need to send v directly without using extra json.loads(v)

response = requests.post('https://url/_bulk', 
                         data=v,
                         headers={'content-type':'application/json', 
                         'charset':'UTF-8'})
print(response)
Sign up to request clarification or add additional context in comments.

Comments

0

This seems to be the problem

data=json.loads(v)

'v' does not contain a parsable json string, it contains multiple JSON documents seperated by new lines. Try sending v directly without parsing.

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.