elasticsearch hanlp插件自定义词典配置

简介: 简要讲述elasticsearch hanlp 插件自定义词典配置

背景

在使用elasticsearch的过程中,总会遇到与分词相关的需求,这里将针对常用的elasticsearch hanlp(后面统称为 es hanlp)分词插件进行讲解演示配置自定义业务字典,提高es hanlp分词的准确性,同时也提高查询效率。关于es hanlp的下载安装可以参考文章:https://developer.aliyun.com/article/1162376

自定义词典配置

新增自定义词典

es hanlp默认的字典配置路径是ES_HOME/plugins/analysis-hanlp/data/dictionary/custom,因此在ES_HOME/plugins/analysis-hanlp/data/dictionary/custom目录中新增自定义词典,这里为了不改变es hanlp插件原始的词典内容,因此需要新增业务关键词词典dongao.txt,里面可以加入业务相关的词

image.png

修改hanlp.properties

找到hanlp.properties配置文件路径ES_HOME/config/analysis-hanlp

image.png

修改hanlp.properties,修改CustomDictionaryPath,增加自定义词典配置

image.png

这里分号后面加【空格】的意思就等于是

data/dictionary/custom/dongao.txt;

省略了前面的路径部分。

自动加载词典

配置完成自定义词典后,无需重启es,等待1分钟后,词典自动加载。

image.png

加载完成之后我们可以开始测试是否生效。

注:每个节点都需要做上述更改

自定义词典测试

1.当没有配置自定义词典的时候我们测试

POST _analyze
{
"text": "普通合伙人",
"analyzer": "hanlp"}

返回结果是

{
"tokens" : [
    {
"token" : "普通",
"start_offset" : 0,
"end_offset" : 2,
"type" : "a",
"position" : 0    },
    {
"token" : "合伙人",
"start_offset" : 2,
"end_offset" : 5,
"type" : "nnt",
"position" : 1    }
  ]
}

返回结果的截图

image.png


2.这时我们给自定义词典dongao.txt增加业务专有名词 【普通合伙人】

[root@dev3-test-java-22.190 /elk6.7.1/elasticsearch-6.7.1/plugins/analysis-hanlp/data/dictionary/custom]#vim dongao.txt [root@dev3-test-java-22.190 /elk6.7.1/elasticsearch-6.7.1/plugins/analysis-hanlp/data/dictionary/custom]#cat dongao.txt 普通合伙人

等待1分钟,

[2023-02-16T17:24:21,451][INFO ][c.h.d.ExtMonitor         ] [ei-test-es-node190] reloading hanlp custom dictionary
[2023-02-16T17:24:22,436][INFO ][o.e.m.j.JvmGcMonitorService] [ei-test-es-node190] [gc][young][732][58] duration [713ms], collections [1]/[1.6s], total [713ms]/[7.2s], memory [1.6gb]->[1.1gb]/[2gb], all_pools {[young] [701mb]->[0b]/[0b]}{[survivor] [3mb]->[0b]/[0b]}{[old] [954.6mb]->[1.1gb]/[2gb]}
[2023-02-16T17:24:22,436][INFO ][o.e.m.j.JvmGcMonitorService] [ei-test-es-node190] [gc][732] overhead, spent [713ms] collecting in the last [1.6s]
[2023-02-16T17:24:41,695][INFO ][o.e.c.r.a.DiskThresholdMonitor] [ei-test-es-node190] low disk watermark [85%] exceeded on [cbwVMU6UTACFPxKW0zkOcw][ei-test-es-node190][/es-data/nodes/0] free: 6.1gb[14.8%], replicas will not be assigned to this node[2023-02-16T17:24:47,423][INFO ][c.h.d.c.DictionaryFileCache] [ei-test-es-node190] begin write down hanlp custom dictionary file cache, file path: /elk6.7.1/elasticsearch-6.7.1/plugins/analysis-hanlp/hanlp.cache, custom dictionary file list: [DictionaryFile{path='plugins/analysis-hanlp/data/dictionary/custom/CustomDictionary.txt', lastModified=1668667063000}, DictionaryFile{path='plugins/analysis-hanlp/data/dictionary/custom/Accounting.txt', lastModified=1668668268000}, DictionaryFile{path='plugins/analysis-hanlp/data/dictionary/custom/ModernChineseSupplementaryWord.txt', lastModified=1543232260000}, DictionaryFile{path='plugins/analysis-hanlp/data/dictionary/custom/ChinesePlaceName.txt', lastModified=1543232260000}, DictionaryFile{path='plugins/analysis-hanlp/data/dictionary/custom/PersonalName.txt', lastModified=1543232260000}, DictionaryFile{path='plugins/analysis-hanlp/data/dictionary/custom/OrganizationName.txt', lastModified=1543232260000}, DictionaryFile{path='plugins/analysis-hanlp/data/dictionary/custom/ShanghaiPlaceName.txt', lastModified=1543232260000}, DictionaryFile{path='plugins/analysis-hanlp/data/dictionary/custom/dongao.txt', lastModified=1676539442000}, DictionaryFile{path='plugins/analysis-hanlp/data/dictionary/person/nrf.txt', lastModified=1542195182000}]
[2023-02-16T17:24:47,424][INFO ][c.h.d.c.DictionaryFileCache] [ei-test-es-node190] write down hanlp custom dictionary file cache successfully
[2023-02-16T17:24:47,424][INFO ][c.h.d.ExtMonitor         ] [ei-test-es-node190] finish reload hanlp custom dictionary

自定义词典加载完成后再次测试

image.png

如果测试过程中没有生效的话,建议重启es后再次测试即可,为了防止重启es影响业务进行,建议提前准备好业务关键词,一次性添加进入你的自定义词典dongao.txt然后重启es,这里还有一点需要注意的是,如果你新增自定义词典内容,重启es后对应的新增内容会实现,如果删除之前增加的自定义词典,建议先删除CustomDictionary.txt.bin

image.png

文件之后再重启es,因为CustomDictionary.txt.bin这个文件类似于一个缓存文件,为了防止干扰正常词匹配,删除该文件之后再重启es更稳妥。

相关实践学习
以电商场景为例搭建AI语义搜索应用
本实验旨在通过阿里云Elasticsearch结合阿里云搜索开发工作台AI模型服务,构建一个高效、精准的语义搜索系统,模拟电商场景,深入理解AI搜索技术原理并掌握其实现过程。
ElasticSearch 最新快速入门教程
本课程由千锋教育提供。全文搜索的需求非常大。而开源的解决办法Elasricsearch(Elastic)就是一个非常好的工具。目前是全文搜索引擎的首选。本系列教程由浅入深讲解了在CentOS7系统下如何搭建ElasticSearch,如何使用Kibana实现各种方式的搜索并详细分析了搜索的原理,最后讲解了在Java应用中如何集成ElasticSearch并实现搜索。  
相关文章
|
存储 缓存 固态存储
优化Elasticsearch 硬件配置
优化Elasticsearch 硬件配置
658 5
|
缓存 监控 安全
优化Elasticsearch 集群配置
优化Elasticsearch 集群配置
399 4
|
监控 负载均衡 安全
Elasticsearch集群配置优化
Elasticsearch集群配置优化
365 1
|
11月前
|
安全 Java Linux
Linux安装Elasticsearch详细教程
Linux安装Elasticsearch详细教程
1953 64
|
10月前
|
JSON 安全 数据可视化
Elasticsearch(es)在Windows系统上的安装与部署(含Kibana)
Kibana 是 Elastic Stack(原 ELK Stack)中的核心数据可视化工具,主要与 Elasticsearch 配合使用,提供强大的数据探索、分析和展示功能。elasticsearch安装在windows上一般是zip文件,解压到对应目录。文件,elasticsearch8.x以上版本是自动开启安全认证的。kibana安装在windows上一般是zip文件,解压到对应目录。elasticsearch的默认端口是9200,访问。默认用户是elastic,密码需要重置。
5155 0
|
存储 安全 数据管理
如何在 Rocky Linux 8 上安装和配置 Elasticsearch
本文详细介绍了在 Rocky Linux 8 上安装和配置 Elasticsearch 的步骤,包括添加仓库、安装 Elasticsearch、配置文件修改、设置内存和文件描述符、启动和验证 Elasticsearch,以及常见问题的解决方法。通过这些步骤,你可以快速搭建起这个强大的分布式搜索和分析引擎。
575 5
|
NoSQL 关系型数据库 Redis
mall在linux环境下的部署(基于Docker容器),Docker安装mysql、redis、nginx、rabbitmq、elasticsearch、logstash、kibana、mongo
mall在linux环境下的部署(基于Docker容器),docker安装mysql、redis、nginx、rabbitmq、elasticsearch、logstash、kibana、mongodb、minio详细教程,拉取镜像、运行容器
mall在linux环境下的部署(基于Docker容器),Docker安装mysql、redis、nginx、rabbitmq、elasticsearch、logstash、kibana、mongo
|
存储 JSON Java
elasticsearch学习一:了解 ES,版本之间的对应。安装elasticsearch,kibana,head插件、elasticsearch-ik分词器。
这篇文章是关于Elasticsearch的学习指南,包括了解Elasticsearch、版本对应、安装运行Elasticsearch和Kibana、安装head插件和elasticsearch-ik分词器的步骤。
1375 0
elasticsearch学习一:了解 ES,版本之间的对应。安装elasticsearch,kibana,head插件、elasticsearch-ik分词器。
|
数据可视化 Docker 容器
一文教会你如何通过Docker安装elasticsearch和kibana 【详细过程+图解】
这篇文章提供了通过Docker安装Elasticsearch和Kibana的详细过程和图解,包括下载镜像、创建和启动容器、处理可能遇到的启动失败情况(如权限不足和配置文件错误)、测试Elasticsearch和Kibana的连接,以及解决空间不足的问题。文章还特别指出了配置文件中空格的重要性以及环境变量中字母大小写的问题。
一文教会你如何通过Docker安装elasticsearch和kibana 【详细过程+图解】
|
Docker 容器
docker desktop安装es并连接elasticsearch-head:5
以上就是在Docker Desktop上安装Elasticsearch并连接Elasticsearch-head:5的步骤。
794 2