0

I'm trying to make a script that can modify my score. So I made this:

if (!(doc['score_mod'].empty)) {
    _score * doc['score_mod'].value
}

but now I have a type called web_page that doesn't have the score_mod value and it's being generated via: https://github.com/codelibs/elasticsearch-river-web . So I can't mannually put the value in when it's being genereated.

Is there a way that I could have a static score for the web_page or have the groovy script check if that value exists?

The current code fails for the web_pages results, but for the ones with a score_mod value it works just fine

3
  • 1
    Have you tried _score * (doc['score_mod']?.value ?: 1) so it will default to 1 if score_mod is missing (or considered false) Commented Aug 7, 2015 at 11:18
  • Nope, never used groovy before, but it seems to work so thanks! Commented Aug 7, 2015 at 11:19
  • Cool! Added as an answer :-) Commented Aug 7, 2015 at 11:27

1 Answer 1

2

You should be able to use the elvis operator and the ?. shortcut operator like so:

_score * (doc['score_mod']?.value ?: 1)

So if doc['score_mod'] is null, or value is null (or zero, or empty) it will default to 1 (and multiply that by _score)

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

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.