Scripted field error al crear

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"}

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

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}

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")

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

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")

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