Spot the bug: a tenant-scoped RAG query keeps returning fewer than k results
Same topic, related formats. Practice these next.
Same topic, related formats. Practice these next.
Click any words you think contain an error. Click again to unmark.
The code fetches the global top-k then filters by tenant, so most hits are dropped and the caller gets fewer than k; push the tenant predicate into the search as a pre-filter.
Imagine asking a librarian for the five books most relevant to your question, but you only have a library card for the children's section. The librarian grabs the five best books from the entire building, then checks your card and hands back only the ones from the children's section — maybe one, maybe none. You're left short, even though the children's section is full of great books that would have answered you. The smarter move is to tell the librarian up front: 'only search the children's section.' Then every one of the five they pick is one you're allowed to read. This code makes the librarian's first mistake. It picks the best matches from everyone's data, then throws away the ones that don't belong to your tenant, instead of restricting the search to your tenant from the start.
Everything you need to truly understand this topic: intuition, mechanics, step by step explanation, code, formulas, and worked example. Click to expand.
Everything you need to truly understand this topic: intuition, mechanics, step by step explanation, code, formulas, and worked example.
Everything important, quickly.
3-4 min: why post-filtering caps a small tenant below k, the operation-order walkthrough, the pre-filter fix, and the HNSW/namespace subtleties behind 'just add a filter'.
Real products, models, and research that use this idea.
What an interviewer would ask next. Try answering before peeking at the approach.
Red flags and common mistakes that signal junior thinking. Click to expand.
Filtering metadata after the top-k search instead of inside it; the ranking is decided over the whole corpus, so a small tenant loses most of its k slots before the filter even runs.
The night-before-the-interview bullets. Scan these on the way to the call.
Primary sources. Skim if you want the original framing.