Ankit Kumar
Ankit Kumar

Reputation: 51

How to bulk update a field in elasticsearch?

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.

Upvotes: 0

Views: 981

Answers (2)

A l w a y s S u n n y
A l w a y s S u n n y

Reputation: 38532

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)

Upvotes: 1

Lavneesh Chandna
Lavneesh Chandna

Reputation: 141

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.

Upvotes: 0

Related Questions