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?

3
  • you can place a null check for this field in the script doc['my_numeric_field'].value Commented Apr 1, 2017 at 18:05
  • Yes, that's what I'm doing although I haven't found great documentation on what I can do here. Commented Apr 3, 2017 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 Commented Mar 23, 2018 at 14:19

2 Answers 2

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

Sign up to request clarification or add additional context in comments.

Comments

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.