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.
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
| Field | Matches | Answered from |
|---|---|---|
| a bare word or phrase | in the subject, the body, or a name of from, to or cc | the index |
subject: body: | a word, "a phrase", pre* a beginning | the index; a phrase is checked on the text |
from: to: cc: | with @ the address, with a dot the domain, else words of the name | the index |
folder: | a folder by name, in(a, b), Work/* for those under it | the index |
is: flag: | unread, read, flagged, answered, draft; a keyword by name | the index |
has:attachment | the index | |
thread: id: msgid: list: | a thread, message ids and ranges of them, a Message-ID, a List-Id | the 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
| Pipe | Does |
|---|---|
sort by (f [desc], …) | Orders; may carry limit and offset |
limit N, offset N | Cuts |
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 name | One 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
- A filter becomes sorted lists of message ids from the index, and "and", "or" and "not" are operations on lists.
is:readis All mail less the unread list. - A range of dates becomes a range of ids. A table of days holds the first message id of each day a user got mail, so a month is found without reading any message outside it.
- What the index does not hold is tested on rows: size, the date sent, whether a phrase's words stand together. Only the candidates' rows are read.
- Fields are read after the pipes that cut. A sort and a limit run over the fields they use alone; the rest is read for the rows they leave. A query for the newest 1,000 reads 1,000 rows of thread ids, not one for each message.
- A word's beginning is a range read: word tags keep the word as text, so every word with a prefix lies together.
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.
| Query | ms | Lists | Candidates | Rows read |
|---|---|---|---|---|
has:attachment | limit 50 | 10 | 2 | 14,421 | 50 |
folder:INBOX is:unread | limit 50 | 15 | 2 | 1,132 | 50 |
is:unread | limit 50 | 16 | 1 | 273,733 | 50 |
date:30d | limit 50 | 19 | 1 | 10,631 | 50 |
date:[2024-01-01, 2024-02-01) | stats count() as n | 19 | 1 | 5,888 | 5,888 |
is:unread | stats by (folder) count() as unread | 203 | 33 | 273,733 | 0 |
has:attachment size:>1MB | sort by (size desc) limit 20 | fields id, size | 212 | 2 | 14,421 | 14,421 |
date:52w | top 10 by (from) | 236 | 1 | 63,000 | 63,000 |
| stats by (date:year) count() as n, sum(size) as bytes | 1,570 | 1 | 403,757 | 403,757 |
- A query the index narrows costs its lists and the rows it returns.
- A query the index cannot narrow, by size or over everything, reads a row for every message: about 1.4 seconds here, 3.5 microseconds a row, read as one range.
- Through the server, warm: a subject word in all folders with 50 results is 5.9 ms at the median, unread in a folder 4.1 ms, the ten commonest senders of the last year 201 ms.
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.
- A message is found the moment it has arrived. Its words are written in the delivery's own batch.
- What a word is has one rule, for what is indexed and for every search: a run of letters and digits, lower case, accents folded.
resumefindsrésumé, and the other way. - The text of a message is its text parts that are not attachments, in the message and in messages attached to it, with HTML read as the text a person sees.
- A dictionary of each user's words, 10 MB for this mailbox, answers letters inside words without reading every list.
| Without | With | |
|---|---|---|
| Delivering the whole mailbox, NVMe | 100 to 106 s | 149 to 158 s |
| The index keyspace | 86.7 MB | 150.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.
| Cold | Warm | |
|---|---|---|
| Narrowed by the lists, each match then read | 26,800 | 2,100 |
| Answered by the lists alone | 590 | 125 |
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
- Against every message, on real mail. A program makes queries out of what the mailbox holds: words of its subjects and bodies, senders, folders, flags, threads, dates and sizes, joined two deep. It asks each as text and compares the answer with testing all 403,757 messages by hand. 2,800 queries, then 900 more with body words: every answer exact. The check works from list rows and leaves out 24 messages whose row keeps only part of a very large header; a fault in those was found by review, and mended.
- Against a model. Random filters over random mailboxes, after deletes, flag changes and moves: 2,300 by the author and 25,200 by a reviewer, none wrong.
- The parser. A generated query printed and parsed again is the same query. Three million random texts make it fail in no way other than by saying so.
- Faults put in on purpose. Six, in the executor, in the narrowing of IMAP searches and in the dictionary. The tests caught each.
What it does not do
- Rank by how well a message matches. Answers come newest first.
- Forgive typing mistakes, or know word forms. A word's beginning stands in for them.
- Index the text of attachments, or the words of headers other than the subject and names. IMAP's
TEXTreads headers, and is slow for it. - Keep where each word stands. A phrase is found by its words, then checked in the text: seconds when the words are common and every match is wanted.