# Performance for Filter Context containing Query Context

**URL:** https://discuss.elastic.co/t/performance-for-filter-context-containing-query-context/179225
**Category:** Elasticsearch
**Created:** [May 1, 2019, 3:49pm UTC](https://discuss.elastic.co/t/performance-for-filter-context-containing-query-context/179225 "2019-05-01T15:49:41Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![Sam\_Sudekum](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/sam_sudekum/32/45376_2.png) [@Sam\_Sudekum](https://discuss.elastic.co/u/Sam_Sudekum)
#### Post date: [May 1, 2019, 3:49pm UTC](https://discuss.elastic.co/t/performance-for-filter-context-containing-query-context/179225/1 "2019-05-01T15:49:41Z")

</div>

[https://www.elastic.co/guide/en/elasticsearch/reference/2.4/query-filter-context.html](https://www.elastic.co/guide/en/elasticsearch/reference/2.4/query-filter-context.html)

I'm currently running ES 2.4 working on moving to 5.6. One paradigm shift that needs to happen is the use of bool.query/bool.filter.

There are performance incentives to use filters (non-scoring, and query caching). I want to take advantage of that behavior wherever I can.

Consider the following query:

```auto
	"query": {
	  	"bool": {
	  		"filter": {
	  			"bool": {
	  				"should": {
			  				"term": {
								"first_name": "bob"
							}
			  		},
                    "must": {
			  			"term": {
							"last_name": "smith"
						}
			  		}
	  			}
	  		}
	  	}
	}

```

Regardless of whether this is an optimal query, my question is about behavior. Normally, query.bool.must/should are scoring queries. When I run the query above, the score is normalized to 1 because it is wrapped in a filter. However, I'm curious if the underlying bool.should/must clause is still performing the scoring operations or if I've actually avoided that behavior.

---

<div class="post-metadata">

### Author: ![polyfractal](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/polyfractal/32/48162_2.png) [@polyfractal](https://discuss.elastic.co/u/polyfractal)
#### Post date: [May 6, 2019, 4:54pm UTC](https://discuss.elastic.co/t/performance-for-filter-context-containing-query-context/179225/2 "2019-05-06T16:54:55Z")

</div>

All clauses that are under the `filter` section inherit the "filter context", so scoring won't be executed on all those sub-clauses.

That does cause some behavioral changes though. If you embed a `bool` that has both a `should` and a `must` clause inside the filter of another bool, the `should` clause because useless. E.g. shoulds are optional and only affect scoring, so the inclusion of a must clause means the should is completely useless... it doesn't affect scoring and doesn't affect inclusion/exclusion.

I personally like to convert things to `filter/must_not` when inside a filter context just to keep semantics clear to a human, but it's not strictly necessary 🙂

---

<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: [June 3, 2019, 5:09pm UTC](https://discuss.elastic.co/t/performance-for-filter-context-containing-query-context/179225/3 "2019-06-03T17:09:21Z")

</div>

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.
