简体   繁体   中英

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.

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)

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM