# Convert String to Integer using Painless

**URL:** <https://discuss.elastic.co/t/convert-string-to-integer-using-painless/128560>\
**Category:** Kibana\
**Created:** [April 18, 2018, 3:00pm UTC](https://discuss.elastic.co/t/convert-string-to-integer-using-painless/128560 "2018-04-18T15:00:35Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![sahadevgd](https://avatars.discourse-cdn.com/v4/letter/s/6f9a4e/32.png) [@sahadevgd](https://discuss.elastic.co/u/sahadevgd)\
**Post date:** [April 18, 2018, 3:00pm UTC](https://discuss.elastic.co/t/convert-string-to-integer-using-painless/128560/1 "2018-04-18T15:00:36Z")

</div>

Hi Team,

I am trying to convert one of our field which is in String to Int using painless. Please find my code below.

Code:

> String s = doc['TotalDiskSpace'].value;  
> int tdvalue = Integer.parseInt(s);  
> return tdvalue;

Used the below in the scripted fields -  
Name - TotalDisk  
Type - Number  
Format - Number (default)

Also, should i use TotalDiskSpace or TotalDiskSpace.keyword ?  
I'm still unable to get the value. Am i missing anything. Please help.

Thanks.

---

<div class="post-metadata">

**Author:** ![PolterFox](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/polterfox/32/29781_2.png) [@PolterFox](https://discuss.elastic.co/u/PolterFox)\
**Post date:** [April 19, 2018, 12:29pm UTC](https://discuss.elastic.co/t/convert-string-to-integer-using-painless/128560/2 "2018-04-19T12:29:42Z")

</div>

Its recommended to use always the`.keyword` because every value which is not a .keyword must be loaded into RAM which slow down your ELK-Stack´s performance if you do not have much RAM. If you use the field without .keyword you must activate the fielddata for this field. (You can make this with a PUT-Message)

[https://www.elastic.co/guide/en/elasticsearch/reference/current/fielddata.html](https://www.elastic.co/guide/en/elasticsearch/reference/current/fielddata.html)

Which value do you get from the return value? Is there any error? (Please post it, if there is one)

PS.: I would also recommend you to use a null-check.

Best regards,  
Robert

---

<div class="post-metadata">

**Author:** ![sahadevgd](https://avatars.discourse-cdn.com/v4/letter/s/6f9a4e/32.png) [@sahadevgd](https://discuss.elastic.co/u/sahadevgd)\
**Post date:** [April 20, 2018, 12:30pm UTC](https://discuss.elastic.co/t/convert-string-to-integer-using-painless/128560/3 "2018-04-20T12:30:34Z")

</div>

Error message after i create the scripted field -

> Error: Request to Elasticsearch failed: {"error":{"root\_cause":[{"type":"script\_exception","reason":"runtime error","script\_stack":["java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)","java.lang.Integer.parseInt(Integer.java:583)","java.lang.Integer.parseInt(Integer.java:615)","tdvalue = Integer.parseInt(s);\n"," ^---- HERE"],"script":"String s = doc['TotalDiskSpace.keyword'].value;\nint tdvalue = Integer.parseInt(s);\nreturn tdvalue;","lang":"painless"}],"type":"search\_phase\_execution\_exception","reason":"all shards failed","phase":"query","grouped":true,"failed\_shards":[{"shard":0,"index":"check-2018.04.16","node":"sdfgsdfgsfd","reason":{"type":"script\_exception","reason":"runtime error","script\_stack":["java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)","java.lang.Integer.parseInt(Integer.java:583)","java.lang.Integer.parseInt(Integer.java:615)","tdvalue = Integer.parseInt(s);\n"," ^---- HERE"],"script":"String s = doc['TotalDiskSpace.keyword'].value;\nint tdvalue = Integer.parseInt(s);\nreturn tdvalue;","lang":"painless","caused\_by":{"type":"number\_format\_exception","reason":"For input string: "31568334500""}}}]},"status":500}

Scripted field -  
 ![Untitled](https://us1.discourse-cdn.com/elastic/original/3X/c/a/caa2e5f892e3fa569bb2f2f0b5604855b29347d2.png)

---

<div class="post-metadata">

**Author:** ![PolterFox](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/polterfox/32/29781_2.png) [@PolterFox](https://discuss.elastic.co/u/PolterFox)\
**Post date:** [April 20, 2018, 12:34pm UTC](https://discuss.elastic.co/t/convert-string-to-integer-using-painless/128560/4 "2018-04-20T12:34:48Z")

</div>

If you have Kibana 6.x please type in your script in the developer-console and let you give the result for the normal field (non-scripted). I think your field is containing non-numeric data so painless will fail to convert it.

---

<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 18, 2018, 12:34pm UTC](https://discuss.elastic.co/t/convert-string-to-integer-using-painless/128560/5 "2018-05-18T12:34:52Z")

</div>

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