IT开发必备软件和常用技术基础原理

Elasticsearch分词插件——IK Analysis

2019-01-29  本文已影响18人  意识流丶

IK分词插件

IK Analyzer是一个开源的,基于java语言开发的轻量级的中文分词工具包。IK Analysis插件将Lucene IK分析器(http://code.google.com/p/ik-analyzer/)集成到elasticsearch中,支持自定义词典。是一款基于词典和规则的中文分词器。
Github地址:https://github.com/medcl/elasticsearch-analysis-ik

IK分词插件版本和Elasticsearch版本对应

IK version ES version
master 6.x -> master
6.3.0 6.3.0
6.2.4 6.2.4
6.1.3 6.1.3
5.6.8 5.6.8
5.5.3 5.5.3
5.4.3 5.4.3
5.3.3 5.3.3
5.2.2 5.2.2
5.1.2 5.1.2
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版本的不需要进行任何的配置

启动Elasticsearch,可以看到有读取ik配置文件

image.png

IK分词的原理与测试

IK 的 ik_smartik_max_word 两种分词策略

分词的测试使用curl或者postman都可以,我个人倾向于postman,主要是能保存,要方便一些。

这里我直接在Elasticsearch可视化工具kibana的控制台上操作

默认的分词策略standard

GET _analyze
{
  "text": "共和国国歌"
}

分词结果:

{
  "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
    }
  ]
}
image.png

ik_smart分词策略(智能模式):会做最粗粒度的拆分,用于搜索,更精确的搜索到想要的结果

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

ik_max_word分词策略(细粒度模式):会将文本做最细粒度的拆分,多用于索引,最大化的将文章内容分词

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

IK Analysis的拓展配置

重点是在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 扩展配置</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>
IK分词器自动热更新原理与实现

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

IK分词器原理与源码分析

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

上一篇下一篇

猜你喜欢

热点阅读