# Update with calculation

**URL:** https://discuss.elastic.co/t/update-with-calculation/156305
**Category:** Elasticsearch
**Created:** [November 12, 2018, 2:45pm UTC](https://discuss.elastic.co/t/update-with-calculation/156305 "2018-11-12T14:45:26Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![boeledi01](https://avatars.discourse-cdn.com/v4/letter/b/94ad74/32.png) [@boeledi01](https://discuss.elastic.co/u/boeledi01)
#### Post date: [November 12, 2018, 2:45pm UTC](https://discuss.elastic.co/t/update-with-calculation/156305/1 "2018-11-12T14:45:26Z")

</div>

Hi,

We have just discovered a bug in our system which made a wrong calculation.

Fortunately for us, all documents contain enough data to reprocess the calculation.

My question

How can I run an "update" so that it recomputes a field, based on the content of other fields ?

Example:  
{  
"amount": 5,  
"unit\_price": 25.2,  
"total\_price": 30.2 (wrong value, based on erroneous calculations)  
}  
I would like to replace the "total\_price" by the result of "amount" \* "unit\_price" in all documents (~500000000)

Many thanks for your help

---

<div class="post-metadata">

### Author: ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)
#### Post date: [November 12, 2018, 3:51pm UTC](https://discuss.elastic.co/t/update-with-calculation/156305/2 "2018-11-12T15:51:11Z")

</div>

Have a look at [https://www.elastic.co/guide/en/elasticsearch/reference/current/docs-update-by-query.html](https://www.elastic.co/guide/en/elasticsearch/reference/current/docs-update-by-query.html)

You can execute a script as explained in this doc.

---

<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: [December 10, 2018, 3:51pm UTC](https://discuss.elastic.co/t/update-with-calculation/156305/3 "2018-12-10T15:51:18Z")

</div>

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