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('') && doc[''].value != null && !doc[''].empty) {
return (doc[''].value.substring(0,doc[''].value.indexOf('-')));
return 'Undefined';
below is the log of elasticsearch:
at java.util.concurrent.ThreadPoolExecutor.runWorker( [?:1.8.0_45]
at java.util.concurrent.ThreadPoolExecutor$ [?:1.8.0_45]
at [?:1.8.0_45]
Caused by: java.lang.StringIndexOutOfBoundsException: String index out of range: -1
at java.lang.String.substring( ~[?:1.8.0_45]
at org.elasticsearch.painless.PainlessScript$Script.execute(if (doc.containsKey('') && doc[''].value != null && !doc[''].empty) {
return (doc[''].value.substring(0,doc[''].value.indexOf('-')));
return 'Undefined';:195) ~[?:?]
at ~[?:?]
at ~[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.