brewedbits.dev

Personal blog with guides and news on selfhosting, Linux and AI, from the Netherlands

nextDash nextdash --aboutYour bookmarks. Your terminal. checks your links knows your Docker host one container, no cloudLive demo →nextdash.cc ↗

Tip: an FTS5 table gives SQLite real full-text search with ranking

Sqlite
~/tips/tip-sqlite-fts5-full-text

CREATE VIRTUAL TABLE notes USING fts5(title, body);

Searches text by words and prefixes with ranked results instead of slow LIKE scans.

LIKE '%word%' reads every row and can’t rank anything. SQLite ships a full-text engine, FTS5, that builds a word index.

CREATE VIRTUAL TABLE notes USING fts5(title, body);
INSERT INTO notes VALUES ('sqlite wal', 'write ahead logging lets readers continue');
SELECT title, snippet(notes, 1, '[', ']', '...', 5)
FROM notes WHERE notes MATCH 'read*' ORDER BY rank;

MATCH takes a query string: plain words, read* for a prefix, "exact phrase", AND, OR and NOT, and title:wal to search one column. ORDER BY rank sorts by relevance, best match first, which is the order I’d want for a search box. snippet() returns a short piece of the text around the hit with markers you choose, and highlight() wraps the hits in the full column.

For example, read* finds readers and also proxy_read_timeout, because the default tokenizer splits on underscores. Good to know when you index code or config.

The FTS table is a separate copy of the text. It doesn’t follow your normal table, so you either insert into both or use the external content option with triggers, which the FTS5 docs describe. The default tokenizer also does no stemming, so running does not find run; the porter tokenizer adds that for English. Most builds include FTS5; PRAGMA compile_options; lists ENABLE_FTS5 when yours does.

Further reading: SQLite FTS5 extension

More Sqlite tips SELECT unixepoch('2026-01-01 00:00:00'); SELECT name, iif(qty > 0, 'in stock', 'sold out') AS state sqlite3 -bail app.db < migrate.sql CREATE INDEX 'orders_user_id' ON 'orders'('user_id'); –> users(id) CREATE INDEX users_active_email ON users(email) WHERE active = 1; All Sqlite tips (27) All topics

Link