白话Elasticsearch29-IK中文分词之IK分词器配置文件+自定义词库

本文涉及的产品
检索分析服务 Elasticsearch 版,2核4GB开发者规格 1个月
简介: 白话Elasticsearch29-IK中文分词之IK分词器配置文件+自定义词库

20190806092132811.jpg


概述

继续跟中华石杉老师学习ES,第29篇

课程地址: https://www.roncoo.com/view/55


ik配置文件

配置文件位置: ${ES_HOME}/plugins/ik/config/IKAnalyzer.cfg.xml


20190810233021208.png


IKAnalyzer.cfg.xml:


20190811203034426.png


这里使用的是6.4.1版本对应的ik分词器,可以看到 配置文件中 ext_dict和ext_stopwords 默认是空的,如果需要的话,我们可以修改该配置项。


几个配置文件的作用


IKAnalyzer.cfg.xml:用来配置自定义词库


main.dic:ik原生内置的中文词库,总共有27万多条,只要是这些单词,都会被分在一起


quantifier.dic:存放了一些单位相关的词


suffix.dic:存放了一些后缀


surname.dic:中国的姓氏


stopword.dic:英文停用词


最常用的两个


main.dic:包含了原生的中文词语,会按照这个里面的词语去分词,只要是这些单词,都会被分在一起

stopword.dic:包含了英文的停用词 ( 停用词 stop word ,比如 a 、the 、and、 at 、but 等 . 通常像停用词,会在分词的时候,直接被干掉,不会建立在倒排索引中 )



IK自定义词库


自定义词库


有一些特殊的流行词,一般不会在ik的原生词典main.dic里。

举个例子,比如2019年很火的 “盘他”,我们到原生词典main.dic中去查找下看看


20190811212410115.png


这个时候,我们用ik的ik_max_word分词器来查下分词

GET _analyze
{
  "text": ["盘他","杠精","脱粉"],
  "analyzer": "ik_max_word"
}


返回

{
  "tokens": [
    {
      "token": "盘",
      "start_offset": 0,
      "end_offset": 1,
      "type": "CN_CHAR",
      "position": 0
    },
    {
      "token": "他",
      "start_offset": 1,
      "end_offset": 2,
      "type": "CN_CHAR",
      "position": 1
    },
    {
      "token": "杠",
      "start_offset": 3,
      "end_offset": 4,
      "type": "CN_CHAR",
      "position": 2
    },
    {
      "token": "精",
      "start_offset": 4,
      "end_offset": 5,
      "type": "CN_CHAR",
      "position": 3
    },
    {
      "token": "脱",
      "start_offset": 6,
      "end_offset": 7,
      "type": "CN_CHAR",
      "position": 4
    },
    {
      "token": "粉",
      "start_offset": 7,
      "end_offset": 8,
      "type": "CN_CHAR",
      "position": 5
    }
  ]
}


可以看到使用ik的 ik_max_word分词器还是将每个汉字作为一个term , 这个时候去使用这些词语去搜索,效果肯定不是很理想。


Step1 : 新建自定义分词库

我们这里新建个目录 custom , 在该目录下新建一个文件: artisan.dic


20190811220239831.png


将希望不分词的词语放到该文件中,比如

盘他
杠精
脱粉



20190811220348118.png


Step2 : 添加到ik的配置文件中


在 ext_ditc节点 添加自定义的扩展字典 , ik本身提供的 extra_main.dic 词语更加丰富,这里我们也添加进去吧


20190811220456459.png


Step3 :重启es ,查看分词

重启es

GET _analyze
{
  "text": ["盘他","杠精","脱粉"],
  "analyzer": "ik_max_word"
}


返回

{
  "tokens": [
    {
      "token": "盘他",
      "start_offset": 0,
      "end_offset": 2,
      "type": "CN_WORD",
      "position": 0
    },
    {
      "token": "盘",
      "start_offset": 0,
      "end_offset": 1,
      "type": "CN_WORD",
      "position": 1
    },
    {
      "token": "他",
      "start_offset": 1,
      "end_offset": 2,
      "type": "CN_CHAR",
      "position": 2
    },
    {
      "token": "杠精",
      "start_offset": 3,
      "end_offset": 5,
      "type": "CN_WORD",
      "position": 3
    },
    {
      "token": "脱粉",
      "start_offset": 6,
      "end_offset": 8,
      "type": "CN_WORD",
      "position": 4
    }
  ]
}


可以看到,和未添加自定义词典相比,已经可以按照自己指定的规则进行分词了。


自定义停用词库


比如了,的,啥,么,我们可能并不想去建立索引,让人家搜索

可以看到

  • stopword.dic 中是 英文 停用词
  • extra_stopword.dic 中文停用词


20190811221112195.png

验证下分词

