Skip to content

feat(search): find other word forms with messages search --forms - #216

Merged
neoromantic merged 22 commits into
neoromantic:mainfrom
ertembiyik:ertembiyik/NO-TASK/search-word-forms
Nov 27, 2026
Merged

neoromantic merged 22 commits into
neoromantic:mainfrom
ertembiyik:ertembiyik/NO-TASK/search-word-forms

Conversation

@ertembiyik

@ertembiyik ertembiyik commented Nov 19, 2026 •

Copy link
Copy Markdown
Contributor

What and why

tg messages search --forms also matches the other forms of each plain word that occur in the index: квартира finds квартиру and квартирах, meeting finds meetings. Without the flag search is what 0.16.0 does: each word as typed. This is #92, with the benchmark it asks for. Refs #92

The need: the index has no stemming, so a Russian word finds about a third of the messages that contain some form of it, and a two-word Russian query about a ninth. A caller has to know to write квартир*.

Without the flag (unchanged; real output of the e2e environment, trimmed to the fields that matter)

$ tg messages search --query квартира --limit 3 --snippet
"filters": { …, "forms": false }, "forms": null
results: 2 "**Квартира** ещё свободна?"
total 1, no next

With --forms

$ tg messages search --query квартира --forms --limit 3 --snippet
"filters": { …, "forms": true }
"forms": {"квартира": {"words": ["квартира","квартирах","квартирой","квартиру","квартиры"], "cut": false}}
results: 1 "Сдаю **квартиру** у парка", 2 "**Квартира** ещё свободна?", 3 "Поговорим о **квартирах** и ценах"
"next": [{"command": "tg messages search --query='квартира' --forms --limit=3 --offset=3 --snippet", "why": "The next page of results."}]

The benchmark (bun run bench:search, defined in docs/storage.md §9 before any candidate ran)

Judge: a dictionary lemmatizer for each language, independent of the candidates; a result is right when it holds a form of the query word's lemma. Corpus: 50,000 public conversational sentences per language, fetched at run time. 385 Russian and 400 English single-word queries in four frequency bands, 200 two-word queries per language, paired bootstrap intervals.

Candidate RU found RU correct EN found EN correct Cost
exact (0.14.0) 0.37 1.00 0.75 1.00
automatic prefix (option 1) 0.45 0.92 0.96 0.90 prefix= index 1.8–2.1× the size; loses the newest-first walk of #118
trigram index (option 2) 0.84 0.70 0.92 0.72 index 4.1–6.9×
aggressive stems (option 3) 0.84 0.89 0.96 0.91 about 1,570 vendored lines
light stems (option 3) 0.70 0.95 0.84 1.00 about 120 lines in the repo

The same run on a real 62,000-message cache (read-only, aggregates only) agrees: Russian 0.33–0.37 found with exact search, 0.74–0.76 with light stems at 0.93 correct; two-word Russian queries with no result fall from 60% to 36%.

Stems in the index and expansion of the query gave identical results (the same matches by construction), so this PR expands the query and leaves the index alone.

Opt-in, as reviewed

No candidate met the bar fixed in advance (Russian recall up by at least 0.15 and precision down by at most 0.05): light stems gain 0.34 and lose 0.059. An earlier head of this PR made forms the default anyway; per your review of 2026-11-22 it is a flag again, as in 17b5bd7, and plain searches stay exact for existing callers.

About half of the wrong matches are different words with one stem (брат/брать, лет/лето); the unit tests list them as known limits, and docs/search.md states them.

How it works

  • No index change, no migration. For each plain word, the index's own words with the same light stem are read from message_search_vocab and transcript_search_vocab, persistent fts5vocab tables over the message and transcript indexes, and the FTS5 expression becomes an AND of OR groups. The tables are created with the schema if missing, have no storage of their own, survive a rebuild of the index, and are readable on query_only connections such as tg sql. Older tg versions and other SQLite clients read and write the cache as before.
  • Light stemmers in src/utils/word-stems.ts: Russian noun and adjective endings, English plurals, ё read as е. They are ports; NOTICE carries the attribution. The benchmark imports the same module, so it measures what ships.
  • Plain words only. --forms with FTS5 syntax (a quoted phrase, word*, OR, sender:, chat:) or with --phrase is INVALID_ARGS with a suggestion, as in 17b5bd7: forms beside a NOT, a phrase or sender: would change what the query means.
  • allowedChats. While an allowlist is set, --forms matches words as typed and forms is null: the forms come from the whole cache and would show words of other chats.
  • Short words. A word whose stem has one or two letters (ok, не) and a word with no stem (2026, 2ms) are looked up as typed, without scanning the vocabulary. So it does not find its.
  • At most 30 forms per word: the typed word first, then the most frequent. A longer group is cut and says so (cut: true).
  • Output: forms (per query word, the words searched for; null without --forms or with allowedChats) and filters.forms. next carries --forms. --snippet highlights every form. Filters, --count and both sorts work with it.
  • ё/е: with --forms, еще finds ещё and the reverse; without it they are different letters, as before.
  • Speed (medians on the real cache, measured on the earlier head, before the short-word lookup): a word with one form keeps the one-word fast path; 0.08 ms against 0.06 ms for a rare word, 7.1 against 7.1 for a common one, 4.8 against 4.2 for a mid one and 0.8 against 0.3 for two words.

NOTICE in the release

NOTICE was only in the npm tarball. Now release.yml copies it into dist, adds it to checksums.txt (6 lines instead of 5) and uploads it to the GitHub Release next to the binaries; formula.sh reads its checksum and adds a resource "notice" that the formula installs into its prefix; tap.sh waits for the asset. docs/releasing.md and docs/build-distribution.md follow.

Not run: the release workflow itself, the upload, brew audit --strict --online and brew install (no release has the asset yet). Checked locally: sh -n on both scripts; a dry run of formula.sh on made-up binaries plus the real NOTICE (checksums verify, 6 lines); formula.sh refuses a checksums.txt without NOTICE; ruby -c and brew style on the generated formula. Please look at that part with the eyes of whoever runs the next release.

Other decisions you may want to change: the cap of 30; forms always present in the output.

