# Script appends instead of adding the value

**URL:** <https://discuss.elastic.co/t/script-appends-instead-of-adding-the-value/105646>\
**Category:** Elasticsearch\
**Created:** [October 28, 2017, 11:16pm UTC](https://discuss.elastic.co/t/script-appends-instead-of-adding-the-value/105646 "2017-10-28T23:16:17Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Aisha1990](https://avatars.discourse-cdn.com/v4/letter/a/b2d939/32.png) [@Aisha1990](https://discuss.elastic.co/u/Aisha1990)\
**Post date:** [October 28, 2017, 11:16pm UTC](https://discuss.elastic.co/t/script-appends-instead-of-adding-the-value/105646/1 "2017-10-28T23:16:17Z")

</div>

Hi All,

I am trying to update the size field of a document using script however, instead of adding 2 to the existing value (9) of the size field, it is appending it.

Document is -

{  
"\_index" : "products",  
"\_type" : "shoes",  
"\_id" : "2",  
"\_version" : 1,  
"found" : true,  
"\_source" : {  
"name" : "Adidas",  
"size" : "9",  
"color" : "black"  
}  
}

curl -X POST -d "{"script":"ctx.\_source.size += 2"}" -H "Content-type: application/json" -X POST "[http://localhost:9200/products/shoes/2/\_update?pretty](http://localhost:9200/products/shoes/2/_update?pretty)"

{  
"\_index" : "products",  
"\_type" : "shoes",  
"\_id" : "2",  
"\_version" : 2,  
"found" : true,  
"\_source" : {  
"name" : "Adidas",  
"size" : "92", \<------------------------------------------------------ I want (9+2=11) but it is appending 9 to 2 (92)  
"color" : "black"  
}  
}

Kindly advise.

---

<div class="post-metadata">

**Author:** ![Igor\_Motov](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/igor_motov/32/45193_2.png) [@Igor\_Motov](https://discuss.elastic.co/u/Igor_Motov)\
**Post date:** [October 29, 2017, 12:08am UTC](https://discuss.elastic.co/t/script-appends-instead-of-adding-the-value/105646/2 "2017-10-29T00:08:55Z")

</div>

> [@Aisha1990](#):
>
> "size" : "9",

It looks like when you originally added the data, the size contained a string `"size" : "9"` instead of a number `"size" : 9`. As a result this field was mapped as a string field instead of a long. The string `+` operation concatenates string, as a result you are getting back the string `"92"` instead of the number `11`. The simplest way to solve this issue is to reindex your data while making sure that the field `size` is mapped as a number.

Alternatively, you can also convert from a string to integer and back to string on the fly in your script by doing something like this `ctx._source.size = (Integer.parseInt(ctx._source.size) + 2).toString()`

---

<div class="post-metadata">

**Author:** ![Aisha1990](https://avatars.discourse-cdn.com/v4/letter/a/b2d939/32.png) [@Aisha1990](https://discuss.elastic.co/u/Aisha1990)\
**Post date:** [October 29, 2017, 10:44pm UTC](https://discuss.elastic.co/t/script-appends-instead-of-adding-the-value/105646/3 "2017-10-29T22:44:27Z")

</div>

Thank you Igor. Its now clear.

---

<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:** [November 26, 2017, 10:44pm UTC](https://discuss.elastic.co/t/script-appends-instead-of-adding-the-value/105646/4 "2017-11-26T22:44:52Z")

</div>

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