Litestone guide / Built in
Full-text search
@@fts gives a model a search() method over the columns you name, backed by an FTS5 index that litestone creates and keeps in sync for you.
model Message {
id Int
title String?
body String
([body, title])
(body)
}
await db.message.create({ data: { id: 1, title: 'important', body: 'hello world invoice' } })
await db.message.create({ data: { id: 2, body: 'hello goodbye' } })
const hits = await db.message.search('hello world')
if (hits.length !== 1 || hits[0].id !== 1) throw new Error('both words must match')
What else it can do
- Results are ranked by relevance, and each row carries its score as
_rank where,limitandoffsetnarrow the searchhighlight: { field, open, close }andsnippet: { field, length }mark the match- The text is what a person typed: every word must match,
"a phrase"matches as one, punctuation is safe prefix: truematches the last word as a prefix, for search-as-you-typeraw: truehands the text to FTS5 as written, forOR,NEARand column filters@@fts([...], tokenize: porter)picksunicode61,ascii,porterortrigram- Soft-deleted rows are hidden, and
withDeletedoronlyDeletedchange that - Row policies apply to results
db.$search(q)ranks hits across every@@ftsmodel at onceoptimizeFts()andlitestone optimizemerge the index segments@@externalwith@@ftssearches a virtual table you manage yourself
In depth: Full-text search, § search(), § What the text means, § $search()