Painless string to int

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: class_cast_exception: org.elasticsearch.index.fielddata.ScriptDocValues$Strings cannot be cast to java.lang.Boolean
	at org.elasticsearch.painless.Def.DefToboolean( ~[?:?]
	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?

1 Like

I think this is not producing a boolean:


So you should probably test with:

doc['rcptto_count.keyword'] != null
1 Like

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 :slight_smile:


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