Left out, on purpose: typo correction (measured in the same benchmark: a fallback when a query finds nothing passes its bar; I'll send it separately), phrases of word forms (they need stems in the index), keyboard-layout and transliteration rewrites.

The benchmark vendors the Snowball stemmers under scripts/snowball/ (BSD-3, COPYING included) only to measure the aggressive option; nothing in src/ uses them. Say if you'd rather the benchmark fetch them at run time.

How I checked it

  • E2E through the real CLI on a made-up cache (10 tests in src/test/messages-search-forms.e2e.test.ts): Russian and English forms found with --forms and not without; ё/е; a word with no forms; FTS5 syntax and --phrase matched as written without the flag and refused with it; chat, sender, date and deleted filters; allowedChats with --forms; relevance sort, count and snippets; next with and without --forms.

  • Unit tests beside the code: the stemmers (with the wrongly merged pairs as known limits), the expansion (range scan, stem filter, cap, absent word, ё/е, case and diacritics, exact lookups for short and stemless words counted by database calls, the tables surviving a rebuild and read on a query_only connection), the query builder.

  • Read-only on a real cache with an earlier head: one word found 146 messages with forms and 19 as typed (7 forms); a two-word query 4 and 1; the existing cache got message_search_vocab on open.

  • On the current head (3434504): bun run check clean, bun run test 1361 pass, bun run test:e2e 448 pass, 0 fail.

  • Not verified: Linux and Windows (the SQLite bundled there), query time on a cache of a million messages, and the release steps listed above.

  • Tests pass locally and use made-up data only

  • README, docs and affected demos follow the product content standard (docs/search.md, docs/cli-commands.md, docs/database-schema.md, docs/storage.md, docs/releasing.md, docs/build-distribution.md, README, skill, CHANGELOG)

  • Title follows the repo's commit style: Conventional Commits (feat: …, fix: …) unless AGENTS.md says otherwise

Update: review of 2026-11-22 applied (3434504)

  • 8cd2b55: --forms is back and plain searches are exact, as in 17b5bd7; --exact and filters.exact are gone; with allowedChats set --forms does not expand. Help, docs, skill, changelog and tests follow. The title lost its !: nothing changes for existing callers.
  • aba9541: short and stemless words are looked up exactly instead of scanning the vocabulary by a one- or two-character prefix (the "known and not changed" point of the previous update).
  • 3434504: NOTICE ships with the release binaries and the Homebrew formula (section above).

The updates below are the history of the branch while forms were the default; where they mention --exact or the default, the sections above are current.

Update: second code review (617c9fe)

A second, independent review of this branch. Each point below was first shown by a failing test, then fixed.

  • allowedChats: forms listed words from the whole account's index, so with an allowlist it showed words that occur only in chats outside it. While an allowlist is set, words are now matched as typed and forms is null, as with --exact. Reporting only the forms that occur in allowed chats would need, for each form, a match filtered by chat, which reads every message with that word when it occurs only elsewhere. Your call if you prefer that cost over losing word forms under an allowlist.
  • Every expanded group starts with the word as typed, and the cap of 30 keeps a place for it; a word typed with a combining mark could be replaced by its composed form and lose its exact match.
  • Only a word made of Latin (a–z) or Cyrillic letters is stemmed: 2ms was read as 2m.
  • A one-letter word is looked up as it is, without the range scan. A two-letter word still scans the index words with its first letter (its and it are forms of each other).
  • docs/storage.md says the benchmark judges a match by lemma without context, so precision on ambiguous words is somewhat lower in real use.
  • NOTICE is in the npm tarball. The binary release and the Homebrew formula ship only the executable; the notice for the ported stemmers would need to be a release asset the formula installs. .github/ is untouched.

After these commits: bun run check clean, bun run test 1216 pass, bun run test:e2e 347 pass, 0 fail.

Update: main merged in (649eef9)

main up to #220 merged in, no conflicts and no changes to this branch's code.

After the merge: bun run check clean, bun run test 1227 pass, bun run test:e2e 348 pass, 0 fail.

Update: main merged in (853a651)

main up to 0.15.1 and #232 merged in. One conflict: CHANGELOG.md (the entry of this PR now sits under the new ## [Unreleased]). No code change.

After the merge: bun run check clean, bun run test 1239 pass, bun run test:e2e 374 pass, 0 fail.

Update: main merged in (ac85803)

main up to 0.15.2 (#234) merged in. One conflict: CHANGELOG.md (the entry of this PR stays under ## [Unreleased], above the new 0.15.2 section). No code change in this PR.

After the merge: bun run check clean, bun run test 1239 pass, bun run test:e2e 374 pass, 0 fail.

Update: main merged in (392c8d8), d9113f5

Conflicts in CHANGELOG.md, src/commands/messages/search.ts and src/db/messages-search.ts: the forms are now worked out before readSearchPage and passed through it, and main's --type, typesPending, suggestion and transcriptMatch output is kept.

Two things were wrong once this was combined with transcript search on main, each fixed with a test that fails without it:

  • 2649c58: count, the untyped-match check and a search with transcript hits built their match without the forms, so --count gave 1 where the list had 4 results. They now get the same query as the list.
  • d9113f5: forms were read only from the message index, so a form that occurs only in a voice or video note transcript was never searched for. A second vocabulary table, transcript_search_vocab, is read together with message_search_vocab; docs/search.md, docs/database-schema.md and the schema description follow.

Checks: bun run check clean, unit 1361 pass, e2e 447 pass, 0 fail.

Known and not changed here: a two-letter word or a number still scans the vocabulary by a one- or two-character prefix before the search, and the description of message_search_vocab in tg sql print-schema still names --forms, which is now the default with --exact to turn it off.

🤖 Generated with Claude Code

ertembiyik and others added 7 commits November 19, 2026 12:48
… add the stemmers' notice

Co-Authored-By: Claude Fable 5.2 <[email protected]>
Plain queries expand to the index's forms of each word. A query with FTS5
syntax or --phrase is matched as written, as before, so the two refusals
of the opt-in flag are gone. The answer has forms (null when the query was
not expanded) and filters.exact.

BREAKING CHANGE: tg messages search returns more results for plain words;
pass --exact for the previous behaviour.

Co-Authored-By: Claude Fable 5.2 <[email protected]>
@ertembiyik ertembiyik changed the title feat(search): find other word forms with messages search --forms feat(search)!: find other word forms by default in messages search Nov 19, 2026
ertembiyik and others added 6 commits November 19, 2026 19:53
The forms come from the whole cache's vocabulary, so listing them could
show words of chats outside the allowlist. Checking each form against the
allowed chats would read every message with that word, so expansion is
off while a list is set, as with --exact.

Co-Authored-By: Claude Fable 5.2 <[email protected]>
2ms stemmed to 2m, so a search for milliseconds also matched minutes. A
word with a digit or a letter of another script is now matched as typed.

Co-Authored-By: Claude Fable 5.2 <[email protected]>
The vocabulary lookup reads the folded word, which can be another index
word than the one the tokenizer makes of the typed word (a letter typed
with a combining mark), so the exact match dropped out. Every group now
starts with the word as typed, and the cap of 30 keeps a place for it.

Co-Authored-By: Claude Fable 5.2 <[email protected]>
No other word stems to one letter, so scanning every index word with
that first letter only cost time. Two-letter words keep the scan: its
stems to it, and её reads as ее.

Co-Authored-By: Claude Fable 5.2 <[email protected]>
@ertembiyik

Copy link
Copy Markdown
Contributor Author

Two things from a second review that are your call:

  1. With allowedChats set, forms listed words from the whole index, including words that occur only in chats outside the allowlist. I turned word forms off while an allowlist is set (forms: null, as with --exact). The alternative keeps them but needs one chat-filtered match per form, which reads every message with that word when it occurs only elsewhere. Say if you prefer that.
  2. NOTICE (for the ported stemmers) is in the npm tarball, but the binary release and the Homebrew formula ship only the executable. I didn't touch .github/.

@neoromantic neoromantic left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's go with the opt-in version for #92. The Russian recall gain is worth shipping, and the precision and two-word latency results support keeping today's behavior for existing callers. Please bring back tg messages search --forms and keep plain searches exact by default, as in 17b5bd7. Please update the help, docs, changelog and tests to describe that behavior.

The allowedChats behavior from your second review looks right. Please keep expansion off when an allowlist is active, including with --forms, and include the stemmer NOTICE with the release binaries and the Homebrew install. Once those changes are on the branch, we can review the updated head and run CI.

ertembiyik and others added 5 commits November 22, 2026 23:53
…arch-word-forms

# Conflicts:
#	CHANGELOG.md
#	src/commands/messages/search.ts
#	src/db/messages-search.ts

Co-Authored-By: Claude Fable 5.2 <[email protected]>
The FTS5 match built for count, the untyped-match check and the search
that joins transcript hits passed only the phrase option, so it dropped
the word forms: --count disagreed with the results, and forms stopped
working once a transcript matched.

Co-Authored-By: Claude Fable 5.2 <[email protected]>
findQueryForms read only message_search_vocab, so a form that occurs only
in a voice or video note transcript was never searched for. A second
fts5vocab table, transcript_search_vocab, is read together with it,
summing doc per term.

Co-Authored-By: Claude Fable 5.2 <[email protected]>
Plain tg messages search matches each word as typed, as 0.16.0 does.
--forms turns word forms on and is refused with FTS5 syntax or --phrase,
as in 17b5bd7; --exact and filters.exact are gone, filters.forms is back.
While allowedChats is set, --forms doesn't expand and forms is null.
The help, docs, skill, changelog and storage.md §9 record the opt-in
choice made in review.

Co-Authored-By: Claude Fable 5.2 <[email protected]>
A word whose stem has one or two letters (ok, не) scanned every index word
that starts with its first letter, and a word with no stem (2026) scanned
on its prefix only to keep itself. Both are now exact lookups, the first
with е and ё both, so --forms no longer reads a large part of the
vocabulary before such a search. it no longer finds its.

Co-Authored-By: Claude Fable 5.2 <[email protected]>
…ormula

The release workflow copies NOTICE into dist, checksums it with the
binaries and uploads it to the GitHub Release. The Homebrew formula takes
it as a resource and installs it in the formula's prefix; tap.sh waits
for it to download before the audit.

Co-Authored-By: Claude Fable 5.2 <[email protected]>
@ertembiyik ertembiyik changed the title feat(search)!: find other word forms by default in messages search feat(search): find other word forms with messages search --forms Nov 25, 2026
@ertembiyik

Copy link
Copy Markdown
Contributor Author

Done as you asked, on 3434504:

  • --forms is a flag again and plain searches are exact, as in 17b5bd7; --exact is gone.
  • With allowedChats set, --forms does not expand (forms: null).
  • NOTICE is a release asset, is in checksums.txt, and the Homebrew formula installs it. I could not run the release workflow or brew install; the description lists what I checked locally.
  • Also: words with a stem of one or two letters, and tokens without a stem, are looked up as typed instead of scanning the vocabulary.

The description is rewritten for the opt-in version. CI is green.

@neoromantic
neoromantic merged commit 3434504 into neoromantic:main Nov 27, 2026
5 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants