Skip to content

[FEATURE] Let hybrid relaxed FTS retry OR user-delimited script phrases when strict AND returns 0 results #2183

Backlog

Description

@Flynn-Kawolsky

Summary

Follow-up to #1533 and #1548 — thank you for adding portable script_ngrams support.

In both backends — SQLite (src/basic_memory/repository/sqlite_search_query.py:592-629) and Postgres (src/basic_memory/repository/postgres_search_query.py:570-578, 976-1012) — when allow_relaxed=True (used by hybrid search) retries a zero-result strict query, it relaxes only the word-query channel (:text / word_text), leaving script_text (script_text_* on Postgres) strict AND and gating the retry on params.get("text"):

# sqlite_search_query.py:592-629
relaxed_search_text = search_text
if search_text and "script_text" in params:
    relaxed_search_text = analyze_script_query(search_text.strip()).word_text
...
relaxed = relaxed_fts_text(relaxed_search_text) if allow_relaxed and not rows else None
if relaxed and params.get("text"):

Because word_text is None for an all-CJK query, a user-spaced query such as "顯示器 邊緣 磁感測" (3 explicit space-delimited CJK phrases) never triggers the relaxed OR retry when strict AND returns 0 rows. Likewise, in a mixed query ("OpenAI 顯示器 磁感測"), relaxing only word_text still requires every CJK phrase to appear in the same note.

Why It Matters

In a 1,126-query retrieval eval on a ~574-note CJK/English vault (measured on basic-memory 0.24.1 with script_ngrams enabled; the exact same word-only relaxation logic remains in 1.0.0 and main 9d13a666), the FTS leg of hybrid search returned 0 candidates on 1,078 / 1,126 queries (95.7%), forcing hybrid search to rely almost entirely on vector recall even when 2 of 3 space-separated CJK keywords matched verbatim.

Proposal

When allow_relaxed=True and strict FTS returns 0 rows, if len(script_query.gram_phrases) >= 2, allow the relaxed retry to OR the user-delimited script phrases (while keeping each phrase's own bigrams ordered and adjacent).

This respects explicit user whitespace boundaries and invents no word segmentation, unlike the overlapping-bigram OR that was rightly declined in #1533.

Happy to share a minimal synthetic reproduction or help test a branch on Windows.

Activity

  1. added
    enhancement
    on Nov 30, 2026
  2. mikemikimike commented on Dec 1, 2026

    @mikemikimike
    Contributor

    Implemented and updated in PR #2194: #2194

    • Search fallback now uses the shared eligibility rule and groups script terms by whitespace-delimited query token, preserving punctuation-separated runs as AND while deduplicating groups.
    • PostgreSQL no longer repeats an invalid word-query retry when only the script fallback is available.
    • SQLite and PostgreSQL retry telemetry now reports script-only fallback accurately.
    • Added regression coverage for eligibility, token grouping, duplicate and decoy matches, PostgreSQL retry behavior, and telemetry.

    Verification:

    • Focused search and retry tests: 40 passed.
    • Expanded search and script regression suite: 183 passed, 2 skipped.
    • Full-suite differences were reproduced and classified on the unmodified base; no failure was in the changed search paths.
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

    enhancement

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions