Files
Enterprise-Knowledge-Base/migrations/versioned
toy0116 3fee3a454d feat(migrations): HNSW index for 1024-dim embeddings (bge-m3)
Upstream's 000002 only adds HNSW partial indexes for dim=3584 (OpenAI
text-embedding-3-large) and dim=798. Any deployment binding bge-m3 (the
default local embedder · 1024 dim · what localhub-embedder ships) falls
back to a sequential scan of the entire embeddings table on every vector
query. Functionally correct, but ~100-500ms per query depending on KB
size · dominates p50 latency on chat/agent paths.

Add a partial HNSW index targeting dim=1024 with the same expression and
opclass as the other two (embedding::halfvec(1024) halfvec_cosine_ops).
Matches the SearchEmbedding query in repository.go verbatim so the
planner picks it up automatically.

Local measurement on a 63k-row embeddings table (Robustel_Support KB):
  before · seq scan: ~100-500ms per vector query (varies)
  after  · index scan: ~28-30ms

CONCURRENTLY is skipped because plpgsql DO blocks open an implicit
transaction and CREATE INDEX CONCURRENTLY cannot run inside one. On
fresh installs the embeddings table is empty so this is instant. For
production backfill the operator should build the index manually using
CONCURRENTLY first; the IF NOT EXISTS guard then makes this migration a
no-op.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-06-08 12:18:26 +08:00
..