SQLite Full-Text Search (FTS5) Cheatsheet

FTS5 virtual tables, MATCH queries, prefix search, phrase search, bm25 ranking, snippets, and highlighting patterns for SQLite search features.

View
StandardDetailedCompact
Export
Copy the compact sheet, download it, or print it.
Download
`D` dense toggle · `C` copy all

Create FTS5 tables

Set up virtual tables for full-text search.

Create an FTS5 virtual table

Store searchable text in an FTS5 index.

sqlANYsqlitefts5virtual table
sql
CREATE VIRTUAL TABLE docs USING fts5(title, body);

FTS5 is the go-to SQLite feature for efficient full-text search.

Insert searchable content

Add rows to an FTS5 table.

sqlANYsqlitefts5insert
sql
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.

Use an external content table pattern

Separate storage from the FTS index when needed.

sqlANYsqlitefts5content table
sql
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.

Search, rank, and highlight

Run practical search queries and return better results.

Search with MATCH

Find rows containing a term.

sqlANYsqlitematchfts5
sql
SELECT rowid, title FROM docs WHERE docs MATCH 'sqlite';

MATCH is the core FTS operator.

Rank results with bm25()

Sort by relevance score.

sqlANYsqlitebm25ranking
sql
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.

Highlight matches or return snippets

Decorate matched terms in search output.

sqlANYsqlitehighlightsnippet
sql
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.

Recommended next

No recommendations yet.