SQLite FTS5 for Chinese, Japanese, Korean: 11 tokenizers tested

SQLite's default FTS5 tokenizer barely works for full-text search in Chinese, Japanese and Korean (CJK). Projects ship their own fixes: trigram, a LIKE fallback, jieba, Intl.Segmenter, hand-rolled bigrams. I wrote one of those fixes myself, a C tokenizer called fts5-cjk. I had no numbers that said how far apart these fixes are.

So I built cjk-fts-bench. It runs 11 common setups on the same data. This post gives the result and the reasons. The full tables, the confidence intervals and the commands to reproduce them are in the README.

Use bigrams plus the first character of each run

If you want one setup that works for all three languages, do two things:

  1. Split every run of CJK characters into overlapping bigrams.
  2. Add the first character of the run as one more token.

全文檢索 becomes 全 全文 文檢 檢索. Do this to the documents and to the queries.

The benchmark calls this setup app_bigram_first. Against plain bigrams, its nDCG@10 is 0.045 higher in Chinese, 0.024 higher in Japanese and 0.039 higher in Korean, each with p < 0.0001. In every language it has the top score or is statistically tied with the top score.

The code is about ten lines of Python: bigramize_edge in bench/systems.py.

The defaults fail on CJK

unicode61 is the FTS5 default. It turns a whole run of Han characters into one token, so 中華民國憲法 matches only a query for exactly those six characters. Its nDCG@10 on the Chinese ranking task is 0.008.

The built-in trigram tokenizer does better, but it returns nothing for a query shorter than three characters, and most Chinese words have two. On the exact-substring task over Taiwan's laws it finds 59.5% of the matches. A common patch sends short queries to LIKE. The patch finds 100%, but LIKE scans the whole table, and the ranking stays as poor as trigram (0.251 in Chinese).

The first character helps and the last one only costs

The first version I tested added both the first and the last character (app_bigram_edge). It ranks the same as the first-character version, with no significant difference in any language. But a Korean ranking query took 96 ms at the median, against 8 ms for plain bigrams.

Korean particles explain the cost. A Korean word often ends in 는, 의, 가 or 은, and each of those four characters occurs in 52% to 79% of the passages. A last-character query token makes FTS5 score most of the index with bm25, and the token says almost nothing about which passage is relevant.

Without the last character, the Korean median drops to 38 ms and the score does not change. You still pay for the first character. The index is larger than with plain bigrams (8% in Chinese, 6% in Japanese, 26% in Korean). A one-character query that occurs only at the end of a run finds nothing, so exact-substring recall is 98.4%. Use the version with both characters only if you need every single-character match.

One caveat. I ran this breakdown after I had seen the results, on the same queries. It is not a held-out test.

Testing Chinese alone gives the wrong answer

better-trigram makes each CJK character its own token. It beats plain bigrams by 0.041 in Chinese, then loses by 0.079 in Japanese and by 0.107 in Korean.

ICU word segmentation (Intl.Segmenter) has the highest Japanese score, 0.569. In Korean it scores 0.359, about the same as the default unicode61 (0.352). It splits Korean only at spaces, so 검색을 never matches 검색.

jieba shows no measurable difference from plain bigrams on Simplified Chinese. On the Traditional Chinese law text its recall is 61.3%. Its dictionary is Simplified.

Many tools that say "CJK" in the name were tested on Chinese only. These numbers say you have to test the three languages separately.

My own tokenizer does not win

fts5-cjk also adds the first and last characters when it indexes a document. It does not add them to the query. It ranks the same as plain bigrams, with no significant difference in any language. Adding the tokens to documents alone does nothing.

It gets worse. The first version of the benchmark showed fts5-cjk ahead by 0.05. An independent review found the cause: the benchmark read query tokens back in document mode, so only the fts5-cjk queries carried the extra characters. After the fix the lead was gone. When I added the same characters to plain bigrams, the same lead came back. The technique earned the gain, and my C code did not.

That mistake and two others are listed under Corrections in the README. A benchmark whose author also competes in it deserves suspicion, so the mistakes stay public.

fts5-cjk still has a use. It needs no text rewriting in the application, its exact-substring recall is 100%, and its index is smaller than app-level bigrams.

What the benchmark does not answer

If you maintain one of these tokenizers and I configured it wrong, open an issue on the repo. That is a bug in the benchmark.