tmail.sh

Search: a query language over lists of words

One string says what a client wants to see: which messages, in what order, how many, which fields, or what counts. The server works out how to answer it from the index, and says in its reply what it read.

The language

Filters, then pipes. The model is VictoriaMetrics' LogsQL: someone who knows it can guess most of this one.

Queries
folder:INBOX is:unread date:7d
from:example.org subject:"quarterly report" -is:read
(from:ann OR to:ann) has:attachment size:>1MB
body:invoice* date:[2026-01-01, 2026-04-01)
is:unread | stats by (folder) count() as unread
folder:INBOX | sort by (date desc) | limit 50 | fields id, from, subject, flags
folder:Lists | last 1 by (date) partition by (thread) | limit 50
date:52w | top 10 by (from)

Filters

FieldMatchesAnswered from
a bare word or phrasein the subject, the body, or a name of from, to or ccthe index
subject: body:a word, "a phrase", pre* a beginningthe index; a phrase is checked on the text
from: to: cc:with @ the address, with a dot the domain, else words of the namethe index
folder:a folder by name, in(a, b), Work/* for those under itthe index
is: flag:unread, read, flagged, answered, draft; a keyword by namethe index
has:attachmentthe index
thread: id: msgid: list:a thread, message ids and ranges of them, a Message-ID, a List-Idthe index
date:arrival: 7d, a day, comparisons, [a, b)a table of days, then the row
sent: size:the Date header; >1MB, [a, b]the row

Filters side by side are joined by "and". OR, NOT (also -) and parentheses do what they say. A mistake in a query is refused with the character it is at.

Pipes

PipeDoes
sort by (f [desc], …)Orders; may carry limit and offset
limit N, offset NCuts
fields a, b, …Keeps these fields. Fields kept apart in the store are read only if named or used
stats [by (f, …)] fn(…) [if (filter)] as nameOne row for each group
top N by (f)The N commonest values of a field
uniq by (f, …)Distinct values
first / last N by (f) partition by (g)The first or last of each group: the newest message of each thread
filter …Filters the rows so far

How a query is answered

What a query reads

Query streams the column names, then the rows, and in its last reply what the query read. On the 403,757-message mailbox, each query in a process of its own, on NVMe with the store's files in memory. Queries have not been timed on spinning disks.

QuerymsListsCandidatesRows read
has:attachment | limit 5010214,42150
folder:INBOX is:unread | limit 501521,13250
is:unread | limit 50161273,73350
date:30d | limit 5019110,63150
date:[2024-01-01, 2024-02-01) | stats count() as n1915,8885,888
is:unread | stats by (folder) count() as unread20333273,7330
has:attachment size:>1MB | sort by (size desc) limit 20 | fields id, size212214,42114,421
date:52w | top 10 by (from)236163,00063,000
| stats by (date:year) count() as n, sum(size) as bytes1,5701403,757403,757

The index of words

The words of every body are kept as lists in the same engine as everything else: for each word of a user's mail, the messages that have it. No second system to run, and no second format with commits of its own.

What the index of words costs, on the example mailbox
WithoutWith
Delivering the whole mailbox, NVMe100 to 106 s149 to 158 s
The index keyspace86.7 MB150.3 MB

That is 0.12 ms a message more to deliver, and 63.6 MB more to keep: half a per cent of the mail. A word of a body alone is answered from its list in under a millisecond.

Through IMAP

IMAP's SEARCH is answered from the same lists. One word asked for in BODY or SUBJECT reads no message at all.

A word in the text of the inbox (36,898 messages), on spinning disks, milliseconds
ColdWarm
Narrowed by the lists, each match then read26,8002,100
Answered by the lists alone590125

The first row is IMAP's search to the letter, which minds accents; the lists do not, so each match was read to see that it matched. Since 2026-11-27 Tern's IMAP searches fold accents and case, as the query language does.

How it is checked

What it does not do

Back: storageNext: Tern vs Cyrus