Elasticsearch分詞插件——IK Analysis

IK分詞插件

IK Analyzer是一個(gè)開(kāi)源的,基于java語(yǔ)言開(kāi)發(fā)的輕量級(jí)的中文分詞工具包。IK Analysis插件將Lucene IK分析器(http://code.google.com/p/ik-analyzer/)集成到elasticsearch中,支持自定義詞典。是一款基于詞典和規(guī)則的中文分詞器。
Github地址:https://github.com/medcl/elasticsearch-analysis-ik

IK分詞插件版本和Elasticsearch版本對(duì)應(yīng)

IK version ES version
master 7.x -> master
6.x 6.x
5.x 5.x
1.10.6 2.4.6
1.9.5 2.3.5
1.8.1 2.2.1
1.7.0 2.1.1
1.5.0 2.0.0
1.2.6 1.0.0
1.2.5 0.90.x
1.1.3 0.20.x
1.0.0 0.16.2 -> 0.19.0

IK Analysis下載

下載地址:https://github.com/medcl/elasticsearch-analysis-ik/releases
下載壓縮包到Elasticsearch安裝目錄的/plugins/ik文件夾并解壓

image.png

ES版本是2.X需要在conf/elasticsearch.yml加入index.analysis.analyzer.ik.type: ik,5.X版本的不需要進(jìn)行任何的配置

啟動(dòng)Elasticsearch,可以看到有讀取ik配置文件

image.png

IK分詞的原理與測(cè)試

IK 的 ik_smartik_max_word 兩種分詞策略

分詞的測(cè)試使用curl或者postman都可以,我個(gè)人傾向于postman,主要是能保存,要方便一些。

這里我直接在Elasticsearch可視化工具kibana的控制臺(tái)上操作

默認(rèn)的分詞策略standard

GET _analyze
{
  "text": "共和國(guó)國(guó)歌"
}

分詞結(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" : "國(guó)",
      "start_offset" : 2,
      "end_offset" : 3,
      "type" : "<IDEOGRAPHIC>",
      "position" : 2
    },
    {
      "token" : "國(guó)",
      "start_offset" : 3,
      "end_offset" : 4,
      "type" : "<IDEOGRAPHIC>",
      "position" : 3
    },
    {
      "token" : "歌",
      "start_offset" : 4,
      "end_offset" : 5,
      "type" : "<IDEOGRAPHIC>",
      "position" : 4
    }
  ]
}
image.png

ik_smart分詞策略(智能模式):會(huì)做最粗粒度的拆分,用于搜索,更精確的搜索到想要的結(jié)果

GET _analyze
{
  "analyzer": "ik_smart",
  "text": "共和國(guó)國(guó)歌"
}
{
  "tokens" : [
    {
      "token" : "共和國(guó)",
      "start_offset" : 0,
      "end_offset" : 3,
      "type" : "CN_WORD",
      "position" : 0
    },
    {
      "token" : "國(guó)歌",
      "start_offset" : 3,
      "end_offset" : 5,
      "type" : "CN_WORD",
      "position" : 1
    }
  ]
}
image.png

ik_max_word分詞策略(細(xì)粒度模式):會(huì)將文本做最細(xì)粒度的拆分,多用于索引,最大化的將文章內(nèi)容分詞

GET _analyze
{
  "analyzer": "ik_max_word",
  "text": "共和國(guó)國(guó)歌"
}
{
  "tokens" : [
    {
      "token" : "共和國(guó)",
      "start_offset" : 0,
      "end_offset" : 3,
      "type" : "CN_WORD",
      "position" : 0
    },
    {
      "token" : "共和",
      "start_offset" : 0,
      "end_offset" : 2,
      "type" : "CN_WORD",
      "position" : 1
    },
    {
      "token" : "國(guó)",
      "start_offset" : 2,
      "end_offset" : 3,
      "type" : "CN_CHAR",
      "position" : 2
    },
    {
      "token" : "國(guó)歌",
      "start_offset" : 3,
      "end_offset" : 5,
      "type" : "CN_WORD",
      "position" : 3
    }
  ]
}
image.png

IK Analysis的拓展配置

重點(diǎn)是在IK Analysis的配置文件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>
    <!--用戶可以在這里配置自己的擴(kuò)展字典 -->
    <entry key="ext_dict"></entry>
     <!--用戶可以在這里配置自己的擴(kuò)展停止詞字典-->
    <entry key="ext_stopwords"></entry>
    <!--用戶可以在這里配置遠(yuǎn)程擴(kuò)展字典 -->
    <!-- <entry key="remote_ext_dict">words_location</entry> -->
    <!--用戶可以在這里配置遠(yuǎn)程擴(kuò)展停止詞字典-->
    <!-- <entry key="remote_ext_stopwords">words_location</entry> -->
</properties>
IK分詞器自動(dòng)熱更新原理與實(shí)現(xiàn)

參考:https://www.cnblogs.com/liang1101/p/7282744.html

IK分詞器原理與源碼分析

參考:http://3dobe.com/archives/44/

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容