Skip to content

Lesson 5 of 9 · 26m

Full-Text Search with FTS5 Without Illusions

Model searchable content correctly, choose external-content tables, maintain index consistency, and interpret ranking and query syntax safely.

FTS5 is one of the reasons SQLite can replace a surprising amount of application infrastructure. It is also easy to implement badly: index the wrong text, forget updates, expose raw query syntax to users, then declare search broken. Full-text search is a data pipeline, not merely a virtual table.

What FTS5 gives you

FTS5 tokenizes text, stores inverted index structures, supports phrase and boolean-style queries, and ranks matches. Create a basic index like this:

CREATE VIRTUAL TABLE posts_fts
USING fts5(title, body, tokenize = 'porter unicode61');

The tokenizer matters. unicode61 handles Unicode-aware tokenization. porter applies stemming in English, so running can match run. That is useful for human search and undesirable for exact identifiers.

External-content tables are usually the right shape

A plain FTS table stores its own copy of the text. For data already stored in a normal table, duplication is often wasteful and consistency becomes manual. External-content FTS5 indexes reference the source table:

CREATE TABLE posts(
  id INTEGER PRIMARY KEY, title TEXT NOT NULL,
  body TEXT NOT NULL, updated_at INTEGER NOT NULL
);

CREATE VIRTUAL TABLE posts_fts USING fts5(
  title, body, content='posts', content_rowid='id', tokenize='unicode61'
);

External content reduces duplication but does not magically maintain itself. You still need triggers or application-level maintenance.

Maintain the index transactionally

A typical trigger set looks like:

CREATE TRIGGER posts_ai AFTER INSERT ON posts BEGIN
  INSERT INTO posts_fts(rowid, title, body) VALUES (new.id, new.title, new.body);
END;

CREATE TRIGGER posts_ad AFTER DELETE ON posts BEGIN
  INSERT INTO posts_fts(posts_fts, rowid, title, body) VALUES('delete', old.id, old.title, old.body);
END;

CREATE TRIGGER posts_au AFTER UPDATE ON posts BEGIN
  INSERT INTO posts_fts(posts_fts, rowid, title, body) VALUES('delete', old.id, old.title, old.body);
  INSERT INTO posts_fts(rowid, title, body) VALUES (new.id, new.title, new.body);
END;

The exact form depends on table shape and whether you use external content. The non-negotiable point is that updates to the source table and index must be part of the same transaction. Otherwise a crash can leave search and truth permanently out of sync.

Query syntax is not user input

FTS5 query language supports operators such as AND, OR, NOT, phrases in double quotes, column filters, and prefix matching. Passing arbitrary user input directly into MATCH can produce syntax errors or surprising semantics:

SELECT p.id, p.title, bm25(posts_fts) AS rank
FROM posts_fts f JOIN posts p ON p.id = f.rowid
WHERE posts_fts MATCH ? ORDER BY rank LIMIT 20;

Use parameter binding for the query string, but transform user search text into the syntax you intend. For simple applications, quote each term or escape input rather than exposing boolean operators accidentally.

Field notes

  • Decide whether stemming is desirable. It helps human prose and hurts exact-token searches.
  • FTS5 indexes do not replace constraints or foreign keys in the source table.
  • Triggers are code. Review them, test them, and include them in migrations.
  • Do not concatenate user input into FTS query strings; parameterize and normalize.
  • bm25 ordering is not deterministic truth. It is a tunable relevance heuristic.
  • Reindexing belongs in maintenance tooling because schemas and tokenizers evolve.

Was this lesson helpful?

Sign in to rate