這篇文章給大家介紹elasticsearch中如何使用ik中文分詞器,內(nèi)容非常詳細(xì),感興趣的小伙伴們可以參考借鑒,希望對(duì)大家能有所幫助。
我們提供的服務(wù)有:成都網(wǎng)站建設(shè)、成都網(wǎng)站設(shè)計(jì)、微信公眾號(hào)開發(fā)、網(wǎng)站優(yōu)化、網(wǎng)站認(rèn)證、衡南ssl等。為成百上千企事業(yè)單位解決了網(wǎng)站和推廣的問題。提供周到的售前咨詢和貼心的售后服務(wù),是有科學(xué)管理、有技術(shù)的衡南網(wǎng)站制作公司
# 下載地址 https://github.com/medcl/elasticsearch-analysis-ik/releases
直接從網(wǎng)絡(luò)地址安裝
cd /Users/huan/soft/elastic-stack/es/es02/bin # 下載插件 ./elasticsearch-plugin -v install https://github.com/medcl/elasticsearch-analysis-ik/releases/download/v7.12.0/elasticsearch-analysis-ik-7.12.0.zip # 查看插件是否下載成功 ./elasticsearch-plugin list
從本地安裝
cd /Users/huan/soft/elastic-stack/es/es02/bin # 下載插件(file后面跟的是插件在本地的地址) ./elasticsearch-plugin install file:///path/to/plugin.zip
注意:
如果本地插件的路徑中存在空格,需要使用雙引號(hào)包裝起來。
# 查找es進(jìn)程 jps -l | grep 'Elasticsearch' # 殺掉es進(jìn)程 kill pid # 啟動(dòng)es /Users/huan/soft/elastic-stack/es/es01/bin/elasticsearch -d -p pid01
ik
分詞器提供了2種分詞的模式
ik_max_word
: 將需要分詞的文本做最小粒度的拆分,盡量分更多的詞。
ik_smart
: 將需要分詞的文本做最大粒度的拆分。
語句
GET _analyze { "analyzer": "default", "text": ["中文分詞語"] }
結(jié)果
{ "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 } ] }
可以看到默認(rèn)的分詞器,對(duì)中文的分詞完全無法達(dá)到我們中文的分詞的效果。
語句
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" : 4, "type" : "CN_WORD", "position" : 1 }, { "token" : "詞語", "start_offset" : 3, "end_offset" : 5, "type" : "CN_WORD", "position" : 2 } ] }
可以看到基于ik
分詞可以達(dá)到我們需要的分詞效果。
語句
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" : 3, "type" : "CN_CHAR", "position" : 1 }, { "token" : "詞語", "start_offset" : 3, "end_offset" : 5, "type" : "CN_WORD", "position" : 2 } ] }
${IK_HOME}/config/analysis-ik /Users/huan/soft/elastic-stack/es/es01/config/analysis-ik
<!--?xml version="1.0" encoding="UTF-8"?--> <properties> <comment>IK Analyzer 擴(kuò)展配置</comment> <!--用戶可以在這里配置自己的擴(kuò)展字典 --> <entry key="ext_dict">custom-ext.dic</entry> <!--用戶可以在這里配置自己的擴(kuò)展停止詞字典--> <entry key="ext_stopwords">custom-stop.dic</entry> <!--用戶可以在這里配置遠(yuǎn)程擴(kuò)展字典 --> <!-- <entry key="remote_ext_dict">words_location</entry> --> <!--用戶可以在這里配置遠(yuǎn)程擴(kuò)展停止詞字典--> <!-- <entry key="remote_ext_stopwords">words_location</entry> --> </properties>
注意:
1、自定義分詞的文件必須是UTF-8
的編碼。
1、修改 IKAnalyzer.cfg.xml 文件,配置遠(yuǎn)程字典。
$ cat /Users/huan/soft/elastic-stack/es/es01/config/analysis-ik/IKAnalyzer.cfg.xml 11.87s ? 16.48G ? 2.68 ? <!--?xml version="1.0" encoding="UTF-8"?--> <properties> <comment>IK Analyzer 擴(kuò)展配置</comment> <!--用戶可以在這里配置遠(yuǎn)程擴(kuò)展字典 --> <entry key="remote_ext_dict">http://localhost:8686/custom-ext.dic</entry> <!--用戶可以在這里配置遠(yuǎn)程擴(kuò)展停止詞字典--> <entry key="remote_ext_stopwords"></entry> </properties>
注意:
1、此處的 custom-ext.dic
文件在下方將會(huì)配置到 nginx
中,保證可以訪問。
2、http 請(qǐng)求需要返回兩個(gè)頭部(header),一個(gè)是 Last-Modified,一個(gè)是 ETag,這兩者都是字符串類型,只要有一個(gè)發(fā)生變化,該插件就會(huì)去抓取新的分詞進(jìn)而更新詞庫。
3、http 請(qǐng)求返回的內(nèi)容格式是一行一個(gè)分詞,換行符用 \n 即可。 4、在 nginx 的目錄下放置一個(gè) custom-ext.dic
文件
多次修改 custom-ext.dic 文件,可以看到分詞的結(jié)果也會(huì)實(shí)時(shí)變化,如此就實(shí)現(xiàn)了分詞的熱更新。
關(guān)于elasticsearch中如何使用ik中文分詞器就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,可以學(xué)到更多知識(shí)。如果覺得文章不錯(cuò),可以把它分享出去讓更多的人看到。
新聞名稱:elasticsearch中如何使用ik中文分詞器
鏈接地址:http://aaarwkj.com/article10/pdpjdo.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供外貿(mào)建站、電子商務(wù)、網(wǎng)站內(nèi)鏈、營銷型網(wǎng)站建設(shè)、外貿(mào)網(wǎng)站建設(shè)、小程序開發(fā)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)