Repository navigation
perf(core): rank only filter-admitted chunks inside sqlite-vec and pgvector - #1753
Merged
Merged
Conversation
…vector A filtered vector search ranked every chunk by similarity, then asked an FTS pass which candidates the filters admit, and widened its window when too few survived (#1746). The built-in indexes share the SQL database with the search rows, so they can rank only the admitted chunks. - SemanticVectorIndex.search takes a VectorAdmission: a SQL predicate over the chunk manifest. Row types become `c.source_type IN (...)`; every other filter is the SQL full-text search already applies, as a subquery over search rows (FtsBackend.row_filter), with its own binds. - sqlite-vec evaluates it as `rowid IN (...)` inside its vec0 KNN, which stays exhaustive and exact: 17.5 ms vs 8.3 ms unfiltered at 50,000 vectors. - pgvector counts the admitted chunks up to 50,000. Few: an exact ranking of just those chunks (47 ms for 30,000 of 300,000). Many: the filtered HNSW iterative scan, forced with a transaction-local enable_sort=off because the planner underestimates the filter and otherwise chose the exact plan at 90% admitted (600 ms vs 126 ms). The HNSW scan recalled 66% of the true top 100 at 10% admitted, hence the exact branch. - The reader does one read for built-in indexes: no widening loop and no filter pass. Milvus keeps the filter-and-widen path and refuses an admission. - Migration 0041 (and the SQLite/bootstrap DDL) indexes chunks by (project_id, source_type, source_row_id) for the admission join. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01EQPZkbnuxxtTEJoNJCkt45 Signed-off-by: phernandez <paul@basicmachines.co>
Codex Review SummaryThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 754f02bb28
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
8 of 9 tasks
phernandez
added a commit
that referenced
this pull request
Oct 11, 2026
#1753 ended the window loop after the first read whenever the index had evaluated the filters, on the grounds that every chunk was admitted. But admitted chunks still collapse to rows: with ~52 chunks per note, a 100-chunk read holds two notes, so a default (entity-filtered) MCP search for ten results could return one or two while more admitted notes sat just past the window. Filtered reads now widen to the row target as they did before, each read still carrying the admission and skipping the reader-side filter pass. The rerank prefix bound extends for any filtered window again, since a widened built-in window can also admit its leading rows past the fixed bound. Also accept the admission argument in the semantic coverage test's _run_vector_query recorder. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01EQPZkbnuxxtTEJoNJCkt45 Signed-off-by: phernandez <paul@basicmachines.co>
3 tasks done
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Part 2 of the #1746 search rework, stacked on #1750. The vector leg now evaluates structured filters inside the vector index, so a filtered search reads the exact top-k of the admitted chunks in one query. There is no widening loop and no post-filter pass for the built-in indexes.
Before this PR, a filtered vector search ranked every chunk by similarity, asked an FTS pass which candidates the filters admit, and widened its window when too few survived. Every default MCP search is filtered (
entity_types=["entity"]).How filters reach the index
SemanticVectorIndex.searchtakes aVectorAdmission: a SQL predicate over the chunk manifestcwith its own bind names.c.source_type IN (...), because chunks already carry their row type, so the default entity filter needs no subquery.FtsBackend.row_filteras a subquery over search rows, joined on(c.project_id, c.source_type, c.source_row_id).sqlite-vec
The admission goes into
vec0's KNN asrowid IN (...), together with the ready-manifest check.vec0is exhaustive, so the filtered top-k is exact. Measured at 50,000 vectors: 17.5 ms filtered vs 8.3 ms unfiltered. Computing distances in plain SQL instead was 60× slower and was not used.pgvector
No single plan fits every filter. Measured on 300,000 clustered 384-dimension chunks:
The adapter therefore counts the admitted chunks, stopping at 50,000:
ORDER BY distance + 0keeps the HNSW index from serving the sort.enable_sort = off. The planner underestimates the filter subquery (10 rows estimated, 1,800 actual), and at 90% admitted it otherwise chose the exact plan (853 ms).Unfiltered searches run the same statement as before, byte for byte.
Other changes
0041: indexessearch_vector_chunks (project_id, source_type, source_row_id)for the admission join. Without it, every filtered Postgres search scanned the whole chunk table. The SQLite runtime DDL and the Postgres bootstrap DDL create the index too.Measured against
mainon a real projectThe parity harness ran 138 searches against a copy of a real local project (485 notes, 5,671 chunks): hybrid, vector and Boolean text, with entity-type, note-type, tag and category filters, pages 1 to 3.
has_moreare identical tomain. That includes the category-filtered search that perf(core): stop runaway vector widening and cap note text in search reads #1750's window rule had changed: the exact filtered top-k restoresmain's ranking.matched_chunkexcerpts. Excerpts still join the best chunks the window read. fix: fix permalink uniqueness violations on create/update/sync #3 in this stack ranks each returned note's own chunks instead.Local latency (same project, in process)
Median and p95 of each search's median over 3 warm runs, for the same 138 searches. Times include CLI overhead.
mainTest plan
row_filterand handed to the index; the external path still intersects.prefixed_binds, Milvus refusing an admission.just fast-checkjust test-unit-sqlite: 8238 passedjust test-int-sqlite: 701 passed🤖 Generated with Claude Code
https://claude.ai/code/session_01EQPZkbnuxxtTEJoNJCkt45