CREATE VIRTUAL TABLE docs USING fts5(title, body);FTS5 is the go-to SQLite feature for efficient full-text search.
FTS5 virtual tables, MATCH queries, prefix search, phrase search, bm25 ranking, snippets, and highlighting patterns for SQLite search features.
Set up virtual tables for full-text search.
CREATE VIRTUAL TABLE docs USING fts5(title, body);FTS5 is the go-to SQLite feature for efficient full-text search.
INSERT INTO docs (title, body)
VALUES ('SQLite WAL guide', 'Write-ahead logging improves concurrency for many app workloads.');You can index plain text fields directly or sync from content tables.
Separate storage from the FTS index when needed.
CREATE TABLE articles (
id INTEGER PRIMARY KEY,
title TEXT,
body TEXT
);
CREATE VIRTUAL TABLE articles_fts USING fts5(
title,
body,
content='articles',
content_rowid='id'
);Common when the app needs a normal table plus a search index over the same content.
Run practical search queries and return better results.
SELECT rowid, title FROM docs WHERE docs MATCH 'sqlite';MATCH is the core FTS operator.
SELECT rowid, title FROM docs WHERE docs MATCH '"write ahead logging"';Phrase queries are useful for documentation and knowledge-base search.
SELECT rowid, title FROM docs WHERE docs MATCH 'concurr*';Useful for search-as-you-type and partial matching.
SELECT rowid, title, bm25(docs) AS rank
FROM docs
WHERE docs MATCH 'sqlite wal'
ORDER BY rank;bm25() is the usual relevance-ranking helper for FTS5 results.
Decorate matched terms in search output.
SELECT title,
highlight(docs, 1, '<mark>', '</mark>') AS highlighted_body,
snippet(docs, 1, '<b>', '</b>', '…', 12) AS excerpt
FROM docs
WHERE docs MATCH 'sqlite';Great for search result pages where you want visible context around the match.