Search code examples
elasticsearchelasticsearch-5elasticsearch-painless

Elasticsearch custom scoring function test null date values


I can't find anywhere examples of how to test null values in ES custom scoring functions. According to the doc the scripts are in groovy, according to the log the script is evaluated in painless, but even with that I'm left puzzled by some errors

"script":"doc['response_rate'].value ? (doc['response_rate'].value + 1) : 0",
"lang":"painless",
"caused_by":{
  "type":"wrong_method_type_exception",
  "reason":"cannot convert MethodHandle(Doubles)double to (Object)boolean"}}}]

This seems to say I'm trying to cas a double to boolean and raises, but I need to test for non-null values.

How should I write my scoring script ?

EDIT : I have understood that in painless I cannot use the ternary ? : operator, so I must write explicitely doc['xx'].value != null. However, this seems to produce weird results for dates that were indexed with null values. It would seem that in painless the value is NOT null (although it is indeed null in the json when I GET /_search it) and the following does not work

"script":"(doc['unavailable_until'].value != null) ? 1 : 0"

and always seem to return 0 (as if the null date was actually not null). I have seen some people reporting something some weird default date, in this case how can I compare this date to something like Date.now ?


Solution

  • I wonder why I couldn't find this page before...

    Basically, one can just call .empty to check for null values. Works with dates too

    "script":"doc['unavailable_until'].empty ? 1 : 0",