Elasticsearch分词插件——IK Analysis

ElasticSearch及IK分词插件相关安装

一. 简介

  • ElasticSearch是一个基于Lucene的搜索服务器。它提供了一个分布式多用户能力的全文搜索引擎,基于RESTful web接口。Elasticsearch是用Java开发的,并作为Apache许可条款下的开放源码发布,是当前流行的企业级搜索引擎。设计用于云计算中,能够达到实时搜索,稳定,可靠,快速,安装使用方便。
  • Elasticsearch-head是一个界面化的集群操作和管理工具,他是通过html5编写,可以对集群进行傻瓜式操作。
  • IK Analyzer是一个开源的,基于java语言开发的轻量级的中文分词工具包。从2006年12月推出1.0版开始, IKAnalyzer已经推出了4个大版本。最初,它是以开源项目Luence为应用主体的,结合词典分词和文法分析算法的中文分词组件。从3.0版本开 始,IK发展为面向Java的公用分词组件,独立于Lucene项目,同时提供了对Lucene的默认优化实现。在2012版本中,IK实现了简单的分词 歧义排除算法,标志着IK分词器从单纯的词典分词向模拟语义分词衍化。

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 versionES version
master7.x -> master
6.x6.x
5.x5.x
1.10.62.4.6
1.9.52.3.5
1.8.12.2.1
1.7.02.1.1
1.5.02.0.0
1.2.61.0.0
1.2.50.90.x
1.1.30.20.x
1.0.00.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_smart 和 ik_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
    }
  ]
}

 

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

 

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/

IK中文分词器在Elasticsearch上的使用。原生IK中文分词是从文件系统中读取词典,es-ik本身可扩展成从不同的源读取词典。目前提供从sqlite3数据库中读取。es-ik-plugin-sqlite3使用方法: 1. 在elasticsearch.yml中设置你的sqlite3词典的位置:ik_analysis_db_path: /opt/ik/dictionary.db 我提供了默认的词典:https://github.com/zacker330/es-ik-sqlite3-dictionary 2. 安装(目前是1.0.1版本)./bin/plugin -i ik-analysis -u https://github.com/zacker330/es-ik-plugin-sqlite3-release/raw/master/es-ik-sqlite3-1.0.1.zip 3. 现在可以测试了:     1. 创建index curl -X PUT -H "Cache-Control: no-cache" -d '{     "settings":{         "index":{             "number_of_shards":1,             "number_of_replicas": 1         }     } }' 'http://localhost:9200/songs/'      2. 创建map: curl -X PUT -H "Cache-Control: no-cache" -d '{         "song": {             "_source": {"enabled": true},             "_all": {                 "indexAnalyzer": "ik_analysis",                 "searchAnalyzer": "ik_analysis",                 "term_vector": "no",                 "store": "true"             },             "properties":{                 "title":{                     "type": "string",                     "store": "yes",                     "indexAnalyzer": "ik_analysis",                     "searchAnalyzer": "ik_analysis",                     "include_in_all": "true"                 }             }         } }     ' 'http://localhost:9200/songs/_mapping/song'       3.curl -X POST  -d '林夕为我们作词' 'http://localhost:9200/songs/_analyze?analyzer=ik_analysis' response: {"tokens":[{"token":"林夕","start_offset":0,"end_offset":2,"type":"CN_WORD","position":1},{"token":"作词","start_offset":5,"end_offset":7,"type":"CN_WORD","position":2}]} 标签:中文分词  分词插件
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值