Are function score filters internally optimized?


(gm42) #1

I will explain better with an example, if I have a function score like this:

"function_score": {
"query": {},
"functions": [
    {
        "filter": {
                       "bool": { "must_not": [{ "term": { "field_A": 12345 } ] }
                    },
        "FUNCTION": { /* something here returns zero */ }
    },
    {
        "filter": {
                       "bool": { "must": [{ "term": { "field_A": 12345 } ] }
                    },
        "FUNCTION": { /* something here returns non-zero */ }
    },
"min_score" : 1

}

So the first function flattens the score of a negated filter to 0, while the second one applies a custom score function. The intersection between first filter and second filter is empty e.g. they are exclusive.

If I do not specify the filter in the second function, will Elasticsearch waste computations calculating a score that is already supposed to be discarded? e.g. do I need to specify that filter or no?


(gm42) #2

To reply to myself: I just discovered score mode "first", that would do the trick I think.


(Lee Hinman) #3

Yes that's correct, "first" means to only execute the first function with filters that match, and then skip the others.


(system) #4