給已有索引加同義詞過濾器、或改
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
