# Scripted field convert string to number

**URL:** <https://discuss.elastic.co/t/scripted-field-convert-string-to-number/198646>\
**Category:** Kibana\
**Created:** [September 9, 2019, 9:27am UTC](https://discuss.elastic.co/t/scripted-field-convert-string-to-number/198646 "2019-09-09T09:27:37Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Sjaak01](https://avatars.discourse-cdn.com/v4/letter/s/73ab20/32.png) [@Sjaak01](https://discuss.elastic.co/u/Sjaak01)\
**Post date:** [September 9, 2019, 9:27am UTC](https://discuss.elastic.co/t/scripted-field-convert-string-to-number/198646/1 "2019-09-09T09:27:37Z")

</div>

Hi,

I need to convert a string to a number.

I wrote this script but it does not work and causes no data to display anymore when looking at discovery.

```
if (doc['stringnumber']) {
    Integer.parseInt(doc['stringnumber'].value)
}

```

The field type in the scripted field is set as a number.

The source data looks like this: `077.8`.

Is it possible to convert a string to a number with a scripted field?

---

<div class="post-metadata">

**Author:** ![lukas](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/lukas/32/6812_2.png) [@lukas](https://discuss.elastic.co/u/lukas)\
**Post date:** [September 12, 2019, 5:44pm UTC](https://discuss.elastic.co/t/scripted-field-convert-string-to-number/198646/2 "2019-09-12T17:44:29Z")

</div>

You're going to need to `return` inside the `if` statement.

---

<div class="post-metadata">

**Author:** ![Sjaak01](https://avatars.discourse-cdn.com/v4/letter/s/73ab20/32.png) [@Sjaak01](https://discuss.elastic.co/u/Sjaak01)\
**Post date:** [September 13, 2019, 12:48am UTC](https://discuss.elastic.co/t/scripted-field-convert-string-to-number/198646/3 "2019-09-13T00:48:28Z")

</div>

Thanks but that doesn't change anything.

The problem seems to be that the initial if statement just doesn't do anything but no information is provided as to why.

I tried a simple test on two indices just to see if the if statement works.

```
if (doc['myfield'].value == "some value") { 
    return "YES"
} else {
    return "NO"
}

```

Does not return anything in the test screen.

I got another index where I had Elastic do the mapping and for some reason it does work there by using 'myfield.keyword'. Adding .keyword to the field for the other two indices does not work.

In the two indices that don't return any results, `myfield` is mapped as `keyword` only.

```
"myfield" : {
  "type" : "keyword",
  "ignore_above" : 256
}
```

---

<div class="post-metadata">

**Author:** ![dpr](https://avatars.discourse-cdn.com/v4/letter/d/8797f3/32.png) [@dpr](https://discuss.elastic.co/u/dpr)\
**Post date:** [September 13, 2019, 3:12am UTC](https://discuss.elastic.co/t/scripted-field-convert-string-to-number/198646/4 "2019-09-13T03:12:41Z")

</div>

One problem is that "077.8" is not an integer.

Try this:

```
if (doc['stringnumber']) {
  return Double.parseDouble(doc['stringnumber'].value)
}

```

Note that you can troubleshoot scripted fields nicely by using constants instead of document fields until you've got the syntax correct, then drop in your fields. To do this, you would simply build a scripted field with this code:

```
return Double.parseDouble("077.8");
```

---

<div class="post-metadata">

**Author:** ![Sjaak01](https://avatars.discourse-cdn.com/v4/letter/s/73ab20/32.png) [@Sjaak01](https://discuss.elastic.co/u/Sjaak01)\
**Post date:** [September 13, 2019, 6:45am UTC](https://discuss.elastic.co/t/scripted-field-convert-string-to-number/198646/5 "2019-09-13T06:45:58Z")

</div>

Thanks for the tip. I figured out the problem is with the if statement. Apparently they don't work on strings.

This script sort of works though it produces a lot of unnecessary data.

However this could work in my benefit. Is it possible to use painless to check for a last known value instead of only returning a static value?

This probably won't work as I would need to last known value before there is another known value and not simply the last known value.

If I had something like this  
100  
0  
0  
0  
200  
0  
0  
0  
300

I would want it to look like this  
100  
100  
100  
100  
200  
200  
200  
200  
300

```
if (doc['stringnumber'].empty) { 
    return "0" 
} else { 
    return Double.parseDouble(doc['stringnumber'].value); 
}

```

> Limitations
> 
> There are a few limitations relative to other script languages:
> 
> - Only numeric, boolean, date, and geo\_point fields may be accessed
> - Stored fields are not available

---

<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:** [October 11, 2019, 6:46am UTC](https://discuss.elastic.co/t/scripted-field-convert-string-to-number/198646/6 "2019-10-11T06:46:00Z")

</div>

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