# Painless string to int

**URL:** <https://discuss.elastic.co/t/painless-string-to-int/172963>\
**Category:** Elasticsearch\
**Tags:** painless\
**Created:** [March 19, 2019, 12:30pm UTC](https://discuss.elastic.co/t/painless-string-to-int/172963 "2019-03-19T12:30:23Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![pjanzen](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/pjanzen/32/13756_2.png) [@pjanzen](https://discuss.elastic.co/u/pjanzen)\
**Post date:** [March 19, 2019, 12:30pm UTC](https://discuss.elastic.co/t/painless-string-to-int/172963/1 "2019-03-19T12:30:24Z")

</div>

Hi All,

Due to a mapping mistake I have a number field mapped as a string. I have updated my mapping already to integer so that is fixed. Due to the amount of indices and the amount of data is re-indexing not an option. To move forward I want to add an scripted field that converts the string to an int.

Trough a scripted field in kibana I try the following:

`if (doc['rcptto_count.keyword'] && !doc['rcptto_count.keyword'].empty) { return Integer.parseInt(doc['rcptto_count.keyword'].value); } else { return null;}`

However this results in an error:

```
Caused by: org.elasticsearch.common.io.stream.NotSerializableExceptionWrapper: class_cast_exception: org.elasticsearch.index.fielddata.ScriptDocValues$Strings cannot be cast to java.lang.Boolean
	at org.elasticsearch.painless.Def.DefToboolean(Def.java:626) ~[?:?]
	at org.elasticsearch.painless.PainlessScript$Script.execute(if (doc['rcptto_count.keyword'] && !doc['rcptto_count.keyword'].empty) { return Integer.parseInt(doc['rcptto_count.keyword'].value); } else { return null;}:8) ~[?:?]

```

Could someone help me out here in a solution?

---

<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:** [March 19, 2019, 2:05pm UTC](https://discuss.elastic.co/t/painless-string-to-int/172963/2 "2019-03-19T14:05:30Z")

</div>

I think this is not producing a boolean:

```auto
doc['rcptto_count.keyword']

```

So you should probably test with:

```auto
doc['rcptto_count.keyword'] != null

```

---

<div class="post-metadata">

**Author:** ![pjanzen](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/pjanzen/32/13756_2.png) [@pjanzen](https://discuss.elastic.co/u/pjanzen)\
**Post date:** [March 19, 2019, 4:00pm UTC](https://discuss.elastic.co/t/painless-string-to-int/172963/3 "2019-03-19T16:00:02Z")

</div>

I ended up with this.

```
if (doc.containsKey('rcptto_count') ) { 
	if (doc['rcptto_count.keyword'] != null) {
		return Integer.parseInt(doc['rcptto_count.keyword'].value) 
	}
}

```

So, who knows it might help some one 🙂

---

<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:** [April 16, 2019, 4:06pm UTC](https://discuss.elastic.co/t/painless-string-to-int/172963/4 "2019-04-16T16:06:55Z")

</div>

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