Skip to content

tg messages search treats the whole query as one phrase #84

Closed

Description

@neoromantic

escapeFts5Query in src/db/messages-search.ts wraps every query in double quotes, so FTS5 reads it as one phrase. Checked on an in-memory FTS5 table with the same escaping (SQLite 3.54.0):

Query Matches with the escaping Without it
error OR timeout 0 2
sender:alice 0 3
hel* 0 1
квартир* 0 2

So the operators, column filters and prefixes that --help and docs/search.md describe don't work, hello world finds only messages where the two words stand next to each other, and there is no way to reach other word forms (квартира, квартиру) with a prefix. A query FTS5 can't parse would also come back as TELEGRAM_ERROR.

Proposed:

  • A query without FTS5 syntax matches messages that contain every word, in any order. Each word is quoted, so punctuation (don't, e-mail) never causes a syntax error.
  • A query with FTS5 syntax ("a phrase", OR, NOT, NEAR, column:, prefix*, parentheses) goes to FTS5 as written. A syntax error returns INVALID_ARGS with a suggestion.
  • --phrase keeps today's behaviour: the whole query as one phrase.
  • --help, docs/search.md and docs/cli-commands.md describe the rules with examples, including word* for other word forms.

Done when e2e tests cover every row of the table, a Cyrillic prefix, punctuation in a plain query and a syntax error.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions