- elastic默認的分詞插件對中文支持不好寄啼,比如對
中華人民共和國
進行分詞的時候:
1.使用默認分詞插件纽哥,會分別搜索中钠乏、華、人春塌、民晓避、共、和只壳、國
2. 推薦使用大名鼎鼎的ik分詞器俏拱, 地址:https://github.com/medcl/elasticsearch-analysis-ik/
3. 安裝方法如前面的一片文章所述,建議使用ik_smarter就夠了,上面的分詞會變成: 中華吼句、人民锅必、共和國
4. 使用ik之后,還是有一個問題惕艳,對于中文用戶搞隐,很多人在搜索的時候不一定會切換到中文的輸入法,也就是輸入的是拼音尔艇, 但是ik并不支持拼音搜索尔许,這樣搜出來的結(jié)果是英文或者是不準確的,所以需要使用拼音分詞插件:https://github.com/medcl/elasticsearch-analysis-pinyin
5. 安裝方法依舊如前面所述
- 安裝插件之后终娃,需要重新更新一下mapping味廊,以實現(xiàn)拼音+多音字的搜索結(jié)果:
topic = \
{
"settings": {
"analysis": {
"analyzer": {
"ik_pinyin_analyzer": {
"type":"custom",
"tokenizer": "ik_smart",
"filter": ["my_pinyin","word_delimiter"]
}
},
"filter": {
"my_pinyin": {
"type": "pinyin",
"keep_first_letter": False,
"keep_full_pinyin": True,
"keep_none_chinese": True,
"keep_none_chinese_in_first_letter": True,
"keep_original": False,
"limit_first_letter_length": 16,
"lowercase": True,
"trim_whitespace": True,
}
}
}
},
"mappings" : {
"topic" : {
"properties" : {
"creator" : {
"type" : "string",
"index": "not_analyzed"
},
"postCount" : {
"type" : "integer",
"index": "not_analyzed"
},
"followNum" : {
"type" : "integer",
"index": "not_analyzed"
},
"creatTime" : {
"type" : "date",
"index": "not_analyzed"
},
"tagName": {
"type": "text",
"index": "analyzed",
"store": "no",
"analyzer": "ik_pinyin_analyzer",
"term_vector": "with_positions_offsets",
"boost": 10,
"fields" : {
"untouch": {
"type": "keyword"
}
}
}
}
}
}
}
-
搜索實現(xiàn)的結(jié)果如下圖所示: