Filtered Vector Search

Filtered vector search returns the nearest neighbors of a query vector among only those items that satisfy a metadata condition, such as “the user may read this document”, “the language is German”, or “the item is published”. The condition is easy to state. The difficulty is that approximate nearest-neighbor indexes are built to answer “what is closest to this vector” and not “what is closest among the items that pass this predicate”, so the placement of the filter changes what comes back.

Post-filter and pre-filter

With a post-filter, the index first produces its candidate list, say the 40 nearest items, and the predicate then removes the candidates that fail it. What survives is returned, up to \( k \) items. With a pre-filter, the predicate is applied while the index is searched, so only items that pass can enter the result and the search can continue to seek \( k \) passing items. Approximate traversal and scan budgets can still stop it before \( k \) are found, even when enough matching items exist. Vector databases use different names, and some implement a hybrid, but the question to ask of any product is the same: is the predicate evaluated before the result list is cut to its final size, or after?

The post-filter problem is arithmetic. If the predicate passes a fraction \( s \) of the items and the candidate list has \( C \) entries, the expected number of survivors before the final \( k \)-item cap is about \( sC \) when passing the filter is independent of distance to the query. Correlation between metadata and vector distance changes the candidate pass rate. pgvector’s documentation gives the example of a condition matching 10% of rows with the default HNSW candidate list size of 40: about four rows match on average. Elasticsearch describes the same effect for a filter placed outside the knn clause, which can return far fewer than \( k \) hits or none at all even when the index holds plenty of matching items.

A hypothetical comparison

The hypothetical table below illustrates the filtering effect without modeling an approximate index. Consider 20,000 random unit vectors in 32 dimensions and 300 random queries. Take the exact top \( C \) neighbors as the “candidate list” of an index and apply a random filter that passes a fraction \( s \) of the items. The reference is the exact top 10 among the items that pass the filter. Recall is the share of those 10 that the post-filter returns. These illustrative values assume the random filter is independent of distance. Real approximate indexes add their own misses, which this comparison leaves out.

selectivity  C   mean_returned  recall@10_vs_exact_filtered  empty_queries
0.5          40      10.00                1.00               0
0.1          40       4.10                0.41               2
0.1          200     10.00                1.00               0
0.02         40       0.72                0.07             143
0.02         200      3.51                0.35               9
0.02         1000     9.98                1.00               0

The half-passing scenario in this table returns ten results per query; that does not guarantee the same outcome for every 50% filter. At 10% the 40-entry candidate list yields 4.1 results on average, which matches pgvector’s example. At 2%, 143 of 300 queries return nothing. Raising \( C \) restores the result, which shows that the right items exist and that the cut-off removed them. The cost of a larger \( C \) is more distance computations per query, and the needed \( C \) grows as the filter gets more selective, so a fixed value that works in testing fails for the narrowest user group in production.

Ways to get \( k \) results

  • Filter during the graph or list traversal. Elasticsearch applies a filter inside the knn clause this way. The price is that a very selective filter can make the search slower, because the traversal must visit more of the graph to find enough passing items. Elasticsearch falls back to brute-force scoring when few documents pass the filter in a segment.
  • Keep scanning until enough results pass. pgvector offers iterative index scans for HNSW and IVFFlat (hnsw.iterative_scan, ivfflat.iterative_scan), which continue through the index until enough rows survive the filter or a scan limit is reached, such as hnsw.max_scan_tuples or ivfflat.max_probes.
  • Narrow the index itself. Partial indexes for a fixed filter value, or partitioning by the filter column, make the filter a table selection rather than a result cut. This works when the filter values are few and stable, and it fails for per-user rules.
  • Widen the candidate list for post-filtering. It is simple and needs no special index, and the comparison above illustrates how the candidate budget grows as selectivity falls.

What a post-filter can leak

When the predicate is an access rule, placement also decides what an unauthorized user can observe. A post-filter that drops unreadable items before anything is shown returns no unreadable content. The leak appears when anything is computed from the unfiltered candidate list before the filter runs: a “related documents” list, a result count, a facet total, a rerank score, a cache entry keyed on the query. Each exposes something about items the user may not read, such as a title or the fact that a document exists. Safe post-filtering therefore requires that nothing is shown, counted, ranked, or cached from the unfiltered candidates. A pre-filter makes this structural, since unreadable items are never candidates. Neither placement helps when the access information in the index is stale. If a permission was revoked and the index copy has not caught up, both return the item, and only a check against the source of truth removes it.

References

Similar Posts

Questions, corrections, or additional insights?

This site uses Akismet to reduce spam. Learn how your comment data is processed.