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

简介: 白话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


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

相关实践学习
以电商场景为例搭建AI语义搜索应用
本实验旨在通过阿里云Elasticsearch结合阿里云搜索开发工作台AI模型服务,构建一个高效、精准的语义搜索系统,模拟电商场景,深入理解AI搜索技术原理并掌握其实现过程。
ElasticSearch 最新快速入门教程
本课程由千锋教育提供。全文搜索的需求非常大。而开源的解决办法Elasricsearch(Elastic)就是一个非常好的工具。目前是全文搜索引擎的首选。本系列教程由浅入深讲解了在CentOS7系统下如何搭建ElasticSearch,如何使用Kibana实现各种方式的搜索并详细分析了搜索的原理,最后讲解了在Java应用中如何集成ElasticSearch并实现搜索。  
相关文章
|
自然语言处理 大数据 应用服务中间件
大数据-172 Elasticsearch 索引操作 与 IK 分词器 自定义停用词 Nginx 服务
大数据-172 Elasticsearch 索引操作 与 IK 分词器 自定义停用词 Nginx 服务
301 5
|
自然语言处理 Java 网络架构
elasticsearch学习三:elasticsearch-ik分词器的自定义配置 分词内容
这篇文章是关于如何自定义Elasticsearch的ik分词器配置以满足特定的中文分词需求。
718 0
elasticsearch学习三:elasticsearch-ik分词器的自定义配置 分词内容
|
测试技术 API 开发工具
ElasticSearch的IK分词器
ElasticSearch的IK分词器
262 7
|
存储 安全 网络协议
Elasticsearch 配置文件解析
【10月更文挑战第3天】Elasticsearch 配置文件解析
469 3
|
存储 JSON Java
elasticsearch学习一:了解 ES,版本之间的对应。安装elasticsearch,kibana,head插件、elasticsearch-ik分词器。
这篇文章是关于Elasticsearch的学习指南,包括了解Elasticsearch、版本对应、安装运行Elasticsearch和Kibana、安装head插件和elasticsearch-ik分词器的步骤。
1329 0
elasticsearch学习一:了解 ES,版本之间的对应。安装elasticsearch,kibana,head插件、elasticsearch-ik分词器。
|
存储 自然语言处理 关系型数据库
ElasticSearch基础3——聚合、补全、集群。黑马旅游检索高亮+自定义分词器+自动补全+前后端消息同步
聚合、补全、RabbitMQ消息同步、集群、脑裂问题、集群分布式存储、黑马旅游实现过滤和搜索补全功能
ElasticSearch基础3——聚合、补全、集群。黑马旅游检索高亮+自定义分词器+自动补全+前后端消息同步
|
JSON 自然语言处理 Java
ElasticSearch 实现分词全文检索 - 搜素关键字自动补全(Completion Suggest)
ElasticSearch 实现分词全文检索 - 搜素关键字自动补全(Completion Suggest)
571 1
|
JSON 自然语言处理 数据库
Elasticsearch从入门到项目部署 安装 分词器 索引库操作
这篇文章详细介绍了Elasticsearch的基本概念、倒排索引原理、安装部署、IK分词器的使用,以及如何在Elasticsearch中进行索引库的CRUD操作,旨在帮助读者从入门到项目部署全面掌握Elasticsearch的使用。
|
自然语言处理 Java 关系型数据库
ElasticSearch 实现分词全文检索 - SpringBoot 完整实现 Demo 附源码【完结篇】
ElasticSearch 实现分词全文检索 - SpringBoot 完整实现 Demo 附源码【完结篇】
514 0
|
存储 自然语言处理 Java
ElasticSearch 实现分词全文检索 - 经纬度定位商家距离查询
ElasticSearch 实现分词全文检索 - 经纬度定位商家距离查询
352 0