Elasticsearch Partial Update and Update by query


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')