網(wǎng)上大多數(shù)都是 es5 的配置方法, 關(guān)于 es6 的同義詞配置 斷斷續(xù)續(xù)研究了好幾天,今天本地 服務(wù)器上,終于配成功了,記錄一下!
1. 創(chuàng)建一個(gè)索引
curl -v -X PUT localhost:9200/station_index
2. close index,因?yàn)槲覀兘酉聛硪獙υ撍饕O(shè)置
curl -XPOST 'localhost:9200/station_index/_close'
3. 配置ik同義詞
curl -l -H "Content-Type:application/json" -H "Accept:application/json" -X PUT 'http://localhost:9200/station_index/_settings?preserve_existing=true' -d '{
"index.analysis.analyzer.ik_syno.filter" : [
"my_synonym_filter"
],
"index.analysis.analyzer.ik_syno.tokenizer" : "ik_max_word",
"index.analysis.analyzer.ik_syno.type" : "custom",
"index.analysis.analyzer.ik_syno_smart.filter" : [
"my_synonym_filter"
],
"index.analysis.analyzer.ik_syno_smart.tokenizer" : "ik_smart",
"index.analysis.analyzer.ik_syno_smart.type" : "custom",
"index.analysis.filter.my_synonym_filter.synonyms_path" : "synonyms.txt",
"index.analysis.filter.my_synonym_filter.type" : "synonym"
}'
如 synonyms.txt 發(fā)生變動,重啟 es 服務(wù)器即可生效
我遇到的問題:
問題1:這里說一下,網(wǎng)上很多博客 修改 elasticsearch.yml 的做法,在 es6 中無法使用,例如:


es 服務(wù)器 告訴我 ,自從5.x 以后他們就不讓這么做啦,只能通過訪問 API 來設(shè)置,好吧,我直接復(fù)制改一下索引搞定
問題2:"type":"malformed_input_exception","reason":"Input length = 1"
詳細(xì)信息如下
{"error":{"root_cause":[{"type":"illegal_argument_exception","reason":"failed to build synonyms"}],
"type":"illegal_argument_exception","reason":"failed to build synonyms",
"caused_by":{"type":"malformed_input_exception","reason":"Input length = 1"}},"status":400}
原因:這個(gè)問題是由于 synonyms.txt 解析錯誤造成
1)檢查你的語法配置是否如下
程王, 程王線 => 程王
2)編碼 UTF-8 (很關(guān)鍵!??!我就是被坑在這里)

4. open index
curl -XPOST 'localhost:9200/station_index/_open'
5. 測試
curl -l -H "Content-Type:application/json" -H "Accept:application/json" -X POST 'http://localhost:9200/station_index/_analyze' -d '{
"analyzer" : "ik_syno_smart",
"text" : "程王線"
}'

到這里已經(jīng)大功告成了!
下面再把我的映射貼出來,有需要的可以參考
curl -l -H "Content-Type:application/json" -H "Accept:application/json" -X POST 'localhost:9200/station_index/station/_mapping' -d '{
"properties": {
"name": {
"type": "text",
"analyzer": "ik_syno_smart",
"search_analyzer": "ik_syno_smart"
}
}
}'