# Are function score filters internally optimized?

**URL:** <https://discuss.elastic.co/t/are-function-score-filters-internally-optimized/61508>\
**Category:** Elasticsearch\
**Created:** [September 26, 2016, 1:03pm UTC](https://discuss.elastic.co/t/are-function-score-filters-internally-optimized/61508 "2016-09-26T13:03:50Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![gm42](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/gm42/32/11804_2.png) [@gm42](https://discuss.elastic.co/u/gm42)\
**Post date:** [September 26, 2016, 1:03pm UTC](https://discuss.elastic.co/t/are-function-score-filters-internally-optimized/61508/1 "2016-09-26T13:03:50Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![gm42](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/gm42/32/11804_2.png) [@gm42](https://discuss.elastic.co/u/gm42)\
**Post date:** [September 26, 2016, 1:13pm UTC](https://discuss.elastic.co/t/are-function-score-filters-internally-optimized/61508/2 "2016-09-26T13:13:09Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![dakrone](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dakrone/32/23351_2.png) [@dakrone](https://discuss.elastic.co/u/dakrone)\
**Post date:** [September 26, 2016, 4:24pm UTC](https://discuss.elastic.co/t/are-function-score-filters-internally-optimized/61508/3 "2016-09-26T16:24:36Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [July 5, 2017, 10:17pm UTC](https://discuss.elastic.co/t/are-function-score-filters-internally-optimized/61508/4 "2017-07-05T22:17:14Z")

</div>


