Skip to content

[BUG] SQLite script_ngrams double-indexes content_snippet (significant FTS bloat), and Boolean/quoted CJK queries bypass script_ngrams #2155

Backlog

Description

@Flynn-Kawolsky

Summary

Two issues in the script n-gram indexing/query pipeline (script_ngrams) significantly affect CJK-heavy SQLite vaults:

  1. Double-indexing bloat on SQLite: SearchRepositoryBase.index_item() (src/basic_memory/repository/search_repository_base.py:982-986), SearchRepositoryBase.bulk_index_items() (:1035-1039), and accepted_note_search_insert_params() (src/basic_memory/repository/accepted_note_search_repository.py:137-145 when include_full_content_grams=True) pass all three of (title, content_stems, content_snippet) to build_script_ngrams().

    • Because SearchService.index_entity_markdown() and accepted_note_content_stems() already embed content inside content_stems (up to 6,000 chars) and set content_snippet = _strip_nul(content), every CJK/script character run in every note <= 6,000 chars and every observation is tokenized and stored twice via content_stems + content_snippet in script_ngrams.
    • For notes > 6,000 chars, the first ~6,000 chars are still indexed twice while only the remainder past 6,000 chars comes exclusively from content_snippet (needed for test_search_matches_script_text_beyond_the_parent_fts_limit).
    • On a ~580-note CJK-heavy SQLite vault (4,510 search_index rows: 593 entities, 2,087 observations, 1,830 relations), 2,652 of 2,680 entity/observation rows (99.0%) have content_snippet contained verbatim inside content_stems. script_ngrams currently totals 6.09M characters (vs 1.47M content_stems and 0.88M content_snippet). Deduplicating content_snippet against content_stems cuts entity script_ngrams by 40.5% (2.38M -> 1.42M chars; up to ~50% on notes <= 6,000 chars), observation script_ngrams by 21.5% (3.55M -> 2.78M chars; observation content_stems also repeats text via _generate_variants), and total script_ngrams by 28.3% (6.09M -> 4.37M chars, -1.73M chars) with 0 lost n-grams.
  2. Boolean and quoted CJK queries bypass script_ngrams completely: analyze_script_query() (src/basic_memory/repository/script_ngrams.py:125-134) short-circuits whenever " or AND / OR / NOT appears:

    if '"' in text or any(f" {operator} " in padded_text for operator in ("AND", "OR", "NOT")):
        return ScriptQuery(word_text=text, gram_phrases=())

    While the comment explains why mixed-script Boolean queries cannot naively split one operand into a second SQL channel, the side effect is that pure-CJK Boolean or quoted queries — e.g. 適者 OR 生存 or "適者生存" — fall back to SQLite's unicode61 tokenizer, where continuous CJK runs are not segmented into words and substring queries return 0 hits. This directly conflicts with standard FTS usage (bm_search docs and user workflows that combine terms with OR or quotes).

Proposed Directions

  1. For double-indexing (safe, self-contained fix): Keep build_script_ngrams(*texts) pure, and add a small call-site deduplication helper for SQLite's (title, content_stems, content_snippet) inputs:

    • If content_snippet is already a substring of content_stems, omit content_snippet.
    • If content_stems ends with a truncated prefix of content_snippet starting at a whitespace/start boundary (long notes > 6,000 chars), pass the non-overlapping metadata prefix of content_stems plus the full content_snippet, preserving contiguous runs past char 6,000 without ever splitting a script run mid-run.
    • If disjoint (as in test_search_matches_script_text_beyond_the_parent_fts_limit), keep both.
      I have a small PR with unit + repository tests ready for part 1.
  2. For Boolean/quoted CJK queries (for discussion): Consider compiling each CJK operand in a Boolean/quoted expression into its script_ngrams bigram-phrase match within the same FTS5/tsquery expression (e.g. 適者 OR 生存 -> (script_ngrams : "適者") OR (script_ngrams : "生存"), and "適者生存" -> script_ngrams : "適者 者生 生存" rather than falling back to unicode61). Happy to discuss design preferences before writing code for part 2.

Activity

  1. added
    bug
    on Nov 26, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bug

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions