# Is it possible to perform calculations using multiple fields in a document

**URL:** https://discuss.elastic.co/t/is-it-possible-to-perform-calculations-using-multiple-fields-in-a-document/28220
**Category:** Elasticsearch
**Created:** [August 28, 2015, 5:08am UTC](https://discuss.elastic.co/t/is-it-possible-to-perform-calculations-using-multiple-fields-in-a-document/28220 "2015-08-28T05:08:24Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![spiral\_circ](https://avatars.discourse-cdn.com/v4/letter/s/e68b1a/32.png) [@spiral\_circ](https://discuss.elastic.co/u/spiral_circ)
#### Post date: [August 28, 2015, 5:08am UTC](https://discuss.elastic.co/t/is-it-possible-to-perform-calculations-using-multiple-fields-in-a-document/28220/1 "2015-08-28T05:08:24Z")

</div>

I am new to elasticsearch, and trying to evaluate if my sql query can be migrated to elastic search.

1. Is it possible to write an elasticsearch query that returns calculations performed using multiple fields in a document?  
Ex: if I have a document like {"salary": 100000, "spouse\_salary":200000} , I want the query result to give me a field called total\_salary with a value of salary+spouse\_salary (300000 in this case)

2. Is it possible to perform calculations as stated above but with also user supplied parameters into the mix?  
Ex: If the tax on the salary needs to be calculated before the results are returned, and the tax % is passed in the query(I am not sure how though, is there a way to do that ? ), If i pass .05 as tax\_rate to the query, i want a response like total\_tax = 300000 \* .05

I have skimmed through the documentation, but most of the aggregations/calculations appear to be on a single field like sum of salary, avg of salary etc, but couldn't find calculations using multiple fields, like salary+spouse\_salary

Any pointers is appreciated.

---

<div class="post-metadata">

### Author: ![Joshua\_Rich](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/joshua_rich/32/44953_2.png) [@Joshua\_Rich](https://discuss.elastic.co/u/Joshua_Rich)
#### Post date: [August 28, 2015, 5:57am UTC](https://discuss.elastic.co/t/is-it-possible-to-perform-calculations-using-multiple-fields-in-a-document/28220/2 "2015-08-28T05:57:43Z")

</div>

There is probably two options. First option, do this calculation in your client application. Second option, use a [scripted metric](https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-metrics-scripted-metric-aggregation.html). Yep, nearly all built-in aggregations work on single fields only.

---

<div class="post-metadata">

### Author: ![colings86](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/colings86/32/44960_2.png) [@colings86](https://discuss.elastic.co/u/colings86)
#### Post date: [August 28, 2015, 7:39am UTC](https://discuss.elastic.co/t/is-it-possible-to-perform-calculations-using-multiple-fields-in-a-document/28220/3 "2015-08-28T07:39:08Z")

</div>

You could also use a script in the sum aggregation itself to achieve what you describe above. see here for more information: [https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-metrics-sum-aggregation.html#\_script\_3](https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-metrics-sum-aggregation.html#_script_3)

In your case you could do something like:

```auto
{
    ...,

    "aggs" : {
        "salary" : { "sum" : { "script" : "doc['salary'].value + doc['spouse_salary'].value" } }
    }
}

```

---

<div class="post-metadata">

### Author: ![spiral\_circ](https://avatars.discourse-cdn.com/v4/letter/s/e68b1a/32.png) [@spiral\_circ](https://discuss.elastic.co/u/spiral_circ)
#### Post date: [August 29, 2015, 3:28pm UTC](https://discuss.elastic.co/t/is-it-possible-to-perform-calculations-using-multiple-fields-in-a-document/28220/4 "2015-08-29T15:28:29Z")

</div>

Thank you Joshua, colings86,  
I am exploring writing Java plugin for my requirement.

---

<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: [July 5, 2017, 11:53pm UTC](https://discuss.elastic.co/t/is-it-possible-to-perform-calculations-using-multiple-fields-in-a-document/28220/5 "2017-07-05T23:53:10Z")

</div>


