Getting string-out-of_bound exception while using Scripted field

Hi Team,

while using scripted field in metricbeat index, I am getting StringIndexOutOfBoundsException: String index out of range: -1 error while searching. my created scripted field is:

if (doc.containsKey('beat.name') && doc['beat.name.keyword'].value != null && !doc['beat.name.keyword'].empty) {
return (doc['beat.name.keyword'].value.substring(0,doc['beat.name.keyword'].value.indexOf('-')));
}
return 'Undefined';

below is the log of elasticsearch:

at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142) [?:1.8.0_45]
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617) [?:1.8.0_45]
at java.lang.Thread.run(Thread.java:745) [?:1.8.0_45]
Caused by: java.lang.StringIndexOutOfBoundsException: String index out of range: -1
at java.lang.String.substring(String.java:1955) ~[?:1.8.0_45]
at org.elasticsearch.painless.PainlessScript$Script.execute(if (doc.containsKey('beat.name') && doc['beat.name.keyword'].value != null && !doc['beat.name.keyword'].empty) {
return (doc['beat.name.keyword'].value.substring(0,doc['beat.name.keyword'].value.indexOf('-')));
}
return 'Undefined';:195) ~[?:?]
at org.elasticsearch.painless.ScriptImpl.run(ScriptImpl.java:105) ~[?:?]
at org.elasticsearch.search.fetch.subphase.ScriptFieldsFetchSubPhase.hitsExecute(ScriptFieldsFetchSubPhase.java:66) ~[elasticsearch-6.1.1.jar:6.1.1]

Are you sure all your keyword values always contain -? If any values do not contain it, you would get -1 from indexOf, which seems to be what is happening since the exception is on the substring call.

not all values contains -, still getting the error... Also i am using same script in another index for same purpose and it's working fine there...

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