Performance for Filter Context containing Query Context

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:

	"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.

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 :slight_smile:

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