1. 背景
Elasticsearch默認(rèn)的分詞器是standard,其對(duì)中文的分詞是按字拆分,不智能。例如,輸入“美麗的中國(guó)”
GET _analyze
{
"analyzer": "standard",
"text": "美麗的中國(guó)"
}
對(duì)應(yīng)的分詞響應(yīng):
{
"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" : "國(guó)",
"start_offset" : 4,
"end_offset" : 5,
"type" : "<IDEOGRAPHIC>",
"position" : 4
}
]
}
接下來(lái)介紹并集成Elasticsearch的插件elasticsearch-analysis-ik進(jìn)行中文分詞。
2. 下載安裝包
根據(jù)Elasticsearch的版本下載對(duì)應(yīng)的IK插件包,安裝包下載地址如下: https://github.com/medcl/elasticsearch-analysis-ik/releases

7.6.2插件包
3. 解壓并安裝
解壓安裝包,并復(fù)制文件夾到$ES_HOME/plugins目錄下,重命名文件夾為analysis-ik, 如圖所示:

4. 重啟elasticsearch并驗(yàn)證
重啟Elasticsearch后,在Kibana控制臺(tái)驗(yàn)證,使用IK分詞器來(lái)分詞,如圖所示:

ik_smart分詞器
- elasticsearch-analysis-ik插件支持兩種分析器:ik_max_word、ik_smart。
- ik_max_word: 會(huì)將文本做最細(xì)粒度的拆分,會(huì)窮盡各種可能的組合,適合 Term Query;
- ik_smart: 會(huì)做最粗粒度的拆分,比如會(huì)將“中華人民共和國(guó)國(guó)歌”拆分為“中華人民共和國(guó),國(guó)歌”,適合 Phrase 查詢(xún)。
5. 熱更新IK分詞使用方法
elasticsearch-analysis-ik提供了熱更新IK分詞詞庫(kù)的功能。
打開(kāi)$ES_HOME/plugins/analysis-ik/config/IKAnalyzer.cfg.xml配置文件,配置文件如下:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE properties SYSTEM "http://java.sun.com/dtd/properties.dtd">
<properties>
<comment>IK Analyzer 擴(kuò)展配置</comment>
<!--用戶(hù)可以在這里配置自己的擴(kuò)展字典,支持多個(gè)文件,多個(gè)用;隔開(kāi) -->
<entry key="ext_dict"></entry>
<!--用戶(hù)可以在這里配置自己的擴(kuò)展停止詞字典-->
<entry key="ext_stopwords"></entry>
<!--用戶(hù)可以在這里配置遠(yuǎn)程擴(kuò)展字典 -->
<!-- <entry key="remote_ext_dict">words_location</entry> -->
<!--用戶(hù)可以在這里配置遠(yuǎn)程擴(kuò)展停止詞字典-->
<!-- <entry key="remote_ext_stopwords">words_location</entry> -->
</properties>
- 只需配置參數(shù)remote_ext_dict和remote_ext_stopwords的值,其中 words_location 是指一個(gè) url,比如http://yoursite.com/getCustomDict,該請(qǐng)求只需滿(mǎn)足以下兩點(diǎn)即可完成分詞熱更新。
(1)該 http 請(qǐng)求需要返回兩個(gè)頭部(header),一個(gè)是 Last-Modified,一個(gè)是 ETag,這兩者都是字符串類(lèi)型,只要有一個(gè)發(fā)生變化,該插件就會(huì)去抓取新的分詞進(jìn)而更新詞庫(kù)。
(2)該 http 請(qǐng)求返回的內(nèi)容格式是一行一個(gè)分詞,內(nèi)容UTF-8字符集編碼,換行符用 \n。
滿(mǎn)足上面兩點(diǎn)要求就可以實(shí)現(xiàn)熱更新分詞了,不需要重啟 ES 實(shí)例。
6. IK分詞使用
6.1 創(chuàng)建索引 index_test_01
# 指定mappings 創(chuàng)建索引 index_test_01
PUT /index_test_01
{
"mappings": {
"properties": {
"content": {
"type": "text",
"analyzer": "ik_max_word",
"search_analyzer": "ik_smart"
}
}
}
}
6.2 索引數(shù)據(jù)
POST /index_test_01/_create/1
{"content":"紅豆生南國(guó),春來(lái)發(fā)幾枝。愿君多采擷,此物最相思。"}
POST /index_test_01/_create/2
{"content":"日照香爐生紫煙,遙看瀑布掛前川。飛流直下三千尺,疑是銀河落九天。"}
POST /index_test_01/_create/3
{"content":"故人西辭黃鶴樓,煙花三月下?lián)P州。孤帆遠(yuǎn)影碧空盡,唯見(jiàn)長(zhǎng)江天際流。"}
POST /index_test_01/_create/4
{"content":"樓倚霜樹(shù)外,鏡天無(wú)一毫。南山與秋色,氣勢(shì)兩相高。"}
6.3 數(shù)據(jù)搜索
查詢(xún)索引index_test_01、字段content中包含"黃鶴樓"的文檔,示例如下:
GET /index_test_01/_search
{
"query": {
"match": {
"content": "黃鶴樓"
}
}
}
返回文檔數(shù)據(jù)如下:
{
"took" : 5,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 1,
"relation" : "eq"
},
"max_score" : 0.92011774,
"hits" : [
{
"_index" : "index_test_01",
"_type" : "_doc",
"_id" : "3",
"_score" : 0.92011774,
"_source" : {
"content" : "故人西辭黃鶴樓,煙花三月下?lián)P州。孤帆遠(yuǎn)影碧空盡,唯見(jiàn)長(zhǎng)江天際流。"
}
}
]
}
}
返回匹配的文檔數(shù)據(jù),表示搜索時(shí)分詞和索引時(shí)分詞一樣使用了IK分詞器分詞。