elasticsearch - Partial update and update by query - elastic - elastic search - elasticsearch tutorial - elasticsearch docker



Partial update

 Partial Update in ElasticSearch
  • Here is the sequence of steps used to perform a partial update on a document:
    • The client sends an update request to Node 1.
    • It forwards the request to Node 3, where the primary shard is allocated.
    • Node 3 retrieves the document from the primary shard, changes the JSON in the _sourcefield, and tries to reindex the document on the primary shard. If the document has already been changed by another process, it retries step 3 up to retry_on_conflict times, before giving up.
    • If Node 3 has achieved to update the document successfully, it forwards the new version of the document in parallel to the replica shards on Node 1 and Node 2 to be reindexed. Once all replica shards report success, Node 3 reports success to the coordinating node, which reports success to the client.

Here in this example the field name of the document with id doc_id is going to be updated to 'John'. Note: 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

  • The simplest usage of _update_by_query just performs an update on every document in the index without changing the source.
  • Update by query is used when we need to update documents that satisfy a condition. Here 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')

Related Searches to partial update and update by query