SamD
SamD

Reputation: 41

Update ElasticSearch document using Python requests

I am implementing ElasticSearch 7.1.1 in my application using Python requests library. I have successfully created a document in the elastic index using

r = requests.put(url, auth=awsauth, json=document, headers=headers)

However, while updating an existing document, the JSON body(containing to be updated values) that I pass to the method, replaces the original document. How do I overcome this? Thank you.

Upvotes: 1

Views: 2693

Answers (1)

Sandeep Kanabar
Sandeep Kanabar

Reputation: 1302

You could do the following:

document = {
    "doc": {
        "field_1": "value_1",
        "field_2": "value_2"
    },
    "doc_as_upsert": True
}

...
r = requests.post(url, auth=awsauth, json=document, headers=headers)
  1. It should be POST instead of PUT
  2. You can update existing fields and also add new fields.

Refer the doc in the comment posted by Nishant Saini.

Upvotes: 3

Related Questions