# Scripted field error al crear

**URL:** <https://discuss.elastic.co/t/scripted-field-error-al-crear/290667>\
**Category:** Kibana\
**Created:** [December 1, 2021, 1:18pm UTC](https://discuss.elastic.co/t/scripted-field-error-al-crear/290667 "2021-12-01T13:18:48Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Javier\_Garcia\_Alvare](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/javier_garcia_alvare/32/97224_2.png) [@Javier\_Garcia\_Alvare](https://discuss.elastic.co/u/Javier_Garcia_Alvare)\
**Post date:** [December 1, 2021, 1:18pm UTC](https://discuss.elastic.co/t/scripted-field-error-al-crear/290667/1 "2021-12-01T13:18:48Z")

</div>

Buenos días, soy nuevo en el manejo y estoy intentado crear un scripted field y me da error.  
Alguien me puede ayudar, Gracias.  
Nombre scripted\_field: PRUEBA  
if(doc['CAMPO'].value.toString().substring(0,5)==("valor")){return "new\_dato"}

---

<div class="post-metadata">

**Author:** ![majagrubic](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/majagrubic/32/74459_2.png) [@majagrubic](https://discuss.elastic.co/u/majagrubic)\
**Post date:** [December 1, 2021, 2:14pm UTC](https://discuss.elastic.co/t/scripted-field-error-al-crear/290667/2 "2021-12-01T14:14:20Z")

</div>

Hi, could you repeat the question in English and maybe post the error you're getting as well?

---

<div class="post-metadata">

**Author:** ![Javier\_Garcia\_Alvare](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/javier_garcia_alvare/32/97224_2.png) [@Javier\_Garcia\_Alvare](https://discuss.elastic.co/u/Javier_Garcia_Alvare)\
**Post date:** [December 1, 2021, 3:04pm UTC](https://discuss.elastic.co/t/scripted-field-error-al-crear/290667/3 "2021-12-01T15:04:47Z")

</div>

Hi, thanks in advance for the help, I have an index where I create the following scripted field  
if(doc['CANAL'].value.toString().substring(0,5)=="CLAVE"){return "SIM"}  
gives me the following error:  
Error with Painless scripted field 'if(doc['CANAL'].value.toString().substring(0,5)=="CLAVE"){return "SIM"}'.

You can address this error by editing the 'if(doc['CANAL'].value.toString().substring(0,5)=="CLAVE"){return "SIM"}' field in Management \> Index Patterns, under the “Scripted fields” tab.

Request to Elasticsearch failed: {"error":{"root\_cause":[{"type":"script\_exception","reason":"runtime error","script\_stack":["java.base/java.lang.String.checkBoundsBeginEnd(String.java:3319)","java.base/java.lang.String.substring(String.java:1874)","if(doc['CANAL'].value.toString().substring(0,5)=="CLAVE"){"," ^---- HERE"],"script":"if(doc['CANAL'].value.toString().substring(0,5)=="CLAVE"){return "SIM"}","lang":"painless"}],"type":"search\_phase\_execution\_exception","reason":"all shards failed","phase":"fetch","grouped":true,"failed\_shards":[{"shard":0,"index":"sgadlote2sms\_202110","node":"nrYm9c6nRYSIZuOhuTiHLQ","reason":{"type":"script\_exception","reason":"runtime error","script\_stack":["java.base/java.lang.String.checkBoundsBeginEnd(String.java:3319)","java.base/java.lang.String.substring(String.java:1874)","if(doc['CANAL'].value.toString().substring(0,5)=="CLAVE"){"," ^---- HERE"],"script":"if(doc['CANAL'].value.toString().substring(0,5)=="CLAVE"){return "SIM"}","lang":"painless","caused\_by":{"type":"string\_index\_out\_of\_bounds\_exception","reason":"begin 0, end 5, length 4"}}}]},"status":500}

---

<div class="post-metadata">

**Author:** ![majagrubic](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/majagrubic/32/74459_2.png) [@majagrubic](https://discuss.elastic.co/u/majagrubic)\
**Post date:** [December 1, 2021, 4:25pm UTC](https://discuss.elastic.co/t/scripted-field-error-al-crear/290667/4 "2021-12-01T16:25:34Z")

</div>

Certain values of your `CANAL` field are shorter than 5 characters; so when calling `substring(0, 5)`, Java throws an `IndexOutOfBoundsException`. You should change your if-statement to include length check as well:  
`if(doc['CANAL'].value.toString().length() >= 5 && doc['CANAL'].value.toString().substring(0,5)=="CLAVE")`

---

<div class="post-metadata">

**Author:** ![Javier\_Garcia\_Alvare](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/javier_garcia_alvare/32/97224_2.png) [@Javier\_Garcia\_Alvare](https://discuss.elastic.co/u/Javier_Garcia_Alvare)\
**Post date:** [December 2, 2021, 8:08am UTC](https://discuss.elastic.co/t/scripted-field-error-al-crear/290667/5 "2021-12-02T08:08:54Z")

</div>

Good morning, thank you I have tested and it works correctly.

---

<div class="post-metadata">

**Author:** ![Javier\_Garcia\_Alvare](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/javier_garcia_alvare/32/97224_2.png) [@Javier\_Garcia\_Alvare](https://discuss.elastic.co/u/Javier_Garcia_Alvare)\
**Post date:** [December 2, 2021, 11:14am UTC](https://discuss.elastic.co/t/scripted-field-error-al-crear/290667/6 "2021-12-02T11:14:31Z")

</div>

a question how can I get the last characters of the string, that is to say check that the last characters are \_BULK  
doc['CANAL'].value.toString().substring(0,-5)=="\_BULK")

---

<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:** [December 30, 2021, 11:15am UTC](https://discuss.elastic.co/t/scripted-field-error-al-crear/290667/7 "2021-12-30T11:15:12Z")

</div>

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