Facebooktwittergoogle_plusredditpinterestlinkedinmail

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.

Partial Update in Elasticsearchhttp://techieroop.com/wp-content/uploads/2015/10/Partial-Update-in-Elasticsearch.pnghttp://techieroop.com/wp-content/uploads/2015/10/Partial-Update-in-Elasticsearch-150x150.png Roopendra DevOPSElasticsearch,
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...
Partial update in <a href="http://techieroop.com/category/elasticsearch/#.VpHpSlLkrPk" target="_blank">elasticsearch </a>works same as <a href="http://techieroop.com/updating-whole-document-in-elasticsearch/" target="_blank">updating whole document in Elasticsearch</a>. 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? <blockquote>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.</blockquote> <strong>_update</strong> API overwrite existing fields and add non existing fields in document.To demonstrate I have added below document in my <a href="http://techieroop.com/category/elasticsearch/#.VpHpSlLkrPk" target="_blank">Elasticsearch </a>index. <pre>{ "_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" } }</pre> Adding new fields in existing elasticsearch document using <strong>Update API</strong>. <pre>curl -XPOST 'localhost:9200/roopendra/post/100/_update' -d '{ "doc" : { "tags": [ "elasticsearch"], "page_view": 1 } }'</pre> <strong>Response:</strong> <pre>{ "_index": "roopendra", "_type": "post", "_id": "100", "_version": 2 }</pre> <strong>Validate document:</strong> <pre>curl -XGET 'http://localhost:9200/roopendra/post/100'</pre> <strong>Response:</strong> <pre>{ "_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 } }</pre> <h3>Partial update using script:</h3> You can update document using script in <a href="http://techieroop.com/category/elasticsearch/#.VpHpSlLkrPk" target="_blank">Elasticsearch</a>. Default scripting language in elasticsearch is Groovy. You can write your custom logic in script. In script <strong>_source</strong> refers to the document, which can be used as <strong>ctx._source</strong>. If I need to change value of the page_view then my <em>_update </em>command would be <pre>curl -XPOST 'localhost:9200/roopendra/post/100/_update' -d '{ "script" : "ctx._source.page_view+=1" }'</pre> <strong>Response:</strong> <pre>{"_index":"roopendra","_type":"post","_id":"100","_version":3}</pre> If you validate the document after update, you will find value of <em>page_view</em> now 2.If dynamic scripting is disabled then you will get error message like below <pre>{"error":"ElasticsearchIllegalArgumentException[failed to execute script]; nested: ScriptException[dynamic scripting for [mvel] disabled]; ","status":400}</pre> <b>Enabling dynamic scripting:</b> To enable dynamic scripting you need to add <strong>script.disable_dynamic: fals</strong>e in <em>elasticsearch.yml</em> and restart the elasticsearch. Add this in elasticsearch.yml <pre>script.disable_dynamic: false</pre> After restart run the update command.
(Visited 1,892 times, 177 visits today)
Facebooktwittergoogle_pluslinkedinrss