Skip to content

perf(core): rank only filter-admitted chunks inside sqlite-vec and pgvector - #1753

Merged
phernandez merged 1 commit into
perf/1746-search-stopgapfrom
perf/1746-search-rank-by-id
Oct 11, 2026
Merged

phernandez merged 1 commit into
perf/1746-search-stopgapfrom
perf/1746-search-rank-by-id

Conversation

@phernandez

@phernandez phernandez commented Oct 11, 2026 •

Copy link
Copy Markdown
Member

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.search takes a VectorAdmission: a SQL predicate over the chunk manifest c with its own bind names.
  • Row types become c.source_type IN (...), because chunks already carry their row type, so the default entity filter needs no subquery.
  • Every other filter (note type, folder, dates, metadata, valid time, categories) is the same SQL full-text search applies. It comes from the new FtsBackend.row_filter as 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 as rowid IN (...), together with the ready-manifest check. vec0 is 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:

Case Exact ranking of admitted chunks Filtered HNSW scan
Unfiltered 144 ms 94 ms, recall 0.99
Folder filter, 10% admitted 47 ms, recall 1.00 180 ms, recall 0.66
Folder filter, 90% admitted 599 ms 126 ms, recall 0.99

The adapter therefore counts the admitted chunks, stopping at 50,000:

  • 50,000 or fewer: an exact ranking of just those chunks. ORDER BY distance + 0 keeps the HNSW index from serving the sort.
  • More: the filtered HNSW iterative scan, forced with a transaction-local 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

  • Reader: built-in indexes do one admitted read. Milvus cannot evaluate SQL, so it keeps the filter-and-widen path and refuses an admission.
  • Migration 0041: indexes search_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 main on a real project

The 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.

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.

Searches main #1750 this PR
All 138 440 / 1,352 ms 216 / 526 ms 188 / 272 ms
Hybrid, category filter 423 / 1,352 ms 427 / 617 ms 102 / 224 ms
Vector, tag filter 581 / 1,666 ms 280 / 417 ms 108 / 115 ms
Hybrid, note-type filter 477 / 1,547 ms 232 / 311 ms 215 / 228 ms

Test plan

  • Real sqlite-vec: 40 rejected rows nearer the query than 3 admitted ones. One index call, admitted chunks only, nearest first.
  • Real pgvector, both branches (exact ranking; threshold forced to 0 for the filtered HNSW scan): only the admitted notes come back, from one call.
  • The folder-scope test runs on a built-in and an external index. The built-in path evaluates the generated admission SQL against real search rows.
  • Valid-time filter: forwarded unchanged to row_filter and handed to the index; the external path still intersects.
  • Unit: admission composition (type-only predicate, subquery and binds), prefixed_binds, Milvus refusing an admission.
  • just fast-check
  • just test-unit-sqlite: 8238 passed
  • just test-int-sqlite: 701 passed
  • Postgres (testcontainers): repository, search routers, search service, migration graph
  • Full Postgres suite (CI)

🤖 Generated with Claude Code

https://claude.ai/code/session_01EQPZkbnuxxtTEJoNJCkt45

…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>
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 11, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-10-11T20:52:38.785532Z 754f02b PR opened
ℹ️ 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" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 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".

@phernandez
phernandez added this pull request to stack #1756 October 11, 2026 22:03
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>
@phernandez
phernandez merged commit 9f26e5e into main Oct 11, 2026
37 of 38 checks passed
@phernandez
phernandez deleted the perf/1746-search-rank-by-id branch October 11, 2026 22:32
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant