Lesson 15 / 25
Faceted Search With post_filter
Filters that do not shrink their own facet.
Facets that stay useful
An e-commerce sidebar shows facet counts (brand, colour, price) next to results. When a user selects "brand = Northpeak", the hits must be filtered, but the brand facet should still show the other brands with their counts so the user can switch or add more. post_filter solves this: it is applied to the hits after aggregations are computed, so aggregations see the unfiltered (query-only) set. To make other facets respect the brand selection, wrap them in a filter aggregation that applies the brand filter. Filters that should affect everything (for example the search text or in-stock) stay in the main query.
Selected brand in post_filter
Kibana Dev Tools console syntax; send the same requests with curl or a client library.
GET /products/_search
{
"query": {
"bool": {
"must": [ { "match": { "name": "jacket" } } ],
"filter": [ { "term": { "in_stock": true } } ]
}
},
"post_filter": { "term": { "brand": "Northpeak" } },
"aggs": {
"brands": { "terms": { "field": "brand" } },
"colors_for_selected_brand": {
"filter": { "term": { "brand": "Northpeak" } },
"aggs": { "colors": { "terms": { "field": "color" } } }
}
}
}A shop with a window
The post_filter decides what is on the counter in front of you, while the window display (the facet counts) still shows everything else the shop has, so you can change your mind.
Quick check: What does post_filter change?
- Only the returned hits, not the aggregations
- Only the aggregations, not the hits
- Both hits and aggregations equally
- The relevance scores of hits
Answer
Only the returned hits, not the aggregations — Aggregations are computed before post_filter is applied.