1

I am looking at elasticsearch's documentation for how to create a script for a custom score function which uses fields that are not queried for scoring at this link.

From what I can see you build a script function like so:

"script_score" : {
    "script" : {
      "lang": "painless",
      "inline": "_score * doc['my_numeric_field'].value"
    }
}

but I am wondering what is the syntax for replacing doc['my_numeric_field'].value if it is missing?

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
user3494047
  • 1,643
  • 4
  • 31
  • 61
  • you can place a null check for this field in the script doc['my_numeric_field'].value – user3775217 Apr 01 '17 at 18:05
  • Yes, that's what I'm doing although I haven't found great documentation on what I can do here. – user3494047 Apr 03 '17 at 14:29
  • @user3775217 How do you make a null check ? I'm getting some errors like `cannot convert MethodHandle(Doubles)double to (Object)boolean` when I try to use ternary operators `float_can_be_null ? float : some_default` – Cyril Duchon-Doris Mar 23 '18 at 14:19

2 Answers2

1

For me, the following worked (my version is 6.2.4):

          "script_score" : {
            "script" : {
              "source": "doc.containsKey('my_numeric_field') ? doc['my_numeric_field'].value : 0"
            }
        },

Found it at:

https://www.elastic.co/guide/en/elasticsearch/reference/master/modules-scripting-fields.html

Stefan Ocke
  • 249
  • 2
  • 6
1

Question is old, but maybe my answer would be useful for someone.

You should use size in order to check if value is missing:

doc['field'].size() == 0
rkm
  • 2,971
  • 22
  • 29