Filtering after and before the search
The search itself is exact here, to keep the effect of the filter clear. With an approximate index the post-filter result can only be worse than shown.
Real searches say "only this user's documents" or "only the 2025 version". Filtering after an approximate search leaves too few results when the filter is selective, filtering before it can be slow or break a graph, and the practical answer depends on how many vectors pass the filter.
Change the code and press Run (Ctrl+Enter). Predict what will happen first, then break it on purpose and read what changes. Your edits are saved and match the lesson page.
Ready for more? This phase has 6 hands-on exercises with automatic checks.
Practice questions
Write the code in the editor, run it, then open the model answer to compare.
Write fetch_needed(k, matching, total) that returns how many results to fetch before filtering so that about k pass, rounded up (use integer arithmetic). Print it for k = 5, total = 1,000 and matching = 500, 50 and 5.
Write choose_strategy(k, matching, total): if matching is at most 100, return "scan the N allowed vectors exactly"; otherwise return "use the index, fetch F, then filter" where F is twice fetch_needed. Print the choice for matching = 5, 50 and 500 out of 1,000 with k = 5.
Explain it without notes
Why does filtering after an approximate search return too few results, and how can you estimate how many to fetch?
What is pre-filtering, and why is it easy with a small allowed set but hard inside a graph index?
A team has one shared index for 5,000 tenants and tests only with their largest tenant. What goes wrong?
Why must access filters be applied before the prompt is built?
Expected output
filter keeps 50 of 1000 documents
filter after the search, fetch 5: 0.12 results on average, full 5 for 0 of 50 queries
filter after the search, fetch 20: 1.06 results on average, full 5 for 0 of 50 queries
filter after the search, fetch 100: 4.24 results on average, full 5 for 29 of 50 queries
filter after the search, fetch 500: 5.00 results on average, full 5 for 50 of 50 queries
filter before the search (50 distances): 5.00 results on average, 0 from the wrong user
filter keeps 5 of 1000 documents
filter after the search, fetch 5: 0.04 results on average, full 5 for 0 of 50 queries
filter after the search, fetch 20: 0.10 results on average, full 5 for 0 of 50 queries
filter after the search, fetch 100: 0.62 results on average, full 5 for 0 of 50 queries
filter after the search, fetch 500: 2.78 results on average, full 5 for 5 of 50 queries
filter before the search (5 distances): 5.00 results on average, 0 from the wrong user