Post-#775: collection-scoped BM25 still truncates global FTS results before collection filtering
Summary
searchFTS still runs a global documents_fts MATCH with LIMIT limit*10, then drops rows whose collection doesn't match. Vector search got an exact per-collection scan for scopes up to 20,000 vector rows (#791 / #803). BM25 did not.
#775 only searches-then-merges when more than one name is passed. Each recursive call is still that global+filter path, so an unrequested collection can empty both -c target and -c target -c other. includeByDefault: false does not isolate you: exclusion is a query-time name list; those rows stay in documents_fts and still fill the window.
*10 over-fetch is not a fix — it already false-empties at 50 / 200 / 500 stronger foreign hits (store / qmd query+MCP / default qmd search). Leftover of #775 / #871; not asking to reopen.
Reproduction
Store-level, no LLM, dbfd0b4 (@tobilu/qmd 2.8.3):
noise: 50 docs titledalpha alpha(never in the requested scope)target: 1 doc with a weaker mention ofalphaother: exists, noalphamatch
searchFTS("alpha", 5, "target") → []
searchFTS("alpha", 5, ["target", "other"]) → []
# 49 noise docs: both calls find target
# searchFTS("alpha", 500, "target"): finds it["target", "noise"] is the wrong demo. With noise in scope, five noise docs is correct top-5 even after a fix.
The existing #775 test uses 12 noise docs and limit=3 (window 30), so post-filter still sees the hits. There is no scoped FTS test with noiseCount >= limit * 10.
Default qmd search (no -c) resolves includeByDefault into that same name list. Unscoped searchFTS("alpha", 5) correctly returns noise. searchFTS("alpha", 5, ["target", "other"]) is the default-scope path and returns [] on this fixture at the store's window — the CLI itself passes a larger limit, so a real default qmd search needs ≥ 500 (see the window table).
Why
// src/store.ts — searchFTS
if (names && names.length > 1) {
return mergeSearchResultsByScore(names.map(name => searchFTS(db, query, limit, name)), limit);
}
const collectionFilter = names?.[0];
const ftsLimit = collectionFilter ? limit * 10 : limit;
// CTE: global MATCH ORDER BY bm25 LIMIT ftsLimit
// then JOIN documents AND d.collection = ?The CTE exists so SQLite doesn't drop the FTS5 index (MATCH + collection in one WHERE was 8ms → 17s). That's why LIMIT happens before the collection predicate.
hybridQuery already claims the guarantee:
// Pass collection directly into FTS query (filter at SQL level, not post-hoc)
const initialFts = store.searchFTS(query, 20, collection);The filter is in SQL, after the CTE LIMIT.
Windows (collection-scoped ftsLimit = limit * 10):
| Path | searchFTS limit |
Window | Empty at |
|---|---|---|---|
| This repro | 5 | 50 | 50 |
qmd query / MCP |
20 (hardcoded) | 200 | 200 |
qmd search -n 5 |
max(50, 2·n) → 50 |
500 | 500 |
If vector still finds the doc, the FTS ranked list is missing, so RRF is worse. qmd search is empty at its window (500); lex-only structured queries are empty at 200.
Suggested fix
Don't LIMIT the FTS CTE independently of collection. Recursing for names.length === 1 is the same inner path.
Recommended: exact per-collection FTS scan, same idea as searchVec (#791 / #803) — score only that collection's rowids, then LIMIT. Keep FTS-first; check EXPLAIN QUERY PLAN on the candidate SQL (the 17s case was MATCH + collection in one planner unit). Constraining MATCH on filepath: is not a collection key under porter unicode61 (target also matches target-noise / targets).
Regression: 50 noise vs limit=5, noise out of scope; assert a target hit for "target" and for ["target", "other"]. Keep the existing #775 test.
Related
- #775 (closed via #871) — multi-collection union / global top-k
- #791, #803 (closed) — vector exact-scan
- #217, #233 (closed) — earlier multi-collection empty results
Source: tobi/qmd