所有不說明elastic 版本的博客都是耍流氓 。 ——某碼農
版本如題。拼音和中文分詞一起的整個測試流程如下:
預備 刪除 index
DELETE /index_name/
{
}
創建一個 index_name 的 index
PUT /index_name/
{
"index": {
"analysis": {
"analyzer": {
"ik_pinyin_analyzer": {
"type": "custom",
"tokenizer": "ik_max_word",
"filter": ["my_pinyin", "word_delimiter"]
}
},
"filter": {
"my_pinyin": {
"type": "pinyin",
"first_letter": "prefix",
"padding_char": " "
}
}
}
}
}
修改 type 的 mapping
PUT /index_name/app/_mapping
{
"app": {
"properties": {
"ProductCName": {
"type": "keyword",
"fields": {
"pinyin": {
"type": "text",
"store": false,
"term_vector": "with_positions_offsets",
"analyzer": "ik_pinyin_analyzer",
"boost": 10
}
}
},
"ProductEName":{
"type":"text",
"analyzer": "ik_max_word"
},
"Description":{
"type":"text",
"analyzer": "ik_max_word"
}
}
}
}
創建測試數據
PUT /index_name/app/1
{
"ProductCName":"口紅世家",
"ProductEName":"Red History",
"Description":"口紅真是很棒的東西呢"
}
測試拼音分詞效果
POST /index_name/_analyze?pretty
{
"analyzer": "pinyin",
"text":"王者榮耀"
}
{
"tokens": [
{
"token": "wang",
"start_offset": 0,
"end_offset": 1,
"type": "word",
"position": 0
},
{
"token": "wzry",
"start_offset": 0,
"end_offset": 4,
"type": "word",
"position": 0
},
{
"token": "zhe",
"start_offset": 1,
"end_offset": 2,
"type": "word",
"position": 1
},
{
"token": "rong",
"start_offset": 2,
"end_offset": 3,
"type": "word",
"position": 2
},
{
"token": "yao",
"start_offset": 3,
"end_offset": 4,
"type": "word",
"position": 3
}
]
}
測試分詞
搜索數據
拼音搜索
在做分詞時遇到的問題
問題和解決
配置完成之後發現分詞不生效,查看 elastcsearch
啓動日誌發現 如下錯誤:
去看下 Ik
的配置文件發現:
修改 路徑 和 xml
結構 錯誤之後,重新加載了配置文件:
停止詞/分詞效果
查看口紅的 的搜索效果。發現搜索到了 含有 紅
這個字的結果
查看 口紅
的 分析器結果,發現 口紅
被 過多
的分詞了
修改 ik.stop.txt
添加 “口” 和 “紅”
elastic
重新加載了 ik
分詞配置文件
再次查看分析器效果,再去搜索 口紅的 ,已經搜不到 口
和 紅
的對應結果了
再比如:馬卡龍
會被 拆分成 馬
卡
龍
,添加 ik.txt
就可以讓分析器按自己的詞語來拆分