Skip to content

feat(memory): migrate recall search from LIKE to SQLite FTS5 full-text search #412

@Aaronontheweb

Description

@Aaronontheweb

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

  1. Create FTS5 virtual table:
CREATE VIRTUAL TABLE memory_fts USING fts5(
  document_id UNINDEXED,
  title,
  content,
  aliases,
  facets
);
  1. Keep in sync with memory_documents — insert/update FTS rows in ApplyInlineCurationBatchAsync and UpsertDocumentAsync

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    memoryMemory formation, recall, curation pipeline

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions