advanced24 min studyLesson 656 in Engineering

Search the eligible population without losing its routes

Define filtered ground truth and compare postfiltering, traversal restrictions, and selective exact fallback.

On this page

What you’ll understand

  • Compute filtered top-k rather than unfiltered top-k with missing results.
  • Explain why filtering may remove navigation paths.
  • Use selectivity estimates only under explicit distribution assumptions.

A metadata predicate changes the population over which nearest neighbors are defined. Searching all documents and then dropping disallowed IDs does not generally solve that changed problem: the closest eligible document may never have entered the candidate list. Graph indexes add another complication because an ineligible document can still be a useful navigation waypoint. An engineering design must therefore separate which nodes may be traversed from which documents may be returned, while applying the correct visibility policy to every output. Evaluation needs exact ground truth for the eligible set, not for the unfiltered corpus.

The predicate changes the optimization domain

Let eligible IDs be those satisfying a fixed predicate at the query’s selected snapshot. The desired answer is the smallest-distance k items within that set under a defined tie order. If only one item is eligible, a request for ten cannot return ten valid unique documents. Declare how short eligible populations are represented and how recall is scored. Mixing those cases into a denominator of ten without explanation confuses corpus size with approximation failure.

Postfiltering first retrieves an unfiltered candidate set and then applies the predicate. It is simple, but a fixed oversampling factor provides no general completeness guarantee. If every nearby candidate fails the predicate, many farther eligible results may be missed. An exact scan over a very selective eligible subset can be both simpler and faster than exploring a large unfiltered graph, provided eligibility lookup and vector access are available and their costs are counted.

Ef={x∈D:f(x)=1},Gk,f(q)=topk⁡x∈Ef(−D(q,x))E_f=\{x\in\mathcal D:f(x)=1\},\qquad G_{k,f}(q)=\operatorname{topk}_{x\in E_f}\bigl(-D(q,x)\bigr)
D is a lower-is-better distance on finite vectors, f is a predicate fixed for the query snapshot, and topk uses a total tie order. Assume |Ef|≥k here; otherwise return fewer and define the evaluation denominator separately.↔ Scroll wide equations horizontally.

Do not cut the only path by accident

Imagine the only route from an entry point to an eligible document passes through an ineligible point. Removing that intermediate point from traversal disconnects the route, even though allowing traversal while suppressing its output would preserve it. Some filtered graph algorithms account for this distinction; others use specialized structures or fallback search. The supported behavior depends on the actual index and implementation. A generic label filter is not a proof that the induced eligible subgraph is navigable.

Faiss exposes ID selectors through supported per-query search parameters, and their interaction with scanning depends on the index and selector type. hnswlib 0.8.0 exposes a label-based query filter with documented Python performance considerations. Verify these concrete APIs rather than assuming a database-style metadata planner exists inside every ANN library. Keep predicate evaluation, accepted output IDs, and measured candidate effort visible so a performance optimization cannot silently change the eligible population.

E[Neligible∣c]=cs,Pr⁡(Neligible=0)=(1−s)c\mathbb E[N_{\rm eligible}\mid c]=cs,\qquad \Pr(N_{\rm eligible}=0)=(1-s)^c
These expressions require c candidate eligibility indicators to be independent Bernoulli variables with common probability s. c is a count and s is dimensionless. Real nearest-neighbor candidates often have correlated metadata; neither formula guarantees that c=k/s yields k eligible results.↔ Scroll wide equations horizontally.

Evaluate the filter and the index together

Construct ground truth by applying the predicate before exhaustive scoring, using the same document versions and metadata seen by the query. Include predicates with different selectivities and correlations with embedding geometry. A tenant, language, or publication-time filter may cluster strongly in the vector space, so a global selectivity average is a weak predictor of the current candidate stream. Measure completeness and latency separately for these slices, including empty eligible sets and boundary cases.

When the predicate expresses access visibility, validate returned IDs against the authoritative visibility snapshot before exposing document contents. This output condition does not imply that every internal graph node must be removed; that depends on the system’s isolation design. For ordinary categories, the same separation prevents stale metadata from producing wrong answers. Record whether filtering happened before candidate generation, during traversal or scanning, or after retrieval, because those choices have different recall and cost behavior.

Four unfiltered candidates still miss one eligible answer

For one query, documents in increasing exact distance are A:1, B:2, C:3, D:4, and E:5. Only C and E satisfy the predicate. Request k=2 and compare postfiltering the first four unfiltered candidates with exact filtered search.

  1. The unfiltered top four IDs are {A,B,C,D}. Applying the predicate removes A, B, and D, leaving only C. Increasing the unfiltered candidate count from two to four has found one eligible item but still has not included E.

  2. Applying the predicate first gives eligible set {C,E}, whose exact filtered top two are C and E in that order. Their distances remain 3 and 5; filtering changes membership rather than the distance formula.

  3. The postfiltered result intersects the filtered ground truth in one of its two IDs, so recall@2 is 1/2. An exhaustive scan of both eligible vectors returns two IDs and recall 1. A fifth unfiltered candidate would repair this particular example.

The result

A short postfiltered list is not evidence that the eligible population is too small. Here two eligible documents exist, but one lies beyond the chosen unfiltered candidate prefix.

Explore the model

Interactive lab

Interactive lab · probability

Oversampling is a probabilistic model, not a promise

Reset, set Success probability p to 0.5 and Trials n to 4. Predict mean 2 and probability of zero successes 1/16 before Step; playback highlights exact binomial masses. Reset, set p to 0.5 and n to 8, predict mean 4 and zero-success probability 1/256 before Step. This assumes independent equally likely eligibility and is not filtered ANN retrieval.

Oversampling is a probabilistic model, not a promiseThese are exact binomial probabilities for independent trials with one fixed success probability. Playback tours the distribution; it does not resample outcomes. Current readings: Mean · np: 5; Variance · np(1−p): 2.5; Total probability: 1. All parameters are adjustable with the labeled controls below.03581000.070.150.220.3Successes kP(X = k)k = 0 · P = 9.77e-4
Exact probabilityHighlighted outcomeMean np
Mean · np
5
Variance · np(1−p)
2.5
Total probability
1
t = 0 s

These are exact binomial probabilities for independent trials with one fixed success probability. Playback tours the distribution; it does not resample outcomes. Parameter changes pause playback and recompute the model at the current elapsed time. Reset restores both parameters and returns time to zero. Playback starts paused. Adjust either parameter, then choose Play or Step.

Look forA larger expected eligible count does not guarantee enough results, and correlated filters violate this toy model.

Check your understanding

Why can forbidding traversal through every ineligible graph node reduce filtered recall?

Try it yourself

Under an explicitly independent model with eligibility probability s=0.25, retrieve c=4 candidates. Compute the expected eligible count and the probability of receiving no eligible candidates. Does expectation one guarantee one result?

Take a moment to make a prediction or work it out on paper. Open a hint when you need one.

Give me a hint

Use cs and (1−s)^c, keeping the independence assumption visible.

Compare with the solution

The expected count is 4×0.25=1. The probability of zero is 0.75⁴=0.31640625, about 31.64%. Thus expectation one plainly does not guarantee even one eligible result. Real retrieval filters can be correlated with neighboring vectors, so these numerical probabilities describe the stated independent toy population rather than a general production prediction.

Take it with you

  • Filtered ground truth ranks within the eligible snapshot.
  • Postfiltering a candidate prefix can miss valid farther results.
  • Navigation paths and output eligibility require separate reasoning.

Read further

Original sources and deeper explanations to follow at your own pace.

Optional progress, saved in this browser when available. Revisit any lesson at any time.

Lesson not marked complete.