Repository navigation
Description
Summary
Two issues in the script n-gram indexing/query pipeline (script_ngrams) significantly affect CJK-heavy SQLite vaults:
-
Double-indexing bloat on SQLite:
SearchRepositoryBase.index_item()(src/basic_memory/repository/search_repository_base.py:982-986),SearchRepositoryBase.bulk_index_items()(:1035-1039), andaccepted_note_search_insert_params()(src/basic_memory/repository/accepted_note_search_repository.py:137-145wheninclude_full_content_grams=True) pass all three of(title, content_stems, content_snippet)tobuild_script_ngrams().- Because
SearchService.index_entity_markdown()andaccepted_note_content_stems()already embedcontentinsidecontent_stems(up to 6,000 chars) and setcontent_snippet = _strip_nul(content), every CJK/script character run in every note <= 6,000 chars and every observation is tokenized and stored twice viacontent_stems+content_snippetinscript_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 fortest_search_matches_script_text_beyond_the_parent_fts_limit). - On a ~580-note CJK-heavy SQLite vault (4,510
search_indexrows: 593 entities, 2,087 observations, 1,830 relations), 2,652 of 2,680 entity/observation rows (99.0%) havecontent_snippetcontained verbatim insidecontent_stems.script_ngramscurrently totals 6.09M characters (vs 1.47Mcontent_stemsand 0.88Mcontent_snippet). Deduplicatingcontent_snippetagainstcontent_stemscuts entityscript_ngramsby 40.5% (2.38M -> 1.42M chars; up to ~50% on notes <= 6,000 chars), observationscript_ngramsby 21.5% (3.55M -> 2.78M chars; observationcontent_stemsalso repeats text via_generate_variants), and totalscript_ngramsby 28.3% (6.09M -> 4.37M chars, -1.73M chars) with 0 lost n-grams.
- Because
-
Boolean and quoted CJK queries bypass
script_ngramscompletely:analyze_script_query()(src/basic_memory/repository/script_ngrams.py:125-134) short-circuits whenever"orAND/OR/NOTappears: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'sunicode61tokenizer, where continuous CJK runs are not segmented into words and substring queries return 0 hits. This directly conflicts with standard FTS usage (bm_searchdocs and user workflows that combine terms withORor quotes).
Proposed Directions
-
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_snippetis already a substring ofcontent_stems, omitcontent_snippet. - If
content_stemsends with a truncated prefix ofcontent_snippetstarting at a whitespace/start boundary (long notes > 6,000 chars), pass the non-overlapping metadata prefix ofcontent_stemsplus the fullcontent_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.
- If
-
For Boolean/quoted CJK queries (for discussion): Consider compiling each CJK operand in a Boolean/quoted expression into its
script_ngramsbigram-phrase match within the same FTS5/tsquery expression (e.g.適者 OR 生存->(script_ngrams : "適者") OR (script_ngrams : "生存"), and"適者生存"->script_ngrams : "適者 者生 生存"rather than falling back tounicode61). Happy to discuss design preferences before writing code for part 2.
Activity