## Background
Suffix (backward) headword search — finding headwords that *end* …with the query —
is currently missing (#2030). The proposed implementation in #3160 does it with a
linear scan over the btree leaf chain, i.e. the same cost as an existing wildcard
query (`*ità`). It is correct, needs no new files, and works on every btree-based
backend.
This is not a niche request: printed reverse dictionaries (倒序词典) group entries by
their last character, and the 7th edition of 《现代汉语词典》 got a reverse edition in
2026. See the People's Daily Overseas Edition report (2026-04-14, in Chinese):
https://paper.people.com.cn/rmrbhwb/pad/content/202604/14/content_30150842.html
For large dictionaries, though, a suffix query is O(headwords). A reversed-headword
index would make it O(log n): store each headword reversed, sort, and turn a suffix
query into `lower_bound(reverse(query))`. That is the same trick printed reverse
dictionaries use, moved into an index.
### Why it matters more for CJK
For Western languages, a missing suffix search is partly papered over by
`stemmedMatch()`, Hunspell suggestions and `expressionMatch()` — all of them fallbacks
after a failed lookup, not a way to browse headwords by their ending. CJK gets little
of that:
- Chinese has no inflection, and its Hunspell data maps traditional/simplified forms
rather than inflections, so `stemmedMatch()` has nothing to work with.
- Japanese does have a morphology dictionary
([hunspell_ja_JP](https://github.com/yx-wh/hunspell_ja_JP)), but its purpose is
**de-inflection and mazegaki restoration**, not browsing words by their ending.
- Neither language uses spaces, so `expressionMatch()`'s compound splitting does not
apply.
So for CJK the prefix-only suggestion list is not "one weak option among several" —
it is the only option. That is why an O(log n) reverse index is worth building once,
instead of accepting the O(n) scan indefinitely.
This issue is only about **whether and where** to add such an index. It does not block
the suffix-search PR, which works correctly without it.
## What exists today
| Index | Built by | Notes |
| --- | --- | --- |
| Headword btree | the dictionary format itself | read-only for us; cannot be changed |
| `_HW_x` headword cache | GoldenDict-ng (`HeadwordCache`, v3) | sorted mmap file, built lazily in the background |
| `_FTS_x` full-text index | GoldenDict-ng (Xapian) | optional feature, expensive to rebuild, not present for every dictionary |
## Options
### A. Extend `_HW_x` to v4 with a reversed table
- One file, one build pass, both forward and backward lookups.
- `HeadwordCache::open()` already rejects a version mismatch, so a format bump would
simply invalidate and rebuild every existing cache — no user data is migrated, but
everyone pays one rebuild.
- File size grows: a second offset table plus the reversed strings (or one extra
offset table if strings are shared and only the ordering differs).
### B. Separate `_SW_x` file
- `_HW_x` stays untouched; if `_SW_x` is absent, suffix search falls back to the scan.
- Can be built, invalidated and deleted independently; users who never use suffix
mode never pay for it.
- Costs one more file and one more background builder to maintain.
### C. Reuse the Xapian FTS index with a `headword_rev` field
- Reuses existing machinery, but FTS is optional, must be built per dictionary, and is
expensive to rebuild. Headword lookup should not depend on the full-text index
existing, so this seems like the wrong layer.
## Questions
1. Is the linear scan acceptable for now, or is O(log n) worth the extra file/format?
(Happy to benchmark on a few million-headword dictionaries if useful.)
2. If yes, A or B?
3. Build lazily on the first suffix query, or together with `_HW_x`?
4. Any compatibility requirement for the `.hwcache` format that would rule out A?
## 中文说明
倒序检索(找以某个字符串**结尾**的词头)目前缺失(#2030)。#3160 里的实现是沿 btree
叶子链线性扫描,代价与现有通配符查询相同,不需要新增任何文件,所有 btree 后端都能用。
这不是小众需求:《现代汉语词典》(第 7 版·倒序本)2026 年出版,多字条目按末字归并,
比如 `动` 字下是"按兵不动、摆动、半自动、暴动、被动……"。《人民日报海外版》的报道
(2026-04-14):
https://paper.people.com.cn/rmrbhwb/pad/content/202604/14/content_30150842.html
### 为什么对中日文更值得
西文缺少倒序检索时,还有 `stemmedMatch`、Hunspell 拼写建议、`expressionMatch` 这些
「查不到之后的补救」;它们不能替代按词尾浏览,但至少还有一层缓冲。CJK 没有这层缓冲:
- 中文没有屈折变化,其 Hunspell 数据用于**繁简映射**而非词形还原,`stemmedMatch` 无从谈起;
- 日文有形态词典([hunspell_ja_JP](https://github.com/yx-wh/hunspell_ja_JP)),
但用途是**原形还原与 mazegaki 还原**,不是按词尾浏览;
- 两者都没有空格,`expressionMatch` 的复合词切分也用不上。
所以对 CJK 来说,只有前缀的候选列表不是「几个弱选项之一」,而是唯一选项。
这也是为什么值得把倒序索引建一次,长期换 O(log n),而不是一直接受 O(n) 扫描。
但对大词典来说这是 O(词头数)。倒序索引可以把它变成 O(log n):把词头反转后排序,
倒序查询就变成 `lower_bound(reverse(query))`——也就是纸质倒序词典的做法,搬进索引里。
这个 issue 只讨论**要不要做、以及放在哪里**,不阻塞倒序检索的 PR;没有这个索引,
PR 也能正确工作。
现有三类索引:词典自带的词头 btree(只读,不能改)、GD 生成的 `_HW_x` 词头缓存
(v3,按需后台构建)、GD 生成的 `_FTS_x` 全文索引(可选、重建昂贵)。
三个方案:
- **A. 把 `_HW_x` 升到 v4,加倒序表**:一个文件、一次构建;但版本号变更会让所有旧缓存
失效重建(不迁移用户数据,但每人都要重建一次),文件也更大。
- **B. 独立 `_SW_x` 文件**:`_HW_x` 不动,文件不存在就降级为线性扫描;可独立构建与删除,
不用倒序模式的用户不付代价;代价是多一个文件和一套构建逻辑。
- **C. 复用 Xapian FTS 加 `headword_rev` 字段**:能复用框架,但 FTS 是可选的、按词典构建、
重建昂贵,词头检索不该依赖全文索引存在,层级不对。
想请维护者定:线性扫描是否可接受、若要做选 A 还是 B、构建时机(首次倒序查询 vs 随 `_HW_x`
一起)、以及 `.hwcache` 格式是否有兼容性要求会排除 A。