# Sum of two fields into a one custom field

**URL:** https://discuss.elastic.co/t/sum-of-two-fields-into-a-one-custom-field/127600
**Category:** Elasticsearch
**Created:** [April 11, 2018, 9:47am UTC](https://discuss.elastic.co/t/sum-of-two-fields-into-a-one-custom-field/127600 "2018-04-11T09:47:53Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![prasad.ram1431](https://avatars.discourse-cdn.com/v4/letter/p/c89c15/32.png) [@prasad.ram1431](https://discuss.elastic.co/u/prasad.ram1431)
#### Post date: [April 11, 2018, 9:47am UTC](https://discuss.elastic.co/t/sum-of-two-fields-into-a-one-custom-field/127600/1 "2018-04-11T09:47:53Z")

</div>

Hi ,

I am new to elasticsearch, I have a requirement like, i have data indexed into elasticsearch, which contains all string fields. I need to do sum of two fields among them into one custom field. Is it possible to perform sum on two string fields using elasticsearch query. If its possible, kindly give me some examples.

Thank you very much in advance for your response.

Regards,  
Prasad

---

<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: [April 11, 2018, 10:05am UTC](https://discuss.elastic.co/t/sum-of-two-fields-into-a-one-custom-field/127600/2 "2018-04-11T10:05:05Z")

</div>

May be using a script field. See [https://www.elastic.co/guide/en/elasticsearch/reference/6.2/search-request-script-fields.html](https://www.elastic.co/guide/en/elasticsearch/reference/6.2/search-request-script-fields.html)

But I'd recommend doing that at index time as this will slow your queries.

You can use the ingest pipeline feature and put your script there.

---

<div class="post-metadata">

### Author: ![prasad.ram1431](https://avatars.discourse-cdn.com/v4/letter/p/c89c15/32.png) [@prasad.ram1431](https://discuss.elastic.co/u/prasad.ram1431)
#### Post date: [April 11, 2018, 10:11am UTC](https://discuss.elastic.co/t/sum-of-two-fields-into-a-one-custom-field/127600/3 "2018-04-11T10:11:58Z")

</div>

Hi David,

Thank you for your reply, but I wanted whether this will work for String type fields or only for numeric fields.

Thanks,  
Prasad

---

<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: [April 11, 2018, 10:23am UTC](https://discuss.elastic.co/t/sum-of-two-fields-into-a-one-custom-field/127600/4 "2018-04-11T10:23:32Z")

</div>

As long as it's a script it will work with whatever you want. I'm not telling you how to write the script though. Which I don't know as I'm not using painless.

I always prefer modifying my data before indexing it.

---

<div class="post-metadata">

### Author: ![prasad.ram1431](https://avatars.discourse-cdn.com/v4/letter/p/c89c15/32.png) [@prasad.ram1431](https://discuss.elastic.co/u/prasad.ram1431)
#### Post date: [April 11, 2018, 10:38am UTC](https://discuss.elastic.co/t/sum-of-two-fields-into-a-one-custom-field/127600/5 "2018-04-11T10:38:43Z")

</div>

thank you very much for your suggestion, but my project requirement is in this way, so i need to go this way.

Thanks,  
Prasad

---

<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: [April 11, 2018, 10:58am UTC](https://discuss.elastic.co/t/sum-of-two-fields-into-a-one-custom-field/127600/6 "2018-04-11T10:58:07Z")

</div>

Good luck.

Out of curiosity, do you know why it's a 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: [May 9, 2018, 10:58am UTC](https://discuss.elastic.co/t/sum-of-two-fields-into-a-one-custom-field/127600/7 "2018-05-09T10:58:16Z")

</div>

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