# Getting string-out-of\_bound exception while using Scripted field

**URL:** <https://discuss.elastic.co/t/getting-string-out-of-bound-exception-while-using-scripted-field/115547>\
**Category:** Elasticsearch\
**Created:** [January 15, 2018, 12:53pm UTC](https://discuss.elastic.co/t/getting-string-out-of-bound-exception-while-using-scripted-field/115547 "2018-01-15T12:53:52Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Shubham\_Mahajan](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/shubham_mahajan/32/22140_2.png) [@Shubham\_Mahajan](https://discuss.elastic.co/u/Shubham_Mahajan)\
**Post date:** [January 15, 2018, 12:53pm UTC](https://discuss.elastic.co/t/getting-string-out-of-bound-exception-while-using-scripted-field/115547/1 "2018-01-15T12:53:52Z")

</div>

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]

---

<div class="post-metadata">

**Author:** ![rjernst](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/rjernst/32/6363_2.png) [@rjernst](https://discuss.elastic.co/u/rjernst)\
**Post date:** [January 15, 2018, 5:56pm UTC](https://discuss.elastic.co/t/getting-string-out-of-bound-exception-while-using-scripted-field/115547/2 "2018-01-15T17:56:39Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Shubham\_Mahajan](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/shubham_mahajan/32/22140_2.png) [@Shubham\_Mahajan](https://discuss.elastic.co/u/Shubham_Mahajan)\
**Post date:** [January 16, 2018, 5:12pm UTC](https://discuss.elastic.co/t/getting-string-out-of-bound-exception-while-using-scripted-field/115547/3 "2018-01-16T17:12:14Z")

</div>

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...

---

<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:** [February 13, 2018, 5:12pm UTC](https://discuss.elastic.co/t/getting-string-out-of-bound-exception-while-using-scripted-field/115547/4 "2018-02-13T17:12:17Z")

</div>

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