You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
[BUG] Postgres text search: ts_rank of multi-word prefix queries collapses when a note contains a longer word starting with a query term other than the alphabetically last (e.g. project-tracker) #1981
On the Postgres backend, a multi-word text query becomes a prefix AND query: project status is sent as project:* & status:* (prepare_single_term in src/basic_memory/repository/postgres_search_query.py). The score is the greatest ts_rank over the document vector, the row vector and the chunk vectors.
PostgreSQL's ts_rank scores a multi-term AND query in calc_rank_and (src/backend/utils/adt/tsrank.c). For a prefix operand it loops over every matching lexeme and overwrites pos[i] on each pass. The operands are sorted first, and each later operand is compared only against the positions of each earlier operand's last matching lexeme.
So when a note holds any longer word that starts with a query term other than the last one in sorted order, only the matching word that sorts last counts for that term. In the example below that term is project, which sorts before status. Typical such words:
a kebab-case name (project-tracker; the parser indexes the whole hyphenated word as well as its parts)
a file name (project-status-monthly-report.md)
The note's proximity score then collapses, even though the query words stand side by side many times. Per the code, a word that extends only the last term does not trigger it (not tested).
In hybrid mode, FTS scores are normalized by the per-query maximum and fused as max(vec, fts) + 0.3 * min(vec, fts) (fuse() in src/basic_memory/repository/search_reader.py; the formula is also in docs/semantic-search.md). The top FTS hit therefore scores at least 1.0 in hybrid mode. A note at about a quarter of the top FTS score, as M and N are against B below, can pass it only with a vector score above 0.926, even when the top hit has no vector match.
Steps To Reproduce
In a Basic Memory Cloud project, create three notes in one folder. Neither the titles nor the folder name may contain the query words.
B: Project status first. Then project status second. Project status third. And project status fourth.
M: the text of B, plus the line Old file: project-status-monthly-report.md
N: the text of B, plus the line See the project-tracker skill.
Run bm_search(query="project status", mode="text", path="<folder>").
Expected Behavior
M and N score about the same as B. They hold the same four close pairs plus one extra line.
Actual Behavior
Results of 30.10.2026:
Note
Score
B
0.751
M
0.185
N
0.185
N's extra word does not even contain the second query term. The score matches what calc_rank_and gives for status at positions 2, 6, 9 and 13 against a single project… position at 17 (distances 4, 8, 11 and 15).
Environment
Basic Memory Cloud (hosted, Postgres backend), used through the Claude connector
Basic Memory version: hosted Cloud on 30.10.2026 (the deployed version is not visible); source read at mainae3599c
PostgreSQL version: not visible. The same calc_rank_and loop is in REL_14_STABLE, REL_17_STABLE and master.
OS, Python, installation method: not applicable (hosted)
Additional Context
In a real project of about 240 notes this decides which note comes first. The note about a subject often names its own tools and files in kebab case, so it can lose first place to notes that only mention the subject.
Rewriting such words as plain words lifted single notes to first place for their own subject. Overall accuracy did not rise (93 of 110 test queries right first afterwards, 94 before), apparently because competing notes gained as much.
Possible Solution
Not tested here:
Score with both the exact query and the prefix query and take the greater, e.g. GREATEST(ts_rank(v, to_tsquery('english', 'project & status')), ts_rank(v, to_tsquery('english', 'project:* & status:*'))). Matching can stay prefix-based.
Or rank with ts_rank_cd. Its get_docrep collects the positions of every lexeme that matches a prefix operand.
Activity
fix(core): rank Postgres prefix queries by the words typed; score titles