Command Palette

Search for a command to run...

Hectal
Back to the lesson: Topic 5.5 — Filtering in a Vector Store
RAG and Vector Search · Example 1 of 1

Filter after, filter first and keep widening

The vectors are random numbers from a small fixed generator, so the example is the same in every language. 'Candidates' stands for what an approximate index collects before the filter.

A filter such as "only this user's documents" sounds simple, but an approximate index may collect its nearest candidates first and filter afterwards, leaving too few results. Stores fix it by filtering inside the search, by searching again with a wider net, or by splitting the data.

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 5 hands-on exercises with automatic checks.

Practice questions

Write the code in the editor, run it, then open the model answer to compare.

01

Write filter_after(items, query, candidates, wanted, k) where items is a list of (id, score, owner) already sorted best first. Return up to k ids from the first candidates items whose owner is wanted. Print the result for the list given in the answer.

02

Write widen(items, wanted, k, start) that doubles the number of candidates from start until k results are found or all items were looked at. Return (ids, candidates_used).

Explain it without notes

01

Why can a filter plus an approximate index return fewer than k results?

02

Compare pre-filtering and post-filtering.

03

What are three ways to fix short filtered results?

04

Why is the access filter applied on the server?

Filter after, filter first and keep widening
Sign in to run this example in your browser.

Expected output

filter first (10 vectors compared): 107,147,127,187,87
filter after, 20 candidates: 2 of 5 found: 107,147
filter after, 40 candidates: 4 of 5 found: 107,147,127,187
keep widening from 20: 5 of 5 found after looking at 80 candidates
same answer as filter first: yes