Pre-filter versus post-filter strategies for metadata-constrained ANN search — what breaks at each extreme?
Same topic, related formats. Practice these next.
Same topic, related formats. Practice these next.
Post-filter is cheap but can return fewer than K when the filter is tight. Pre-filter returns the right K but is expensive and can hurt ANN graph traversability.
Imagine you're searching for the 10 closest restaurants to your location that also accept vegan diners. **Post-filter:** ask the map for the 10 closest restaurants overall, then check each one for vegan options. Fast, but if only 1 of those 10 is vegan, you end up with one restaurant instead of ten, and the map doesn't tell you there are vegan restaurants slightly farther away. **Pre-filter:** first get the list of all vegan restaurants in the city, then find the 10 closest from that list. You always get 10. But if the list of vegan restaurants is huge or scattered, this step costs more. Modern vector databases pick the strategy automatically based on how selective the filter is: strict filter, pre-filter; loose filter, post-filter.
Everything you need to truly understand this topic: intuition, mechanics, step by step explanation, code, formulas, and worked example. Click to expand.
Everything you need to truly understand this topic: intuition, mechanics, step by step explanation, code, formulas, and worked example.
Everything important, quickly.
5 min: post-filter as ANN-then-drop with silent under delivery, pre-filter as predicate-then-ANN with cost and stranding failures, the single stage filter aware index as the modern resolution, selectivity based routing patterns, and how Qdrant / Weaviate / Pinecone implement these choices.
| Strategy | When ANN runs | Result-set size | Cost driver | Main failure mode |
|---|---|---|---|---|
| Post-filter | First, over full corpus | May be < K when filter is tight | Cheap; one ANN call | Silent under delivery on tight filters |
| Pre-filter (allowlist) | Second, over filtered subset | Always K when K matches exist | Expensive; two pass + index work | HNSW stranding; high cost for broad filters |
| Single-stage filter aware | Integrated with filter | K when K matches exist | Moderate; one filter aware pass | None at typical scales; needs vendor support |
| Selectivity-routed | Picked by selectivity | K when K matches exist | Path-dependent | Routing heuristic mis-estimates selectivity |
Real products, models, and research that use this idea.
What an interviewer would ask next. Try answering before peeking at the approach.
Red flags and common mistakes that signal junior thinking. Click to expand.
Believing pre-filter and post-filter return the same results in different order. They do not: post-filter can silently return fewer than K results, while pre-filter always returns K when matches exist (but pays a higher cost to do so).
The night-before-the-interview bullets. Scan these on the way to the call.
Primary sources. Skim if you want the original framing.