ik分詞器
來源 :ES默認的分詞器是認為母語為英語鞍盗,英語的書寫格式是按照空格為詞津肛,并且連詞為AND THIS OR...嫉戚,中文的一句話是沒有空格的棒呛,中國文化博大精深連詞都不止其他語言的那么幾個唬滑,這就要有一個強大的中文詞庫饺鹃,當你的內(nèi)容中出現(xiàn)這個詞時,就會將這個詞提煉出來间雀。這里大家也不用重復(fù)造輪子悔详,經(jīng)過前輩的努力,這個中文的分詞器已經(jīng)有了惹挟,它就是今天要給大家介紹的IK中文分詞器
安裝
ES是默認沒有安裝ik分詞器的茄螃,如果工作中用的是阿里云服務(wù)的elasticsearch的話是會默認安裝,這里僅供自身學(xué)習(xí)连锯,所以可以在對應(yīng)GitHub上下載對應(yīng)的ik分詞器 :https://github.com/medcl/elasticsearch-analysis-ik/releases
附上另一博主的網(wǎng)盤归苍,有es7.6.1全家桶 https://blog.csdn.net/qq_42359314/article/details/105670912
下載完畢之后在es項目的${ES_HOME}/plugins
創(chuàng)建文件名為ik
的目錄用狱,隨后將zip解壓至該文件中
重啟es,觀察日志:
loaded plugin [analysis-ik]`
[2020-11-10T00:11:00,351][INFO ][o.w.a.d.Monitor ] [DESKTOP-FE6V42L] try load config from G:\elasticsearch\elasticsearch-7.6.1\config\analysis-ik\IKAnalyzer.cfg.xml
[2020-11-10T00:11:00,359][INFO ][o.w.a.d.Monitor ] [DESKTOP-FE6V42L] try load config from G:\elasticsearch\elasticsearch-7.6.1\plugins\ik\config\IKAnalyzer.cfg.xml
那么es已經(jīng)加載了上了ik分詞器
使用
先介紹一下ES的分析器接口拼弃,指定了分析器和文本的內(nèi)容夏伊,我們就可以看到分詞的結(jié)果
GET _analyze
{
"analyzer": {分析器名稱},
"text": {要被分詞的文檔}
}
結(jié)果
IK中文分詞器插件給我們提供了兩個分析器
- ik_max_word: 會將文本做最細粒度的拆分
- ik_smart:會做最粗粒度的拆分
分別看下各自的效果
對應(yīng)的 ik_smart
就分的比較粗了,只是大致的拆分了文本
GET _analyze
{
"analyzer": "ik_smart",
"text": "上海自來水來自海上"
}
結(jié)果:
{
"tokens" : [
{
"token" : "上海",
"start_offset" : 0,
"end_offset" : 2,
"type" : "CN_WORD",
"position" : 0
},
{
"token" : "自來水",
"start_offset" : 2,
"end_offset" : 5,
"type" : "CN_WORD",
"position" : 1
},
{
"token" : "來自",
"start_offset" : 5,
"end_offset" : 7,
"type" : "CN_WORD",
"position" : 2
},
{
"token" : "海上",
"start_offset" : 7,
"end_offset" : 9,
"type" : "CN_WORD",
"position" : 3
}
]
}
我們可以看到吻氧,ik_max_word
分詞分的非常細溺忧,我們在使用上面的這些進行搜索時,都可以搜索到對應(yīng)的文本
GET _analyze
{
"analyzer": "ik_max_word",
"text": "上海自來水來自海上"
}
結(jié)果
{
"tokens" : [
{
"token" : "上海",
"start_offset" : 0,
"end_offset" : 2,
"type" : "CN_WORD",
"position" : 0
},
{
"token" : "自來水",
"start_offset" : 2,
"end_offset" : 5,
"type" : "CN_WORD",
"position" : 1
},
{
"token" : "自來",
"start_offset" : 2,
"end_offset" : 4,
"type" : "CN_WORD",
"position" : 2
},
{
"token" : "水",
"start_offset" : 4,
"end_offset" : 5,
"type" : "CN_CHAR",
"position" : 3
},
{
"token" : "來自",
"start_offset" : 5,
"end_offset" : 7,
"type" : "CN_WORD",
"position" : 4
},
{
"token" : "海上",
"start_offset" : 7,
"end_offset" : 9,
"type" : "CN_WORD",
"position" : 5
}
]
}
配置
同樣盯孙,如果我們用ik分析器來分析 簡書
就會發(fā)現(xiàn)鲁森,本應(yīng)該是一個名字的文本,被分詞器分析成兩個字
GET _analyze
{
"analyzer": "ik_smart",
"text": "簡書"
}
GET _analyze
{
"analyzer": "ik_max_word",
"text": "簡書"
}
同樣的返回結(jié)果
{
"tokens" : [
{
"token" : "簡",
"start_offset" : 0,
"end_offset" : 1,
"type" : "CN_CHAR",
"position" : 0
},
{
"token" : "書",
"start_offset" : 1,
"end_offset" : 2,
"type" : "CN_CHAR",
"position" : 1
}
]
}
但是在日常工作中我們有許多的業(yè)務(wù)名詞振惰,我們不期望使用es搜索的時候被ik分詞器拆分成單個漢字歌溉,這時候我們就需要來配置ik分詞器的字典:
配置項如下: ${ES_HOME}\plugins\ik\config\IKAnalyzer.cfg.xml
<properties>
<comment>IK Analyzer 擴展配置</comment>
<!--用戶可以在這里配置自己的擴展字典 -->
<entry key="ext_dict"></entry>
<!--用戶可以在這里配置自己的擴展停止詞字典-->
<entry key="ext_stopwords"></entry>
<!--用戶可以在這里配置遠程擴展字典 -->
<!-- <entry key="remote_ext_dict">words_location</entry> -->
<!--用戶可以在這里配置遠程擴展停止詞字典-->
<!-- <entry key="remote_ext_stopwords">words_location</entry> -->
</properties>
- 字典 :擴展字典中的詞會被篩選出來
- 停止詞字典 :擴展停止詞中的詞會被過濾掉
即可以在\IKAnalyzer.cfg.xml
的相對目錄下新建對應(yīng)的定制字典 如biz.dic
一定要注意dic的編碼格式
簡書
我是簡書
博主
配置進xml中<entry key="ext_dict">biz.dic</entry>
重啟es生效,隨后就會看到文本的分詞不一樣了
被加載:
[2020-11-10T00:49:27,608][INFO ][o.w.a.d.Monitor ] [DESKTOP-FE6V42L] [Dict Loading] G:\elasticsearch\elasticsearch-7.6.1\plugins\ik\config\biz.dic
輸出的結(jié)果:
{
"tokens" : [
{
"token" : "簡書",
"start_offset" : 0,
"end_offset" : 2,
"type" : "CN_WORD",
"position" : 0
}
]
}
創(chuàng)建索引時指定IK分詞器
我們在創(chuàng)建索引時就可以為text類型的字段指定IK中文分詞器了
PUT ik_index
{
"mappings": {
"properties": {
"id": {
"type": "long"
},
"title": {
"type": "text",
"analyzer": "ik_max_word"
}
}
}
}
為索引指定默認IK分詞器
PUT ik_index
{
"settings": {
"analysis": {
"analyzer": {
"default": {
"type": "ik_max_word"
}
}
}
}
}
以上就是個人理解整理的ik分詞器內(nèi)容
附上精彩文章:
[ik分詞器詞典加載原理] (http://www.reibang.com/p/e7f80fda5dd4?utm_campaign=maleskine&utm_content=note&utm_medium=seo_notes&utm_source=recommendation)