1. 程式人生 > >Elasticsearch 中文分詞器IK

Elasticsearch 中文分詞器IK

1、安裝說明

https://github.com/medcl/elasticsearch-analysis-ik

2、release版本

https://github.com/medcl/elasticsearch-analysis-ik/releases

3、安裝外掛

bin/elasticsearch-plugin install https://github.com/medcl/elasticsearch-analysis-ik/releases/download/v6.5.1/elasticsearch-analysis-ik-6.1.1.zip
[[email protected]
elasticsearch-6.5.1]$ ll plugins/analysis-ik/ 總用量 1428 -rw-r--r-- 1 es es 263965 12月 12 10:30 commons-codec-1.9.jar -rw-r--r-- 1 es es 61829 12月 12 10:30 commons-logging-1.2.jar -rw-r--r-- 1 es es 54693 12月 12 10:30 elasticsearch-analysis-ik-6.5.1.jar -rw-r--r-- 1 es es 736658 12月 12 10:30 httpclient-4.5.2.jar -rw-r--r-- 1 es es 326724 12月 12 10:30 httpcore-4.4.4.jar -rw-r--r-- 1 es es 1805 12月 12 10:30 plugin-descriptor.proper

也可以自己下載包之後解壓縮,copy到plugins下即可
4、擴充套件詞庫

在es目錄下config/analysis-ik/中

新建自己的詞庫,utf8編碼

mkdir mydic
vi myword001.dic
魔獸世界
李雲龍
嫦娥

修改配置檔案

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE properties SYSTEM "http://java.sun.com/dtd/properties.dtd">
<properties>
	<comment>IK Analyzer 擴充套件配置</comment>
	<!--使用者可以在這裡配置自己的擴充套件字典 -->
	<entry key="ext_dict">mydic/myword001.dic</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 can be located at {conf}/analysis-ik/config/IKAnalyzer.cfg.xml or {plugins}/elasticsearch-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 擴充套件配置</comment>
	<!--使用者可以在這裡配置自己的擴充套件字典 -->
	<entry key="ext_dict">custom/mydict.dic;custom/single_word_low_freq.dic</entry>
	 <!--使用者可以在這裡配置自己的擴充套件停止詞字典-->
	<entry key="ext_stopwords">custom/ext_stopword.dic</entry>
 	<!--使用者可以在這裡配置遠端擴充套件字典 -->
	<entry key="remote_ext_dict">location</entry>
 	<!--使用者可以在這裡配置遠端擴充套件停止詞字典-->
	<entry key="remote_ext_stopwords">http://xxx.com/xxx.dic</entry>
</properties>

測試:

GET _analyze
{
  "analyzer": "ik_smart",
  "text": "魔獸世界"
}

{
  "tokens" : [
    {
      "token" : "魔獸世界",
      "start_offset" : 0,
      "end_offset" : 4,
      "type" : "CN_WORD",
      "position" : 0
    }
  ]
}
GET _analyze
{
  "analyzer": "ik_max_word",
  "text": "魔獸世界"
}

{
  "tokens" : [
    {
      "token" : "魔獸世界",
      "start_offset" : 0,
      "end_offset" : 4,
      "type" : "CN_WORD",
      "position" : 0
    },
    {
      "token" : "魔獸",
      "start_offset" : 0,
      "end_offset" : 2,
      "type" : "CN_WORD",
      "position" : 1
    },
    {
      "token" : "世界",
      "start_offset" : 2,
      "end_offset" : 4,
      "type" : "CN_WORD",
      "position" : 2
    }
  ]
}
ik_smart 是粗粒度分詞,分過的詞不在參與分詞。
ik_max_word是細粒度分詞,根據可能的詞進行組合.