# How to do inplace update in elasticsearch alternate to script?

**URL:** <https://discuss.elastic.co/t/how-to-do-inplace-update-in-elasticsearch-alternate-to-script/133377>\
**Category:** Elasticsearch\
**Created:** [May 26, 2018, 5:12am UTC](https://discuss.elastic.co/t/how-to-do-inplace-update-in-elasticsearch-alternate-to-script/133377 "2018-05-26T05:12:41Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![vasanth\_kumar](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/vasanth_kumar/32/101447_2.png) [@vasanth\_kumar](https://discuss.elastic.co/u/vasanth_kumar)\
**Post date:** [May 26, 2018, 5:12am UTC](https://discuss.elastic.co/t/how-to-do-inplace-update-in-elasticsearch-alternate-to-script/133377/1 "2018-05-26T05:12:41Z")

</div>

How to do inplace update in elasticsearch alternate to script?

Use-case is, while indexing we need to do something similar to this,

{  
total: 300[emp\_1+emp\_2]  
emp\_1: 100,  
emp\_2: 200,  
}

We tried script(ctx.\_source.total), but this'll be a very costly operation for us. Any alternative is there without get and put?

---

<div class="post-metadata">

**Author:** ![Christian\_Dahlqvist](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/christian_dahlqvist/32/4617_2.png) [@Christian\_Dahlqvist](https://discuss.elastic.co/u/Christian_Dahlqvist)\
**Post date:** [May 26, 2018, 8:11am UTC](https://discuss.elastic.co/t/how-to-do-inplace-update-in-elasticsearch-alternate-to-script/133377/2 "2018-05-26T08:11:46Z")

</div>

Can you provide a full example of what you are looking to do?

---

<div class="post-metadata">

**Author:** ![vasanth\_kumar](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/vasanth_kumar/32/101447_2.png) [@vasanth\_kumar](https://discuss.elastic.co/u/vasanth_kumar)\
**Post date:** [May 26, 2018, 11:49am UTC](https://discuss.elastic.co/t/how-to-do-inplace-update-in-elasticsearch-alternate-to-script/133377/3 "2018-05-26T11:49:53Z")

</div>

Christian,

PUT data\_index  
{  
"settings": { },  
"mappings": {  
"data": {  
"properties": {  
"total\_count": {  
"type": "long"  
},  
"count\_1": {  
"type": "long"  
},  
"count\_2": {  
"type": "long"  
}  
}  
}  
}  
}

PUT lp\_data\_index/data/1  
{  
count\_1:10,  
count\_2:10  
}

After this put, total\_count value should be 20(count\_1+count\_2).

Another use-case,  
POST data\_index/data/1  
{  
"doc": {  
"count\_1": 5  
}  
}

So in this use-case, the total\_count should be (5+10) 15. So how to achieve this without doing get operation?

Currently we're doing like this,  
if(ctx.\_source.containsKey('count\_1')){ctx.\_source.total\_count+=ctx.\_source.count\_1; }

So is there way to achieve without script?

---

<div class="post-metadata">

**Author:** ![Christian\_Dahlqvist](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/christian_dahlqvist/32/4617_2.png) [@Christian\_Dahlqvist](https://discuss.elastic.co/u/Christian_Dahlqvist)\
**Post date:** [May 27, 2018, 6:06am UTC](https://discuss.elastic.co/t/how-to-do-inplace-update-in-elasticsearch-alternate-to-script/133377/4 "2018-05-27T06:06:15Z")

</div>

Any update operation will result in a full reindex of the document, so I don’t think there is a more efficient way. How frequently are you updating a single document?

---

<div class="post-metadata">

**Author:** ![vasanth\_kumar](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/vasanth_kumar/32/101447_2.png) [@vasanth\_kumar](https://discuss.elastic.co/u/vasanth_kumar)\
**Post date:** [May 27, 2018, 9:10am UTC](https://discuss.elastic.co/t/how-to-do-inplace-update-in-elasticsearch-alternate-to-script/133377/5 "2018-05-27T09:10:04Z")

</div>

Christian,  
In a minute, single document can be updated 10-15 times approx. So script is the best way?

---

<div class="post-metadata">

**Author:** ![Christian\_Dahlqvist](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/christian_dahlqvist/32/4617_2.png) [@Christian\_Dahlqvist](https://discuss.elastic.co/u/Christian_Dahlqvist)\
**Post date:** [May 27, 2018, 5:04pm UTC](https://discuss.elastic.co/t/how-to-do-inplace-update-in-elasticsearch-alternate-to-script/133377/6 "2018-05-27T17:04:23Z")

</div>

Frequent updates of a single document is inefficient as it results in a lot of small refreshes and merges, so it would be better to aggregate in memory in the client and the store this periodically in Elasticsearch.

---

<div class="post-metadata">

**Author:** ![vasanth\_kumar](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/vasanth_kumar/32/101447_2.png) [@vasanth\_kumar](https://discuss.elastic.co/u/vasanth_kumar)\
**Post date:** [May 30, 2018, 5:39pm UTC](https://discuss.elastic.co/t/how-to-do-inplace-update-in-elasticsearch-alternate-to-script/133377/7 "2018-05-30T17:39:44Z")

</div>

Christian,  
Client level aggregation is not possible in our use-case because we are dealing with millions of docs. So can we bring the document at the client side, compute the value and then write to ES? will this be faster then scripted update?

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [June 27, 2018, 5:39pm UTC](https://discuss.elastic.co/t/how-to-do-inplace-update-in-elasticsearch-alternate-to-script/133377/8 "2018-06-27T17:39:44Z")

</div>

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.
