Elasticsearch Python Interface Partial Update and Update by query

Help us to keep this website almost Ad Free! It takes only 10 seconds of your time:
> Step 1: Go view our video on YouTube: EF Core Bulk Extensions
> Step 2: And Like the video. BONUS: You can also share it!

Example

Partial Update: Used when a partial document update is needed to be done, i.e. in the following example the field name of the document with id doc_id is going to be updated to 'John'. Note that if the field is missing, it will just be added to the document.

doc = {
    "doc": {
        "name": "John"
    }
}
es.update(index='index_name',
          doc_type='doc_name',
          id='doc_id',
          body=doc)

Update by query: Used when is needed to update documents that satisfy a condition, i.e. in the following example we update the age of the documents whose name field matches 'John'.

q = {
  "script": {
    "inline": "ctx._source.age=23",
    "lang": "painless"
  },
  "query": {
    "match": {
        "name": "John"
    }
  }
}

es.update_by_query(body=q, 
                   doc_type='doc_name', 
                   index='index_name')


Got any Elasticsearch Question?