# Not able to get correct data using painless scripting

**URL:** <https://discuss.elastic.co/t/not-able-to-get-correct-data-using-painless-scripting/179905>\
**Category:** Elasticsearch\
**Tags:** painless\
**Created:** [May 7, 2019, 7:03am UTC](https://discuss.elastic.co/t/not-able-to-get-correct-data-using-painless-scripting/179905 "2019-05-07T07:03:50Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ishan\_deep](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/ishan_deep/32/45690_2.png) [@Ishan\_deep](https://discuss.elastic.co/u/Ishan_deep)\
**Post date:** [May 7, 2019, 7:03am UTC](https://discuss.elastic.co/t/not-able-to-get-correct-data-using-painless-scripting/179905/1 "2019-05-07T07:03:50Z")

</div>

I'm having 3 different TEXT fields containing NUMBERS and i'm trying to add all those values to a new fields as numeric data.

one of those field is:  
// deliveryGroups.0.deliveryGroupId : 1

the painless script i'm using is :  
//doc['deliveryGroups.0.deliveryGroupId'].value+ doc['deliveryGroups.1.deliveryGroupId'].value+ doc['deliveryGroups.2.deliveryGroupId'].value+ doc['deliveryGroups.3.deliveryGroupId'].value+ doc['deliveryGroups.4.deliveryGroupId'].value

this leads to concatenation of numbers in all fields and i get a result as : 1 or 12 or 123 or 124 instead of 1 or 3 or 6 or 7.

can someone please help me out!

---

<div class="post-metadata">

**Author:** ![spinscale](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/spinscale/32/25011_2.png) [@spinscale](https://discuss.elastic.co/u/spinscale)\
**Post date:** [May 8, 2019, 8:13am UTC](https://discuss.elastic.co/t/not-able-to-get-correct-data-using-painless-scripting/179905/2 "2019-05-08T08:13:49Z")

</div>

as you already mentioned that you have text fields, the content is treated as a string, so string concatenation happens.

Try casting the values into a number, i.e. using `Long.valueOf()` and then adding them,

--Alex

---

<div class="post-metadata">

**Author:** ![Ishan\_deep](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/ishan_deep/32/45690_2.png) [@Ishan\_deep](https://discuss.elastic.co/u/Ishan_deep)\
**Post date:** [May 28, 2019, 7:44am UTC](https://discuss.elastic.co/t/not-able-to-get-correct-data-using-painless-scripting/179905/3 "2019-05-28T07:44:34Z")

</div>

thanks a ton!

---

<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 25, 2019, 7:44am UTC](https://discuss.elastic.co/t/not-able-to-get-correct-data-using-painless-scripting/179905/4 "2019-06-25T07:44:36Z")

</div>

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