Skip to content

Find Chinese and Japanese words with the built-in search - #172

Merged
shenxianpeng merged 1 commit into
mainfrom
claude/project-thread-ckcodu
Nov 7, 2026
Merged

shenxianpeng merged 1 commit into
mainfrom
claude/project-thread-ckcodu

Conversation

@shenxianpeng

@shenxianpeng shenxianpeng commented Nov 7, 2026 •

Copy link
Copy Markdown
Member

The built-in search plugin couldn't find Chinese or Japanese words. Chinese and Japanese are written without spaces, so lunr saw a whole sentence as one word:

  • With lang: zh (also what a zh language of a languages: site gets), search didn't work at all: the bundled lunr.zh.js calls jieba, which only exists in Node.js, so building the index stopped with Cannot read properties of undefined (reading 'cut').
  • With lang: [en, zh], lang: ja or a prebuilt index, a word inside a sentence found nothing, and two-character words such as 搜索 were below the default min_search_length.

What changes, with no new dependency:

  • New cjk-segment.js splits Chinese and Japanese text into words with the browser's built-in Intl.Segmenter (one word per character in browsers without it, such as Firefox before 125). It wraps the index tokenizer and splits the search query the same way, so both always agree. It also runs in prebuild-index.js. It is only copied and loaded when lang has zh or ja; other sites get the same files as before.
  • lunr.zh.js uses the default tokenizer outside Node.js instead of failing.
  • lunr.zh.js and lunr.ja.js stem search words with the same stemmer as indexed words. Before, English words on such pages could not be found when English stemming changed them (Jerry → jerri).
  • main.js: a Chinese or Japanese character counts as two towards min_search_length.
  • main.js also shows titles such as Tom & Jerry correctly. Titles in the index are HTML, so escapeHtml turned & into &. The index is unchanged, so other themes that read it see no difference.

Checked in Chromium with the mkdocs and readthedocs themes (search in a Web Worker, in the page, and in the page without Intl.Segmenter):

Site Query Before After
theme.locale: zh_CN 搜索 error, no results page found
lang: [en, zh] 配置文件 no results page found
lang: ja 設定ファイル, 検索 no results page found
lang: ja, prebuild_index: true 設定ファイル — page found
languages: {en, zh}, /zh/ 搜索功能 — page found
lang: zh / ja Jerry, jumps — page found
English only Jerry, documentation found found, title shows Tom & Jerry

Related Issue

Roadmap item 50 in #59.

Checklist

  • New tests added for new behavior (if applicable): which files are copied for zh, ja and [en, zh]
  • Documentation updated (if applicable): search.lang in the configuration docs, Fixed Upstream Issues page
  • Release notes docs/about/release-notes.md updated (if applicable): under 1.11.2 (unreleased)

Chinese and Japanese are written without spaces, so lunr saw a whole
sentence as one word. With `lang: zh` search didn't work at all: the
bundled lunr.zh.js calls jieba, which only exists in Node.js, and the
index build stopped with an error. Japanese only found whole sentences
and two-character words were below min_search_length.

- New cjk-segment.js splits Chinese and Japanese text into words with
  the browser's Intl.Segmenter (one word per character without it). It
  wraps the index tokenizer and splits the query the same way, also for
  prebuilt indexes and several languages together. Loaded only when
  `lang` has zh or ja.
- lunr.zh.js falls back to the default tokenizer outside Node.js.
- lunr.zh.js and lunr.ja.js stem search words like indexed words, so
  English words on such pages are found too.
- A Chinese or Japanese character counts as two for min_search_length.

Also show titles like `Tom & Jerry` instead of `Tom & Jerry` in
search results: titles in the index are HTML, and main.js escaped them
again.
@codspeed

codspeed Bot commented Nov 7, 2026

Copy link
Copy Markdown
Contributor

Merging this PR will not alter performance

✅ 29 untouched benchmarks


Comparing claude/project-thread-ckcodu (72073e8) with main (e1e4198)

Open in CodSpeed

@shenxianpeng
shenxianpeng merged commit 7678864 into main Nov 7, 2026
25 checks passed
@shenxianpeng
shenxianpeng deleted the claude/project-thread-ckcodu branch November 7, 2026 14:51

This branch was successfully deployed

1 active deployment
preview — 72073e81 Deployed Nov 7, 2026 by shenxianpeng via preview #546
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant