Repository navigation
feat(search): FTS5 query syntax, date and relevance filters, snippets, read-only tg sql - #107
Merged
Merged
Conversation
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
force-pushed
the
feat/search-query-filters-snippets
branch
from
October 28, 2026 17:38
125a0fc to
9aff13d
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #84
Fixes #85
Fixes #81
Fixes #88
Problem
tg messages searchput every query in quotes, so FTS5 read it as one phrase.error OR timeout,sender:alice,hel*andквартир*found nothing.hello worldmatched only when the two words were side by side. A query FTS5 couldn't parse returnedTELEGRAM_ERROR.totalis the size of the page.tg sqlcouldn't read a match's text without a hand-written join.snippet()andhighlight()don't work on it.tg sqllet pragma writes through.PRAGMA user_version = 0changed the file.Change
don'tande-mailare read as words, not syntax. A query with FTS5 syntax goes to FTS5 as written.--phrasekeeps the old behaviour. A query FTS5 rejects returnsINVALID_ARGSwith a suggestion. Other SQLite errors returnGENERAL_ERROR.--sinceand--untiltake a date, a date and time, or a duration back from now. A date-only--untilincludes that whole day.messages_cache.dateis in Unix seconds; the schema annotations now say so.--sort relevanceorders by bm25, with ties newest first.--countaddscountfrom a second COUNT query.totalis still the page size, and the docs say so.--snippetadds asnippetfield, built in tg from the free-text words of the query. Words undersender:,chat:orNOTare not highlighted. Matching ignores case and Latin diacritics, andword*matches any word that starts with it.message_search_textjoins the index with the message, chat and sender, soWHERE message_search_text MATCH … ORDER BY rankworks intg sql. The view stores nothing and keeps working after the index is rebuilt.print-schemashows the view asCREATE VIEW.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 withquery_onlyon.SQL_WRITE_NOT_ALLOWED.LIMITto a pragma. Before, reading a pragma failed with a syntax error.docs/search.md,docs/sql.md,docs/cli-commands.md,docs/database-schema.md,docs/testing.md,docs/architecture.md, the README,--helpand the CHANGELOG. ROADMAP drops tg sql: the message text and highlighted snippets for search matches #81.Tests
src/__e2e__/messages-search-query.e2e.test.tscovers:--snippet.sql.e2e.test.tsqueries the view withMATCHandrank. Pragma writes such asPRAGMA user_version = 0are refused and leave the file unchanged; reading pragmas still works.EXPLAINplan and a rebuild;bun run check,bun run test(1864 pass) and the changed e2e files pass locally.🤖 Generated with Claude Code