GET _analyze
{
  "text": ["啥类型"],
  "analyzer": "ik_max_word"
}

返回

{
  "tokens": [
    {
      "token": "啥",
      "start_offset": 0,
      "end_offset": 1,
      "type": "CN_CHAR",
      "position": 0
    },
    {
      "token": "类型",
      "start_offset": 1,
      "end_offset": 3,
      "type": "CN_WORD",
      "position": 1
    },
    {
      "token": "类",
      "start_offset": 1,
      "end_offset": 2,
      "type": "CN_WORD",
      "position": 2
    },
    {
      "token": "型",
      "start_offset": 2,
      "end_offset": 3,
      "type": "CN_CHAR",
      "position": 3
    }
  ]
}

20190811221203845.png


可以看到 “啥”是建立了倒排索引的。那我们下面来将 “啥”添加到自定义的停用词里,来验证下吧。


Step1 : 新建自定义停用词词典

我们在新建的目录 custom , 在该目录下新建一个文件: artisan_stopword.dic , 添加停用词

20190811222843997.png


Step2 : 添加到ik的配置文件中

在 ext_stopwords节点 添加自定义的停用词扩展字典 , ik本身提供的 extra_stopword.dic 这里我们也添加进去吧


20190811222942766.png

Step3 : 重启es ,查看停用词

重启es ,验证停用词

GET _analyze
{
  "text": ["啥类型"],
  "analyzer": "ik_max_word"
}


返回

{
  "tokens": [
    {
      "token": "类型",
      "start_offset": 1,
      "end_offset": 3,
      "type": "CN_WORD",
      "position": 0
    },
    {
      "token": "类",
      "start_offset": 1,
      "end_offset": 2,
      "type": "CN_WORD",
      "position": 1
    },
    {
      "token": "型",
      "start_offset": 2,
      "end_offset": 3,
      "type": "CN_CHAR",
      "position": 2
    }
  ]
}



20190811222524392.png


可以看到 “啥”已经不会在倒排索引中了,自定义停用词功能成功。

相关实践学习
使用阿里云Elasticsearch体验信息检索加速
通过创建登录阿里云Elasticsearch集群,使用DataWorks将MySQL数据同步至Elasticsearch,体验多条件检索效果,简单展示数据同步和信息检索加速的过程和操作。
ElasticSearch 入门精讲
ElasticSearch是一个开源的、基于Lucene的、分布式、高扩展、高实时的搜索与数据分析引擎。根据DB-Engines的排名显示,Elasticsearch是最受欢迎的企业搜索引擎,其次是Apache Solr(也是基于Lucene)。 ElasticSearch的实现原理主要分为以下几个步骤: 用户将数据提交到Elastic Search 数据库中 通过分词控制器去将对应的语句分词,将其权重和分词结果一并存入数据 当用户搜索数据时候,再根据权重将结果排名、打分 将返回结果呈现给用户 Elasticsearch可以用于搜索各种文档。它提供可扩展的搜索,具有接近实时的搜索,并支持多租户。
相关文章
|
7月前
|
自然语言处理 API 索引
ElasticSearch自定义pinyin和ik分词库
ElasticSearch自定义pinyin和ik分词库
80 0
|
2月前
|
自然语言处理 搜索推荐 应用服务中间件
Elasticsearch 外部词库文件更新
Elasticsearch 外部词库文件更新
32 1
|
11月前
|
自然语言处理 安全 关系型数据库
白话Elasticsearch30-IK中文分词之热更新IK词库
白话Elasticsearch30-IK中文分词之热更新IK词库
128 0
|
11月前
|
自然语言处理 算法 应用服务中间件
Elasticsearch安装IK分词器、配置自定义分词词库
Elasticsearch安装IK分词器、配置自定义分词词库
317 0
|
自然语言处理 Linux
elasticsearch扩展ik分词器词库
elasticsearch扩展ik分词器词库
310 0
|
自然语言处理 算法 关系型数据库
Elasticsearch 如何自定义扩展词库?
Elasticsearch 实战项目中势必会用到中文分词,而中文分词器的选型包含但不限于如下开源分词器: IK 分词器 https://github.com/medcl/elasticsearch-analysis-ik Ansj 分词器 https://github.com/NLPchina/elasticsearch-analysis-ansj jieba 分词器 https://github.com/sing1ee/elasticsearch-jieba-plugin 清华大学 thulac 分词器 https://github.com/microbun/elastics
1113 0
Elasticsearch 如何自定义扩展词库?
|
18天前
|
数据可视化 索引
elasticsearch head、kibana 安装和使用
elasticsearch head、kibana 安装和使用
|
30天前
|
存储 负载均衡 索引
linux7安装elasticsearch-7.4.0集群配置
linux7安装elasticsearch-7.4.0集群配置
113 0

热门文章

最新文章