I have a couple of questions about scripting in elasticsearch, I hope someone can help me. I need to add several parameters from the document to _score
and sort by the total value. First, I will describe the data that I have and which need to be added:
rating
- a number from 1 to 9,duration_bucket
is a number from 0 to 2,rating_adj
[ {text
- text, if the passed parameter matches this value, the result will be changed to the next value.adj
- the number by which the result will be changed.
}]
- Well, the
score
itself, usually this value ranges from 1 to 4.
Initially, I just had a sort in this order:
score
rating
duration_bucket
But this gave a slightly different result. Therefore, a small script was written that would add all these values.
def found = null;
if (params.text != null) {
found = params._source['rating_adj'].find(item -> item.text == params.text);
}
def res = _score + params._source['duration_bucket'] + params._source['rating'];
if (found != null) {
return res + found.adj
}
return res;
And the first question. I've tried two options.
- Through
function score
and already sorted by this score. - Directly via
script sort
I did not notice the difference in performance, are there any significant differences in these approaches?
And the second question. When using this script, the processor is fully loaded, in contrast to the usual sorting. Are there any ways to optimize scripts or is it all about hardware?