Faceted Search and Filters
What you'll learn: How to build multi-dimensional filtering interfaces using pre-computed aggregations over indexed documents.
The Core Idea
Faceted search lets users narrow results by selecting multiple categorical filters simultaneously—think shopping sites where you filter by brand, price range, color, and size at once. Behind the scenes, this requires aggregating counts across indexed fields so you can show "Brand A (342)" and "Brand B (127)" before the user clicks anything.
The trick is computing these counts efficiently. You can't scan millions of documents on every query. Instead, modern search engines maintain:
- Field-level indexes for filterable attributes (category, brand, price_bucket, etc.)
- Aggregation structures that count documents matching each facet value
- Intersection logic that combines multiple active filters using AND/OR operations
When a user applies filters, the system:
- Uses inverted indexes to find document sets matching each filter
- Intersects these sets (e.g., "blue" AND "medium" AND "50")
- Recomputes facet counts for remaining documents to update the UI
Why Aggregations Matter
Without pre-computed aggregations, you'd need to scan all results to count "how many items are blue?" Aggregations turn this O(n) scan into an O(1) lookup by maintaining counters as documents are indexed.
Systems like Elasticsearch store facet counts in field data caches or doc values—column-oriented structures optimized for this exact workload.
The Multi-Dimensional Challenge
The hard part is handling combinations: when three filters are active, facet counts must reflect documents matching those three constraints, not the full corpus. This requires dynamic aggregation at query time, often using bitmaps or posting list intersections.
Key Takeaway:
Faceted search relies on pre-computed field aggregations and efficient set intersection to provide instant, multi-dimensional filtering with accurate result counts.