Skip to content
Blog

Filtered Vector Search and the Pre-Filter vs Post-Filter Problem

Why metadata filters break naive vector search, the difference between pre-filtering and post-filtering, and how databases handle both.

Published on • November 22, 2026

AI Assistant

Introduction

Real vector search is rarely “nearest neighbour over everything.” You need the nearest neighbours within a tenant, a date range, a product category. Adding that filter looks trivial — until you discover that filters and ANN indexes interact badly. The pre-filter vs post-filter debate is one of the core design problems in modern vector databases.

Why Filters Break ANN

ANN indexes are built over the whole corpus: HNSW’s graph edges, IVF’s clusters, PQ’s codes all assume unfiltered data. When a query says “only consider vectors where tenant_id = X”:

  • The graph path may be dominated by filtered-out nodes — the search walks through neighbours it cannot return, wasting hops and possibly never reaching the few allowed ones.
  • The nearest allowed vector may be far away in the index — a selective filter (0.1% of data) can hide true matches several graph hops outside the explored region.

The two classic strategies each fail in one direction:

Post-Filtering (filter after search)

Run ANN normally, fetch top-k (or a larger candidate set), then drop items failing the filter.

  • ✅ Simple; index untouched; fast when the filter is loose
  • ❌ Returns fewer than k (or zero) results when the filter is selective — you searched the wrong region
  • ❌ Recall collapses: the true top-k within the filter may never appear in the candidate set
  • Common mitigation: fetch k × overfetch (e.g., 10k) and hope — fails hard at high selectivity

Restrict the search to the allowed ID set first, then run ANN within it.

  • ✅ Conceptually correct: search exactly the eligible subset
  • ❌ Hard to implement efficiently — HNSW/IVF have no native “skip these nodes” operation; naively masking the graph degrades traversal
  • ✅ Modern engines approximate it well: filtered HNSW variants (e.g., ACORN-style, or per-filter sub-indexes) keep traversal on valid nodes

What Production Engines Do

  • Segmented/partitioned indexes — one index per filter value (tenant, category): exact and fast, but cardinality explodes for high-cardinality filters
  • Filtered graph traversal — skip invalid neighbours during the walk, expanding the search until k valid results are found
  • Hybrid execution — for very selective filters, fall back to scanning only the filtered subset (it’s small!) — “scan the filter, not the vector index”
  • Overfetch + rescore as the cheap default

The AWS guidance on vector databases for RAG emphasises that filter support and its performance characteristics are a first-class selection criterion — the same index that flies unfiltered can crawl under selective filters.

Practical Guidance

  1. Measure with your filter selectivity — 90%-selective and 0.1%-selective filters are completely different workloads.
  2. Prefer engines with native filtered-ANN if selective filters are core to your product.
  3. Partition by low-cardinality fields (tenant, language) when possible — it turns filters into index choice.
  4. Watch recall@k under filter — unfiltered benchmarks hide this failure entirely.
  5. Consider hybrid fallback: selective filter → filtered scan; loose filter → ANN + post-filter with overfetch.

Conclusion

Filtering is where vector search stops being a pure similarity problem. Post-filtering is easy but starves under selective filters; pre-filtering is correct but fights the index design. Modern engines blend both with partitioning, filtered traversal, and fallback scans — and your job is to test the selectivity your product actually uses.

References