The 30 second answer
A filter in the bool query limits both the hits and the aggregations, and it's cacheable. A post_filter runs after the aggregations, so it limits only the hits; that's how selected facets narrow the product grid while facet counts still show the other options. The cost is that the main query matches more documents than are returned, so use post_filter only for the selected facet values, never for hard limits like market.
From the basics. An Elasticsearch search does two things over the same set of matching documents: it collects the top hits and it computes aggregations (facet counts). Where you put a condition decides which of the two it affects.
- **
query.bool.filter**: the condition is applied while matching. Documents that fail it are not hits and are not counted in any aggregation. - **
post_filter**: the docs say "the search hits are filtered after the aggregations are calculated. A post filter has no impact on the aggregation results."
Why facets need it. If the shopper selects brand Nike and you put that in filter, the brand aggregation only sees Nike products, so the brand facet shows one option and the shopper can't select a second brand. With post_filter, the aggregation still sees all brands in the category.
Badly written. The market and category are in post_filter too. Every aggregation now runs over all markets and categories, which is wrong and much more expensive:
{ "query": { "match_all": {} },
"post_filter": { "bool": { "filter": [
{ "term": { "market": "de" } }, { "term": { "category_id": "women-sneakers" } },
{ "terms": { "brand_id": ["nike"] } } ] } },
"aggs": { "brands": { "terms": { "field": "brand_id" } } } }Better. Hard limits in the query, only selected facet values in post_filter:
{ "query": { "bool": { "filter": [
{ "term": { "market": "de" } }, { "term": { "category_id": "women-sneakers" } } ] } },
"post_filter": { "terms": { "brand_id": ["nike"] } },
"aggs": { "brands": { "terms": { "field": "brand_id", "size": 30 } } } }Trade-offs.
- The main query matches the wider set, so hit collection does more work than with a plain filter. On a narrow facet selection over a huge category, that's measurable.
- Once you have several selected facets, each facet aggregation needs a
filterwrapper applying the others, as in the listing-page question. That adds aggregation work. - An alternative is two requests: one for hits with all filters, one for facets. It's simpler to reason about and cache, at the cost of an extra round trip.
Alternate interview scenarios
"Should a price slider be a filter or a post_filter?"
If the price facet shows a histogram that should stay visible while dragging, apply the price range in post_filter and exclude it from the price aggregation's own filter. If price is a hard business rule (for example a "under 50 euro" landing page), it goes in the main filter.
"Facet counts don't match what the shopper sees after clicking."
Usually a filter-placement bug: a condition is in the hits path but not in the aggregation's filter, or the other way round. Each facet aggregation must apply exactly the selections of the other facets.