Partial Update in Elasticsearch

Partial update in elasticsearch works same as updating whole document in Elasticsearch. This process retrieve the document, change it and reindex it again. In elasticsearch partial update is done through Update API. Update API also support scripting language to update a document. We can write our own logic in script like add new string in array of the document, increment the value of any field.

Difference between Whole document update and Partial Update?

The main difference in Update API is that all process happens within the shard. So it avoids the network overhead of multiple requests. Update API also reduces the chances of being conflicted from other processes.

_update API overwrite existing fields and add non existing fields in document.

To demonstrate I have added below document in my Elasticsearch index.

{
    "_index": "roopendra",
    "_type": "post",
    "_id": "100",
    "_version": 1,
    "found": true,
    "_source": {
        "title": "Test this test post",
        "content": "Testing update document in Elasticsearch",
        "date": "2015-10-04 12:22:22",
        "author": "roop"
    }
}

Adding new fields in existing elasticsearch document using Update API.

curl -XPOST 'localhost:9200/roopendra/post/100/_update' -d '{
   "doc" : {
        "tags": [ "elasticsearch"],
        "page_view": 1
   }
}'

Response:

{
    "_index": "roopendra",
    "_type": "post",
    "_id": "100",
    "_version": 2
}

Validate document:

curl -XGET 'http://localhost:9200/roopendra/post/100'

Response:

{
    "_index": "roopendra",
    "_type": "post",
    "_id": "100",
    "_version": 2,
    "found": true,
    "_source": {
        "title": "Test this test post",
        "content": "Testing update document in Elasticsearch",
        "date": "2015-10-04 12:22:22",
        "author": "roop",
        "tags": [
            "elasticsearch"
        ],
        "page_view": 1
    }
}

Partial update using script:

You can update document using script in Elasticsearch. Default scripting language in elasticsearch is Groovy. You can write your custom logic in script. In script _source refers to the document, which can be used as ctx._source. If I need to change value of the page_view then my _update command would be

curl -XPOST 'localhost:9200/roopendra/post/100/_update' -d '{
    "script" : "ctx._source.page_view+=1"
}'

Response:

{"_index":"roopendra","_type":"post","_id":"100","_version":3}

If you validate the document after update, you will find value of page_view now 2.

If dynamic scripting is disabled then you will get error message like below

{"error":"ElasticsearchIllegalArgumentException[failed to execute script]; nested: ScriptException[dynamic scripting for [mvel] disabled]; ","status":400}

Enabling dynamic scripting:
To enable dynamic scripting you need to add script.disable_dynamic: false in elasticsearch.yml and restart the elasticsearch.
Add this in elasticsearch.yml

script.disable_dynamic: false

After restart run the update command.

Reference:

https://www.elastic.co/guide/en/elasticsearch/reference/current/docs-update.html

(Visited 1,882 times, 69 visits today)