How are we supposed to check whether a key in map passed as parameter to a painless script has a value? I am currently doing this in Elasticsearch 6.8.4
if (params.feedId!=null) {
whatever()
}
but it throws this exception when
params.feedId
is null
ElasticsearchException[Elasticsearch exception [type=illegal_argument_exception, reason=Cannot cast null to a primitive type [void].]]
It works fine with Elasticsearch 5.6.13, so there seems to be a breaking change but I couldn't find anything so far.
UPDATE: It was actually not a problem with this part of the script but in some parts of it we were doing return null; to break the process of the script, which does not seem to be working anymore for some reason. To give more context, I am doing those updates using the Java API, more specifically the Rest High Level client.
I have been able to reproduce the issue using a curl command like this
curl -X POST "localhost:9200/myindex/_doc/1/_update?pretty" -H 'Content-Type: application/json' -d'
{
"script": {
"source": "ctx._source.result = true; return null;"
}
}'
This produces the next output:
{
"error" : {
"root_cause" : [
{
"type" : "remote_transport_exception",
"reason" : "[sBix--f][172.18.0.4:9300][indices:data/write/update[s]]"
}
],
"type" : "illegal_argument_exception",
"reason" : "failed to execute script",
"caused_by" : {
"type" : "script_exception",
"reason" : "compile error",
"script_stack" : [
"... ce.result = true; return null;",
" ^---- HERE"
],
"script" : "ctx._source.result = true; return null;",
"lang" : "painless",
"caused_by" : {
"type" : "illegal_argument_exception",
"reason" : "Cannot cast null to a primitive type [void]."
}
}
},
"status" : 400
}
Just replacing return null; for return; makes this script working fine in ES 6.8.4.
Found this issue at Elasticsearch: https://github.com/elastic/elasticsearch/issues/35888. There are some inconsistencies in the way short-circuit works in different versions of Elasticsearch.