elasticsearch中如何使用ik中文分詞器

這篇文章給大家介紹elasticsearch中如何使用ik中文分詞器,內(nèi)容非常詳細,感興趣的小伙伴們可以參考借鑒,希望對大家能有所幫助。

我們提供的服務有:成都網(wǎng)站建設、成都網(wǎng)站設計、微信公眾號開發(fā)、網(wǎng)站優(yōu)化、網(wǎng)站認證、衡南ssl等。為成百上千企事業(yè)單位解決了網(wǎng)站和推廣的問題。提供周到的售前咨詢和貼心的售后服務,是有科學管理、有技術的衡南網(wǎng)站制作公司

1、從 github 上找到和本次 es 版本匹配上的 分詞器

# 下載地址
https://github.com/medcl/elasticsearch-analysis-ik/releases

2、使用 es 自帶的插件管理 elasticsearch-plugin 來進行安裝

  • 直接從網(wǎng)絡地址安裝

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

注意:
如果本地插件的路徑中存在空格,需要使用雙引號包裝起來。

3、重啟es

# 查找es進程
jps -l | grep 'Elasticsearch'
# 殺掉es進程
kill pid
# 啟動es
/Users/huan/soft/elastic-stack/es/es01/bin/elasticsearch -d -p pid01

三、測試 ik 分詞

ik分詞器提供了2種分詞的模式

  1. ik_max_word: 將需要分詞的文本做最小粒度的拆分,盡量分更多的詞。

  2. ik_smart: 將需要分詞的文本做最大粒度的拆分。

1、測試默認的分詞效果

語句

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
    }
  ]
}

可以看到默認的分詞器,對中文的分詞完全無法達到我們中文的分詞的效果。

2、測試 ik_max_word 的分詞效果

語句

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分詞可以達到我們需要的分詞效果。

3、測試 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" : 3,
      "type" : "CN_CHAR",
      "position" : 1
    },
    {
      "token" : "詞語",
      "start_offset" : 3,
      "end_offset" : 5,
      "type" : "CN_WORD",
      "position" : 2
    }
  ]
}

4、自定義 ik 的啟用詞和停用詞

1、找到 ik 的配置目錄
${IK_HOME}/config/analysis-ik
/Users/huan/soft/elastic-stack/es/es01/config/analysis-ik
2、修改 IKAnalyzer.cfg.xml 文件
<!--?xml version="1.0" encoding="UTF-8"?-->

<properties>
	<comment>IK Analyzer 擴展配置</comment>
	<!--用戶可以在這里配置自己的擴展字典 -->
	<entry key="ext_dict">custom-ext.dic</entry>
	 <!--用戶可以在這里配置自己的擴展停止詞字典-->
	<entry key="ext_stopwords">custom-stop.dic</entry>
	<!--用戶可以在這里配置遠程擴展字典 -->
	<!-- <entry key="remote_ext_dict">words_location</entry> -->
	<!--用戶可以在這里配置遠程擴展停止詞字典-->
	<!-- <entry key="remote_ext_stopwords">words_location</entry> -->
</properties>
3、custom-ext.dic 和 custom-stop.dic 的內(nèi)容

elasticsearch中如何使用ik中文分詞器

注意:
1、自定義分詞的文件必須是UTF-8的編碼。

4、配置文件完整路徑

elasticsearch中如何使用ik中文分詞器

5、查看分詞結(jié)果

elasticsearch中如何使用ik中文分詞器

5、熱更新IK分詞

1、修改 IKAnalyzer.cfg.xml 文件,配置遠程字典。

 $ 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 擴展配置</comment>
	<!--用戶可以在這里配置遠程擴展字典 -->
	<entry key="remote_ext_dict">http://localhost:8686/custom-ext.dic</entry>
	<!--用戶可以在這里配置遠程擴展停止詞字典-->
	<entry key="remote_ext_stopwords"></entry>
</properties>

注意:
1、此處的 custom-ext.dic 文件在下方將會配置到 nginx中,保證可以訪問。

2、http 請求需要返回兩個頭部(header),一個是 Last-Modified,一個是 ETag,這兩者都是字符串類型,只要有一個發(fā)生變化,該插件就會去抓取新的分詞進而更新詞庫。
3、http 請求返回的內(nèi)容格式是一行一個分詞,換行符用 \n 即可。 4、在 nginx 的目錄下放置一個 custom-ext.dic 文件

elasticsearch中如何使用ik中文分詞器

多次修改 custom-ext.dic 文件,可以看到分詞的結(jié)果也會實時變化,如此就實現(xiàn)了分詞的熱更新。

elasticsearch中如何使用ik中文分詞器

關于elasticsearch中如何使用ik中文分詞器就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,可以學到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到。

新聞名稱:elasticsearch中如何使用ik中文分詞器
鏈接地址:http://muchs.cn/article10/pdpjdo.html

成都網(wǎng)站建設公司_創(chuàng)新互聯(lián),為您提供外貿(mào)建站、電子商務網(wǎng)站內(nèi)鏈、營銷型網(wǎng)站建設、外貿(mào)網(wǎng)站建設、小程序開發(fā)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

h5響應式網(wǎng)站建設