Skip to content

tg sql: the message text and highlighted snippets for search matches #81

Closed

Description

@neoromantic

Since #79 (#1), message_search is a contentless FTS5 table (content='', contentless_delete=1) on SQLite 3.43 and newer. It keeps only the word index (which words occur in which message), not a second copy of each message's text, sender, chat and files. That made a cache 11–18% smaller.

tg messages search is unchanged: it matches in message_search and reads the text from messages_cache by rowid. Direct SQL loses two things:

  • SELECT text, sender, chat, files FROM message_search returns NULL for those columns. Joining messages_cache by rowid gives the same data.
  • snippet() and highlight() don't work on a contentless table, so a tg sql query can't show the matched words in context. No tg command uses them today.

Options considered:

  1. An external-content FTS5 table whose content is a view over messages_cache. snippet() and highlight() would work in SQL, but the sender and chat columns are built from users_cache and chats_cache, which change when someone is renamed, and an external-content index needs the old values to delete a row. The index could drift from the data.
  2. Keep the contentless table and add a view that joins it with messages_cache, so tg sql users get the columns without writing the join.
  3. Add highlighting to tg messages search itself, computed in tg rather than in SQL.

Decision: 2 and 3. A view message_search_text exposes the message's columns plus a column named after the view, so SELECT … FROM message_search_text WHERE message_search_text MATCH 'word' ORDER BY rank works (SQLite flattens the view and passes the MATCH to FTS5; checked on SQLite 3.54.0). tg messages search --snippet returns each match with a short fragment around the matched words, highlighted. snippet() and highlight() in SQL stay unavailable.

Done when tg sql users can query message_search_text with MATCH and get the message text without writing the join, tg messages search --snippet returns highlighted fragments (Latin and Cyrillic, prefixes), the cache stays as small as it is now, and docs/ describe both. Related: #80.

Activity

  1. added
    enhancement
    on Oct 22, 2026
  2. changed the title [-]tg sql: message_search columns are NULL and snippet()/highlight() don't work since the contentless index[/-] [+]tg sql: the message text and highlighted snippets for search matches[/+] on Oct 28, 2026
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

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions