我在以前的文章中介紹過 Elasticsearch的安裝和使用,這裏咱們使用Kibina做爲工具來操做es,可使用es的_analyze來分析分詞器的分詞結果。git
ES默認的分詞器爲英文分詞器,對英文句子能作到比較好的分詞,咱們看一個例子。當輸入如下請求時,對"What's your name"句子進行分詞,能看到將幾個詞都分了出來。github
POST _analyze
{
"tokenizer": "standard",
"text": "What's your name"
} 複製代碼
{
"tokens" : [
{
"token" : "What's",
"start_offset" : 0,
"end_offset" : 6,
"type" : "<ALPHANUM>",
"position" : 0
},
{
"token" : "your",
"start_offset" : 7,
"end_offset" : 11,
"type" : "<ALPHANUM>",
"position" : 1
},
{
"token" : "name",
"start_offset" : 12,
"end_offset" : 16,
"type" : "<ALPHANUM>",
"position" : 2
}
]
}複製代碼
當輸入中文"你叫什麼名字"時,能夠看到標準分詞器將句子分紅了一個一個的字,這顯然在咱們實際使用的過程當中是沒辦法接受的。bash
POST _analyze
{
"tokenizer": "standard",
"text": "你叫什麼名字"
}複製代碼
{
"tokens" : [
{
"token" : "你",
"start_offset" : 0,
"end_offset" : 1,
"type" : "<IDEOGRAPHIC>",
"position" : 0
},
{
"token" : "叫",
"start_offset" : 1,
"end_offset" : 2,
"type" : "<IDEOGRAPHIC>",
"position" : 1
},
{
"token" : "什",
"start_offset" : 2,
"end_offset" : 3,
"type" : "<IDEOGRAPHIC>",
"position" : 2
},
{
"token" : "麼",
"start_offset" : 3,
"end_offset" : 4,
"type" : "<IDEOGRAPHIC>",
"position" : 3
},
{
"token" : "名",
"start_offset" : 4,
"end_offset" : 5,
"type" : "<IDEOGRAPHIC>",
"position" : 4
},
{
"token" : "字",
"start_offset" : 5,
"end_offset" : 6,
"type" : "<IDEOGRAPHIC>",
"position" : 5
}
]
}
複製代碼
因爲英文句子都是使用空{ "tokens" : [ { "token" : "你", "start_offset" : 0, "end_offset" : 1, "type" : "CN_CHAR", "position" : 0 }, { "token" : "叫什麼", "start_offset" : 1, "end_offset" : 4, "type" : "CN_WORD", "position" : 1 }, { "token" : "名字", "start_offset" : 4, "end_offset" : 6, "type" : "CN_WORD", "position" : 2 } ] } 格進行分隔,所以在分詞比較明確,可是中文因爲語言特性,分詞比較難分,也容易產生分詞歧義,若是本身開發分詞器,成本會比較大,因此通常在使用過程當中都會用一些分詞器,比較著名的有Jieba分詞器,hanlp等,咱們這裏介紹一個es的插件分詞器,ik分詞器。能夠從github下載分詞器的壓縮包,下載地址: github.com/medcl/elast… ,在es的plugins目錄下建立一個ik的目錄,把解壓後的文件放到ik目錄下,而後重啓Elasticsearch。app
這時,咱們把以前的分詞器換成ik_smart,再來看效果。能夠看到用ik_smart已經可以將中文進行分詞。elasticsearch
POST _analyze
{
"tokenizer": "ik_smart",
"text": "你叫什麼名字"
}複製代碼
{
"tokens" : [
{
"token" : "你",
"start_offset" : 0,
"end_offset" : 1,
"type" : "CN_CHAR",
"position" : 0
},
{
"token" : "叫什麼",
"start_offset" : 1,
"end_offset" : 4,
"type" : "CN_WORD",
"position" : 1
},
{
"token" : "名字",
"start_offset" : 4,
"end_offset" : 6,
"type" : "CN_WORD",
"position" : 2
}
]
}
複製代碼
除了ik_smart以外,還有一個ik_max_wrod分詞器。工具
{
"tokens" : [
{
"token" : "中華人民共和國",
"start_offset" : 0,
"end_offset" : 7,
"type" : "CN_WORD",
"position" : 0
},
{
"token" : "中華人民",
"start_offset" : 0,
"end_offset" : 4,
"type" : "CN_WORD",
"position" : 1
},
{
"token" : "中華",
"start_offset" : 0,
"end_offset" : 2,
"type" : "CN_WORD",
"position" : 2
},
{
"token" : "華人",
"start_offset" : 1,
"end_offset" : 3,
"type" : "CN_WORD",
"position" : 3
},
{
"token" : "人民共和國",
"start_offset" : 2,
"end_offset" : 7,
"type" : "CN_WORD",
"position" : 4
},
{
"token" : "人民",
"start_offset" : 2,
"end_offset" : 4,
"type" : "CN_WORD",
"position" : 5
},
{
"token" : "共和國",
"start_offset" : 4,
"end_offset" : 7,
"type" : "CN_WORD",
"position" : 6
},
{
"token" : "共和",
"start_offset" : 4,
"end_offset" : 6,
"type" : "CN_WORD",
"position" : 7
},
{
"token" : "國",
"start_offset" : 6,
"end_offset" : 7,
"type" : "CN_CHAR",
"position" : 8
}
]
}
複製代碼
這兩種分詞器在應對具體的場景時,須要選擇合適的分詞器進行使用。post
通常狀況下,爲了提升搜索的效果,須要這兩種分詞器配合使用。既索引時用ik_max_word儘量多的分詞,而搜索時用ik_smart儘量提升匹配準度,讓用戶的搜索儘量的準確。好比一個常見的場景,就是搜索"進口紅酒"的時候,儘量的不要出現口紅相關商品或者讓口紅不要排在前面。優化
咱們首先在Elasticsearch內建立一個叫goods的索引,其中名字的分詞器用的是ik_max_word。spa
PUT /goods
{
"mappings":{
"goods": {
"properties": {
"id": {
"type": "keyword"
},
"name": {
"analyzer": "ik_max_word",
"type": "text"
}
}
}
},
"settings":{
"index": {
"refresh_interval": "1s",
"number_of_shards": 5,
"max_result_window": "10000000",
"mapper": {
"dynamic": "false"
},
"number_of_replicas": 0
}
}
}
複製代碼
而後咱們經過POST請求,往裏面添加一些數據。插件
POST /goods/goods
{
"id":"1",
"name":"美麗粉色口紅明星"
}
POST /goods/goods
{
"id":"2",
"name":"好喝的進口紅酒"
}
POST /goods/goods
{
"id":"3",
"name":"進口紅酒真好喝"
}
複製代碼
最後,在查詢的時候,咱們指定查詢分詞器爲ik_smart。
GET /goods/goods/_search
{
"query":{
"match": {
"name": {
"query": "進口紅酒",
"analyzer": "ik_smart"
}
}
}
}複製代碼
能夠看到兩條進口紅酒相關的記錄被搜了出來,可是口紅沒有被搜出來
{
"took" : 28,
"timed_out" : false,
"_shards" : {
"total" : 5,
"successful" : 5,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : 2,
"max_score" : 0.36464313,
"hits" : [
{
"_index" : "goods",
"_type" : "goods",
"_id" : "cdLk1WoBvRMfJWIKVfOP",
"_score" : 0.36464313,
"_source" : {
"id" : "3",
"name" : "進口紅酒真好喝"
}
},
{
"_index" : "goods",
"_type" : "goods",
"_id" : "ctLk1WoBvRMfJWIKX_O6",
"_score" : 0.36464313,
"_source" : {
"id" : "2",
"name" : "好喝的進口紅酒"
}
}
]
}
}
複製代碼
分詞器是Elasticsearch中很重要的一部分,網上也有不少開源的分詞器,對於通常的應用這些開源分詞器也許足夠用了,可是在某些特定的場景下,可能須要對分詞器作一些優化,甚至須要自研一些分詞器。