Kiwi
Kiwi

Reputation: 2773

elasticsearch nest groovy script where value doesn't exist

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

Upvotes: 0

Views: 367

Answers (1)

tim_yates
tim_yates

Reputation: 171084

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)

Upvotes: 2

Related Questions