Skip to main content

2026-11-27 · 4 min read

Chinese full-text search in SQLite: FTS5 trigram, no word segmentation

Data

Translated from the Chinese original: read it in Chinese

Summary: If you do not want to install a word-segmentation package for Chinese full-text search, you can use the trigram tokenizer of SQLite FTS5: it cuts text into 3-character pieces for the index, so any fragment of 3 or more characters is found, and Node's built-in node:sqlite supports it. Two traps: 2-character words cannot be found (fall back to LIKE), and deleting with LIKE on an FTS5 table deletes 0 rows (use IN instead).

The site's admin has three piles of text: to-dos, the done log and the action log, a few hundred entries in total. To find "what I did before", I only had a command-line LIKE '%text%', and each of the three places was searched separately. I wanted one search box that searches them all.

The hardest part of Chinese full-text search is word segmentation: where do you cut a phrase like "change the title of the accommodation record"? The usual answer is to install a segmentation package or switch search engines. But SQLite's FTS5 has a rarely mentioned tokenizer, trigram, that sidesteps the problem.

trigram: no word cutting, just every 3 characters

trigram cuts text into pieces of "every 3 characters" for the index, and cuts the query the same way. So there is no need to know where words break: as long as the query string has 3 or more characters, any fragment can be matched.

The node:sqlite built into Node 24 already ships with FTS5, so nothing needs installing. Creating the table takes one statement:

CREATE VIRTUAL TABLE search USING fts5(
  src UNINDEXED, ref UNINDEXED,   -- the source and the id in the original table, not indexed
  title, body,
  tokenize = 'trigram'
);

Searching a three-character fragment from the middle of that phrase (the characters for "stay record") finds the entry whose title is "change the title of the accommodation record", with no word segmentation anywhere.

Three sources in one table, kept in sync with triggers

To-dos, the done log and the action log are three different tables. I made them share one search table: src records the source (todo, done, log, with to-dos further split into todo:done and todo:dropped), and ref records the id in the original table. Inserts, updates and deletes on the original tables are synced over with triggers, and when the table is created, the existing data is backfilled once:

CREATE TRIGGER search_done_ai AFTER INSERT ON done_log BEGIN
  INSERT INTO search (src, ref, title, body)
  VALUES ('done', CAST(new.id AS TEXT), new.title, new.body);
END;

This way, whether the website or a command-line script writes the data, the index keeps up by itself.

Querying: treat the whole string as a phrase, mark matches with highlight()

What users type cannot go straight into MATCH: typing -, *, : or a quote makes FTS5 treat it as query syntax and throw an error. The fix is to wrap the whole string in double quotes as one phrase (double quotes inside the string become two):

const phrase = `"${q.replace(/"/g, '""')}"`;
db.prepare(
  `SELECT src, ref,
          highlight(search, 2, ?, ?) AS title,
          snippet(search, 3, ?, ?, '…', 14) AS snippet
   FROM search WHERE search MATCH ? ORDER BY rank LIMIT 40`,
).all(OPEN, CLOSE, OPEN, CLOSE, phrase);

highlight() and snippet() wrap the matching text in the markers you specify, and the page then swaps the markers for <mark>. Results are sorted by rank, so title matches naturally come first. Searching for "accommodation record" returns one to-do, one done-log entry and one action-log entry together, each labeled with its source.

Trap 1: 2-character words cannot be found

trigram needs at least 3 characters. Searching a 2-character word such as "radar" (in Chinese) finds nothing, because the index has no matching piece, and the result is 0 rows. My approach: when the query is under 3 characters, fall back to LIKE, with no ranking and no highlighting, just "the title or body contains this text". The explanation on the page also says plainly "for Chinese, 3 or more characters works best", rather than pretending 2-character words search just as well.

Trap 2: deleting with src LIKE 'todo%' on an FTS5 table deletes 0 rows

When an original row is updated, the trigger must delete the old index row before inserting the new one. At first I wrote:

DELETE FROM search WHERE src LIKE 'todo%' AND ref = old.id;

A unit test immediately caught the problem: after updating a to-do title, the old title could still be found. Testing it in isolation, on the same table src = 'todo:done' AND ref = ... deleted the row, while src LIKE 'todo%' AND ref = ... reported 0 rows deleted. I did not dig into why; I only established that matching an FTS5 UNINDEXED column with LIKE is unreliable. I changed it to list the values explicitly:

DELETE FROM search WHERE src IN ('todo', 'todo:done', 'todo:dropped') AND ref = old.id;

This trap produces no error message at all, only "what should have been deleted was not", so a test must cover the scenario "after an update, the old content can no longer be found".

Summary

  • FTS5 trigram: no word segmentation for Chinese, any fragment of 3 or more characters is found, built into Node 24's node:sqlite with no package to install
  • Several sources share one table, kept in sync by triggers, with a backfill of old data when the table is created
  • Wrap the user's whole input as a phrase, so it is not read as FTS syntax
  • Fall back to LIKE for 2-character words; delete on an FTS5 table with IN (...), not LIKE

Type to search projects and posts too