Command Palette

Search for a command to run...

Hectal
Phase 3: Vector Search: Exact, IVF, HNSW and Smaller Vectors
RAG and Vector Search · Exercise 6 of 6 Previous
Intermediate

Filtering after the search

Write fetch_needed(k, matching, total) that returns how many results to fetch before filtering so that about k of them pass, rounded up: ceil(k * total / matching) with integer arithmetic only.

ranked is the list of all ids, closest first, and allowed is the set of ids that pass the filter. Write post_filter(ranked, allowed, k, fetch) that looks only at the first fetch ids of ranked and returns the first k of them that are in allowed, in order. It may return fewer than k.

What Check tests

  1. 5 percent selectivity needs a fetch of 100 for k = 5
  2. The fetch rounds up
  3. A filter that passes everything needs only k
  4. A small fetch can return too few
  5. A bigger fetch finds k
  6. At most k results, in ranked order

Stuck? One hint at a time

The solution unlocks after your first Check. Try first: that is where the learning happens.

main.py
Sign in to run your code.

Output

Press Run to see what your program prints.