Skip to content

feat(search): FTS5 query syntax, date and relevance filters, snippets, read-only tg sql - #107

Merged
neoromantic merged 7 commits into
mainfrom
feat/search-query-filters-snippets
Oct 28, 2026
Merged

neoromantic merged 7 commits into
mainfrom
feat/search-query-filters-snippets

Conversation

@neoromantic

Copy link
Copy Markdown
Owner

Fixes #84
Fixes #85
Fixes #81
Fixes #88

Problem

  • tg messages search put every query in quotes, so FTS5 read it as one phrase. error OR timeout, sender:alice, hel* and квартир* found nothing. hello world matched only when the two words were side by side. A query FTS5 couldn't parse returned TELEGRAM_ERROR.
  • Search had no date range, no relevance order and no match count. total is the size of the page.
  • The search index is contentless, so tg sql couldn't read a match's text without a hand-written join. snippet() and highlight() don't work on it.
  • tg sql let pragma writes through. PRAGMA user_version = 0 changed the file.

Change

  • Query rules (tg messages search treats the whole query as one phrase #84): a plain query matches every word, in any order. Each word is quoted, so don't and e-mail are read as words, not syntax. A query with FTS5 syntax goes to FTS5 as written. --phrase keeps the old behaviour. A query FTS5 rejects returns INVALID_ARGS with a suggestion. Other SQLite errors return GENERAL_ERROR.
  • Filters (Search filters: date range, relevance order and match count #85):
    • --since and --until take a date, a date and time, or a duration back from now. A date-only --until includes that whole day.
    • messages_cache.date is in Unix seconds; the schema annotations now say so.
    • --sort relevance orders by bm25, with ties newest first.
    • --count adds count from a second COUNT query. total is still the page size, and the docs say so.
  • Snippets and view (tg sql: the message text and highlighted snippets for search matches #81):
    • --snippet adds a snippet field, built in tg from the free-text words of the query. Words under sender:, chat: or NOT are not highlighted. Matching ignores case and Latin diacritics, and word* matches any word that starts with it.
    • The new view message_search_text joins the index with the message, chat and sender, so WHERE message_search_text MATCH … ORDER BY rank works in tg sql. The view stores nothing and keeps working after the index is rebuilt.
    • print-schema shows the view as CREATE VIEW.
  • Read-only tg sql (tg sql lets PRAGMA writes through #88): after the cache is opened and migrated, the query runs on a second, read-only connection with query_only on.
    • SQLite's read-only error maps to SQL_WRITE_NOT_ALLOWED.
    • The keyword check also refuses pragma assignments, so they get a clearer message.
    • tg no longer adds a LIMIT to a pragma. Before, reading a pragma failed with a syntax error.
  • Docs: docs/search.md, docs/sql.md, docs/cli-commands.md, docs/database-schema.md, docs/testing.md, docs/architecture.md, the README, --help and the CHANGELOG. ROADMAP drops tg sql: the message text and highlighted snippets for search matches #81.

Tests

  • New src/__e2e__/messages-search-query.e2e.test.ts covers:
  • sql.e2e.test.ts queries the view with MATCH and rank. Pragma writes such as PRAGMA user_version = 0 are refused and leave the file unchanged; reading pragmas still works.
  • Unit tests cover:
    • the query builder;
    • time bounds, with a fixed clock;
    • snippet words and fragments;
    • the view on both the contentless and the full-content index, including the EXPLAIN plan and a rebuild;
    • ranking and count on both indexes;
    • the view's schema annotations.
  • bun run check, bun run test (1864 pass) and the changed e2e files pass locally.

🤖 Generated with Claude Code

neoromantic and others added 7 commits October 28, 2026 20:42
A plain query now matches every word in any order, each word quoted so
punctuation is never syntax. A query with FTS5 syntax (phrases, OR/NOT/NEAR,
column filters, prefixes) goes to FTS5 as written, --phrase keeps the old
whole-query phrase, and a query FTS5 can't parse is INVALID_ARGS with a
suggestion instead of TELEGRAM_ERROR (#84).

Co-Authored-By: Claude Opus 5.5 <[email protected]>
--since and --until take a date, a date and time or a duration back from
now; --sort relevance orders by FTS5 bm25 with ties newest first; --count
runs a second COUNT with the same filters and returns it as count, while
total stays the page size (#85). The schema annotations now say that
messages_cache.date and edit_date are Unix seconds.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
tg messages search --snippet adds a fragment of each message around the
first match, matched words in **, computed in tg from the query's free-text
terms (prefixes, Latin and Cyrillic, unicode61-style folding). The view
message_search_text joins the contentless index with the message, chat and
sender, so tg sql filters with MATCH and orders by rank without the join; it
stores nothing and outlives index rebuilds. print-schema describes it (#81).

Co-Authored-By: Claude Opus 5.5 <[email protected]>
The keyword check let PRAGMA writes through, so PRAGMA user_version = 0
changed the file and the next start ran the migrations again. After the
cache is opened and migrated, the query now runs on a second connection
opened read-only with query_only on; SQLite's read-only error maps to
SQL_WRITE_NOT_ALLOWED, and the check also refuses pragma assignments for a
clearer message. A pragma no longer gets the LIMIT clause, which made
reading pragmas a syntax error (#88).

Co-Authored-By: Claude Opus 5.5 <[email protected]>
A + or ^ inside a word (C++, 2^10) no longer makes a query FTS5 syntax;
only + between phrases and ^ at the start of a word do. Snippets fold a
diacritic the way unicode61 remove_diacritics=1 does (one per Latin letter,
so o doesn't highlight ộ), and a text with long words or none (a run of
emoji) is cut to 300 characters around the match without splitting a
surrogate pair. docs/search.md says that pretty output prints the ** markers
as they are (#84, #81).

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Date.parse moved 2026-02-30T09:30 to March and read T24:00 as the next day,
so --since/--until took a different date instead of INVALID_ARGS. The day,
hour, minute and second are now checked first (#85).

Co-Authored-By: Claude Opus 5.5 <[email protected]>
PRAGMA writable_schema(1) or journal_mode(OFF) returned success on the
read-only connection; a pragma with an argument is now allowed only when
the argument names what to read (table_info, index_list, …), else
SQL_WRITE_NOT_ALLOWED. A query with a second statement returned the first
one's rows and dropped the rest; it now returns SQL_OPERATION_BLOCKED (#88).

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@neoromantic
neoromantic force-pushed the feat/search-query-filters-snippets branch from 125a0fc to 9aff13d Compare October 28, 2026 17:38
@neoromantic
neoromantic merged commit 676f6a9 into main Oct 28, 2026
5 checks passed
@neoromantic
neoromantic deleted the feat/search-query-filters-snippets branch October 28, 2026 17:45
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

1 participant