给已有索引加同义词过滤器、或改
analysis设置后,搜新词还是搜不到旧文档,行为跟没改一样。根因不是配置写错,而是分析器作用在索引期:改分析器要 close 索引,而且已经写入的旧文档不会自动用新分析器重新分词,必须 reindex。适用 OpenSearch 全版本。
Issue & Env(案发现场与环境)
- 业务场景:跟着 从零跑通 OpenSearch 同义词与停用词 做完,后来想给
movies_dict的synopsis字段再加一组同义词"movie, motion picture"。直接PUT /movies_dict/_settings更新 analysis,然后搜「motion picture」,旧文档一条都不出来。 - 环境:OpenSearch 3.x(Docker latest),单节点,索引
movies_dict,synopsis用自定义分析器text_analyzer,security 开启。 - 关键区分:设置可能写进去了(
GET /movies_dict/_settings能读到),但旧文档的分词结果还是旧的。
StackTrace(报错原文 / 错误现象)
给未关闭的索引直接改 analysis,OpenSearch 会拒绝:
{
"error": {
"type": "illegal_argument_exception",
"reason": "Can't update non dynamic settings [[index.analysis.analyzer.text_analyzer...]] for open indices"
}
}
如果先把索引 close 再改,设置能写进去、也不报错,但搜新同义词依然命中不了旧文档——这是更隐蔽的第二段:设置生效了,索引里的词没重建。
Reproduce(复现代码)
# ① 对 open 索引直接改 analysis → 报错
curl -sk -u admin:Devfix@2026 -X PUT "https://localhost:9200/movies_dict/_settings?pretty" \
-H "Content-Type: application/json" \
-d '{ "analysis": { "filter": { "my_synonyms": { "type": "synonym", "synonyms": ["film, movie, motion picture"] } } } }'
# ② close 后改 settings → 能写入
curl -sk -u admin:Devfix@2026 -X POST "https://localhost:9200/movies_dict/_close?pretty"
curl -sk -u admin:Devfix@2026 -X PUT "https://localhost:9200/movies_dict/_settings?pretty" \
-H "Content-Type: application/json" \
-d '{ "analysis": { "filter": { "my_synonyms": { "type": "synonym", "synonyms": ["film, movie, motion picture"] } } } }'
curl -sk -u admin:Devfix@2026 -X POST "https://localhost:9200/movies_dict/_open?pretty"
# ③ 搜新同义词「motion picture」→ 旧文档仍不命中(词没重建)
curl -sk -u admin:Devfix@2026 -X GET "https://localhost:9200/movies_dict/_search?pretty" \
-H "Content-Type: application/json" \
-d '{ "query": { "match": { "synopsis": "motion picture" } } }'
判定标准:GET /movies_dict/_settings 里能看到新 filter,但搜新同义词命中 0——就是「设置写了、词没重建」。
Root Cause(根因分析)
分析器在索引期分词,是「一次性」动作
同义词是分析器里的 token filter。文档写入时,分析器把 synopsis 切成 token 存进倒排索引;「film」那一刻被扩展成「film + movie」写进了倒排索引。之后改分析器,只改变「以后写入的文档」怎么分词,已经写进去的 token 不会自动重算。
改 analysis 是「非动态」设置,要先 close
分析器(index.analysis.*)属于非动态索引设置,open 状态下改会报 Can't update non dynamic settings。正确顺序是 _close → PUT _settings → _open。这一步只是让设置写入成功,和旧文档重建是两码事。
旧文档必须 reindex 才吃新分析器
要让旧文档按新分析器重新分词,必须 _reindex 重建(reindex 不能写回同一索引,需建新索引再从旧索引迁数据)。只 close/reopen 不会触发任何分词重算。
The Fix(解决方案)
方案一:重建索引 + reindex(标准做法)
# 1) 建新索引,带完整新分析器
curl -sk -u admin:Devfix@2026 -X PUT "https://localhost:9200/movies_dict_v2?pretty" \
-H "Content-Type: application/json" \
-d '{
"settings": { "analysis": { "filter": { "my_synonyms": { "type": "synonym", "synonyms": ["film, movie, motion picture"] }, "my_stop": { "type": "stop", "stopwords": "_english_" } }, "analyzer": { "text_analyzer": { "type": "custom", "tokenizer": "standard", "filter": ["lowercase", "my_synonyms", "my_stop"] } } } },
"mappings": { "properties": { "synopsis": { "type": "text", "analyzer": "text_analyzer" } } }
}'
# 2) 旧索引迁到新索引
curl -sk -u admin:Devfix@2026 -X POST "https://localhost:9200/_reindex?pretty" \
-H "Content-Type: application/json" \
-d '{ "source": { "index": "movies_dict" }, "dest": { "index": "movies_dict_v2" } }'
# 3) 验证 + 切名(删旧、别名指向新,或直接删旧)
curl -sk -u admin:Devfix@2026 -X DELETE "https://localhost:9200/movies_dict?pretty"
方案二:close → 改 settings → reopen(只影响新文档)
如果只关心「以后写入的文档」,close/reopen 改 settings 即可;但旧文档不会变,别指望它。
注意事项
- 改分析器前先想清楚要不要旧数据:要,走 reindex;不要,close/reopen 足够。
- reindex 不能写回源索引,必须换目标索引。
- 完整同义词/停用词配置见 从零跑通 OpenSearch 同义词与停用词。
References(参考来源)
- Synonym token filter — OpenSearch 官方文档opensearch-docs
- Search analyzers — OpenSearch 官方文档opensearch-docs
- Reindex — OpenSearch API 参考opensearch-docs
