把某个词加进 stop-words 之后搜索它仍能命中文档,多半不是设置没生效,而是停用词一个都没匹配上。Meilisearch 的停用词比对有两个硬条件:大小写敏感(
the≠The),且按分词后的词逐个精确比对(中文里填整句不等于 jieba 切出的任何词)。先把任务轮询到succeeded(停用词改动触发全量重索引),再按文档里词的实际形态逐个填写,才能生效。适用 Meilisearch 全版本。
Issue & Env(案发现场与环境)
- 业务场景 A:英文书名字段里有
The Three-Body Problem,想把定冠词干掉,往 stop-words 里填了小写the,重索引完成后搜the,文档照样命中。 - 业务场景 B:中文书籍索引里想剔除「的」,却把整句「平凡的世界」填进了 stop-words,搜「的」仍命中 5 篇。
- 环境:Meilisearch v1.54.3,curl 复现;以下输出均为本地实测。
- 关键区分:先确认
settingsUpdate任务已succeeded(排除异步任务没等完),再确认填的词和文档里词的形态——本 case 处理的是后者。
StackTrace(堆栈日志)
填了小写 the 并等任务 succeeded 后,搜 the 仍命中:
{"hits":[{"id":1,"title":"The Three-Body Problem"}],"query":"the","processingTimeMs":0,"limit":20,"offset":0,"estimatedTotalHits":1}
读回设置,值确实写进去了:
["the"]
中文场景类似:stop-words 填了 ["平凡的世界"],任务成功后搜「的」,命中数一篇没少:
{
"hits": [
{ "id": 5, "title": "平凡的世界" },
{ "id": 4, "title": "甜品店创业记" },
{ "id": 6, "title": "月亮和六便士" },
{ "id": 1, "title": "活着" },
{ "id": 3, "title": "夏日冰事" }
],
"query": "的",
"estimatedTotalHits": 5
}
Reproduce(复现代码)
# 1. 造一条含大写 The 的文档(索引创建与任务轮询套路从略)
curl -X POST 'http://localhost:7700/indexes/books_stop/documents' \
-H 'Authorization: Bearer aSampleMasterKey' \
-H 'Content-Type: application/json' \
--data-binary '[{"id":1,"title":"The Three-Body Problem"}]'
# 2. 停用词填小写 the
curl -X PUT 'http://localhost:7700/indexes/books_stop/settings/stop-words' \
-H 'Authorization: Bearer aSampleMasterKey' \
-H 'Content-Type: application/json' \
-d '["the"]'
# 返回:{"taskUid": N, "type": "settingsUpdate", ...},轮询到 succeeded
# 3. 搜 the —— 仍然命中 id 1
curl -X POST 'http://localhost:7700/indexes/books_stop/search' \
-H 'Authorization: Bearer aSampleMasterKey' \
-H 'Content-Type: application/json' \
-d '{"q":"the"}'
把停用词改成大写 The 再跑一遍,同一条查询返回 "estimatedTotalHits": 0。
Root Cause(根因分析)
停用词比对大小写敏感
官方 Configure stop words 文档明确写了停用词是 case-sensitive 的:列表里的 the 不会匹配文档中的 The。机制上,停用词过滤发生在索引期,分词器切出词之后按原始形态与停用词列表精确比对,不像普通查询匹配那样做大小写归一。文档里的 The 和小写停用词 the 不相等,于是 The 照常进入倒排索引;而查询匹配本身是大小写不敏感的,搜 the 依然能命中词形为 The 的文档——「设了跟没设一样」就是这么来的。
停用词作用在分词后的词上,不是一个子串
停用词比对的对象是分词器(Charabia,中文走 jieba)切出来的单个词,不是对原文做子串删除。「平凡的世界」切出来是「平凡 / 的 / 世界」三个词,停用词列表里的「平凡的世界」不等于其中任何一个,整条配置匹配零个词,等于白配。同理,想剔除英文里的撇号形式、带重音的形式,都要按切词后的实际形态填。
生效前提是重索引完成
停用词在索引期剔除,改设置触发全量重索引。任务还在 enqueued / processing 时搜到的仍是旧索引,也会呈现「不生效」的假象——这是另一种根因,排查时先用 GET /tasks/{taskUid} 把它排除掉。
The Fix(解决方案)
第一步:确认任务已 succeeded
curl 'http://localhost:7700/tasks/{taskUid}' -H 'Authorization: Bearer aSampleMasterKey'
第二步:按文档里词的实际形态填写
- 英文:数据集里是大写
The就填The;两种形态都有就两条都填:["the","The"]。 - 中文:填 jieba 切词结果里的那个词,剔除「的」就填
的:
curl -X PUT 'http://localhost:7700/indexes/books_dict/settings/stop-words' \
-H 'Authorization: Bearer aSampleMasterKey' \
-H 'Content-Type: application/json' \
-d '["的"]'
第三步:等任务完成后双向验证
# 只含停用词的查询应返回 0 命中
curl -X POST 'http://localhost:7700/indexes/books_dict/search' \
-H 'Authorization: Bearer aSampleMasterKey' -H 'Content-Type: application/json' \
-d '{"q":"的"}'
# 含其他词的查询不受影响
curl -X POST 'http://localhost:7700/indexes/books_dict/search' \
-H 'Authorization: Bearer aSampleMasterKey' -H 'Content-Type: application/json' \
-d '{"q":"平凡的世界"}'
实测结果:前者 "estimatedTotalHits": 0,后者仍命中 id 5。
注意事项
- Meilisearch 没有内置停用词表,默认是空集
[],所有停用词都要自己维护。 - 拿不准文档里词的实际形态时,用「设一个、搜一次」的对照实验验证,比猜切词结果可靠。
- 停用词在短语搜索里同样被忽略:
"the great gatsby"与"great gatsby"等价。 - 查询只含停用词时返回 0 命中,这是预期行为,别当成搜索坏了。
References(参考来源)
- Configure stop words — Meilisearch 官方文档meilisearch-docs
- Update stop words — Meilisearch API 参考meilisearch-docs
- Tokenization — Meilisearch 官方文档meilisearch-docs
