Using painless to alter field contents during a reindex...null pointer exception

I'm updating the mappings in a colleagues index and they're trying to change a field from a string to an int. That much is fine but the field currently has data in such as "89%" so obviously the data won't go in.

I'm trying to use Painless in the reindex query to modify the data but am getting a null_pointer_exception.

My reindex query is as follows,

POST _reindex
{
"conflicts": "proceed",
"source": {
"index": "old_index"
},
"dest": {
"index": "new_index"
},
"script": {
"lang": "painless",
"source": "if (ctx._source.the_score=="100 %") {ctx._source.the_score = 100} else {ctx._source.the_score = ctx._source.the_score.substring(0,2)}"
}
}

The error that's being output is as follows,

{
"error": {
"root_cause": [
{
"type": "script_exception",
"reason": "runtime error",
"script_stack": [
"ctx._source.the_score = ctx._source.the_score.substring(0,2)}",
" ^---- HERE"
],
"script": "if (ctx._source.the_score=="100 %") {ctx._source.the_score = 100} else {ctx._source.the_score = ctx._source.the_score.substring(0,2)}",
"lang": "painless"
}
],
"type": "script_exception",
"reason": "runtime error",
"script_stack": [
"ctx._source.the_score = ctx._source.the_score.substring(0,2)}",
" ^---- HERE"
],
"script": "if (ctx._source.the_score=="100 %") {ctx._source.the_score = 100} else {ctx._source.the_score = ctx._source.the_score.substring(0,2)}",
"lang": "painless",
"caused_by": {
"type": "null_pointer_exception",
"reason": null
}
},
"status": 500
}

I'm using Elasticsearch 5.6.8

Am I doing something wrong...or is there just a much better way of achieving what I'm trying to do?

You should see a full stacktrace in your logs. Can you add that here or link to a gist?

I'm running this on Elastic Cloud. It doesn't seem to be logging anything when I see the error after running the query in Kibana.

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