Problem
Memory recall uses LIKE '%term%' queries against memory_documents.title and memory_documents.markdown_body. This has several issues:
- No index usage — leading wildcard forces full table scan on every recall query
- Substring matching — matches within words, not word boundaries
- No relevance ranking — current scoring counts LIKE hits, not true text relevance
- Scales poorly — linear scan gets worse as document count grows
Proposed solution
Use SQLite FTS5 (built-in full-text search) for memory recall queries.
Implementation
- Create FTS5 virtual table:
CREATE VIRTUAL TABLE memory_fts USING fts5(
document_id UNINDEXED,
title,
content,
aliases,
facets
);
-
Keep in sync with memory_documents — insert/update FTS rows in ApplyInlineCurationBatchAsync and UpsertDocumentAsync
-
Replace LIKE queries in SearchByPlanInternalAsync with:
SELECT document_id, rank FROM memory_fts
WHERE memory_fts MATCH $query
ORDER BY rank
LIMIT $limit;
Then join with memory_documents for policy filtering (boundary, audience, sensitivity, expiry).
Benefits
- BM25 relevance scoring out of the box
- Word-level tokenization — proper term matching
- Index-backed queries — fast even with large document counts
- Boolean operators — AND, OR, NOT, prefix queries
- Phrase matching —
"health insurance" as exact phrase
Migration
- Add FTS table in schema migration
- Backfill existing documents into FTS table
- Dual-write on all memory mutations
- Switch recall coordinator to use FTS MATCH instead of LIKE
Related
Problem
Memory recall uses
LIKE '%term%'queries againstmemory_documents.titleandmemory_documents.markdown_body. This has several issues:Proposed solution
Use SQLite FTS5 (built-in full-text search) for memory recall queries.
Implementation
Keep in sync with
memory_documents— insert/update FTS rows inApplyInlineCurationBatchAsyncandUpsertDocumentAsyncReplace LIKE queries in
SearchByPlanInternalAsyncwith:Then join with
memory_documentsfor policy filtering (boundary, audience, sensitivity, expiry).Benefits
"health insurance"as exact phraseMigration
Related