elasticsearch hanlp插件自定义词典配置

2024-06-06 16:28

本文主要是介绍elasticsearch hanlp插件自定义词典配置,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

elasticsearch hanlp插件自定义词典配置

  • 背景
  • 自定义词典配置
    • 新增自定义词典
    • 修改 hanlp.properties
    • 自动加载词典
  • 自定义词典测试

背景

在使用 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,里面可以加入业务相关的词
在这里插入图片描述

修改 hanlp.properties

找到 hanlp.properties 配置文件路径 ES_HOME/config/analysis-hanlp
在这里插入图片描述
修改 hanlp.properties ,修改 CustomDictionaryPath ,增加自定义词典配置
在这里插入图片描述
这里分号后面加【空格】的意思就等于是

data/dictionary/custom/dongao.txt;

省略了前面的路径部分。

自动加载词典

配置完成自定义词典后,无需重启es,等待1分钟后,词典自动加载。
在这里插入图片描述
加载完成之后我们可以开始测试是否生效。
注:每个节点都需要做上述更改

自定义词典测试

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

返回结果的截图
在这里插入图片描述
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

自定义词典加载完成后再次测试
在这里插入图片描述
如果测试过程中没有生效的话,建议重启 es 后再次测试即可,为了防止重启 es 影响业务进行,建议提前准备好业务关键词,一次性添加进入你的自定义词典 dongao.txt 然后重启es,这里还有一点需要注意的是,如果你新增自定义词典内容,重启es后对应的新增内容会实现,如果删除之前增加的自定义词典,建议先删除 CustomDictionary.txt.bin
在这里插入图片描述
文件之后再重启 es ,因为 CustomDictionary.txt.bin 这个文件类似于一个缓存文件,为了防止干扰正常词匹配,删除该文件之后再重启es更稳妥。

这篇关于elasticsearch hanlp插件自定义词典配置的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1036625

相关文章

SpringBoot全局异常拦截与自定义错误页面实现过程解读

《SpringBoot全局异常拦截与自定义错误页面实现过程解读》本文介绍了SpringBoot中全局异常拦截与自定义错误页面的实现方法,包括异常的分类、SpringBoot默认异常处理机制、全局异常拦... 目录一、引言二、Spring Boot异常处理基础2.1 异常的分类2.2 Spring Boot默

jdk1.8的Jenkins安装配置实践

《jdk1.8的Jenkins安装配置实践》Jenkins是一款流行的开源持续集成工具,支持自动构建、测试和部署,通过Jenkins,开发团队可以实现代码提交后自动进行构建、测试,并将构建结果分发到测... 目录Jenkins介绍Jenkins环境搭建Jenkins安装配置Jenkins插件安装Git安装配

自定义注解SpringBoot防重复提交AOP方法详解

《自定义注解SpringBoot防重复提交AOP方法详解》该文章描述了一个防止重复提交的流程,通过HttpServletRequest对象获取请求信息,生成唯一标识,使用Redis分布式锁判断请求是否... 目录防重复提交流程引入依赖properties配置自定义注解切面Redis工具类controller

Nginx之https证书配置实现

《Nginx之https证书配置实现》本文主要介绍了Nginx之https证书配置的实现示例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起... 目录背景介绍为什么不能部署在 IIS 或 NAT 设备上?具体实现证书获取nginx配置扩展结果验证

VSCode开发中有哪些好用的插件和快捷键

《VSCode开发中有哪些好用的插件和快捷键》作为全球最受欢迎的编程工具,VSCode的快捷键体系是提升开发效率的核心密码,:本文主要介绍VSCode开发中有哪些好用的插件和快捷键的相关资料,文中... 目录前言1、vscode插件1.1 Live-server1.2 Auto Rename Tag1.3

springboot3.x使用@NacosValue无法获取配置信息的解决过程

《springboot3.x使用@NacosValue无法获取配置信息的解决过程》在SpringBoot3.x中升级Nacos依赖后,使用@NacosValue无法动态获取配置,通过引入SpringC... 目录一、python问题描述二、解决方案总结一、问题描述springboot从2android.x

nginx跨域访问配置的几种方法实现

《nginx跨域访问配置的几种方法实现》本文详细介绍了Nginx跨域配置方法,包括基本配置、只允许指定域名、携带Cookie的跨域、动态设置允许的Origin、支持不同路径的跨域控制、静态资源跨域以及... 目录一、基本跨域配置二、只允许指定域名跨域三、完整示例四、配置后重载 nginx五、注意事项六、支持

SpringBoot简单整合ElasticSearch实践

《SpringBoot简单整合ElasticSearch实践》Elasticsearch支持结构化和非结构化数据检索,通过索引创建和倒排索引文档,提高搜索效率,它基于Lucene封装,分为索引库、类型... 目录一:ElasticSearch支持对结构化和非结构化的数据进行检索二:ES的核心概念Index:

Spring配置扩展之JavaConfig的使用小结

《Spring配置扩展之JavaConfig的使用小结》JavaConfig是Spring框架中基于纯Java代码的配置方式,用于替代传统的XML配置,通过注解(如@Bean)定义Spring容器的组... 目录JavaConfig 的概念什么是JavaConfig?为什么使用 JavaConfig?Jav

Spring Boot Interceptor的原理、配置、顺序控制及与Filter的关键区别对比分析

《SpringBootInterceptor的原理、配置、顺序控制及与Filter的关键区别对比分析》本文主要介绍了SpringBoot中的拦截器(Interceptor)及其与过滤器(Filt... 目录前言一、核心功能二、拦截器的实现2.1 定义自定义拦截器2.2 注册拦截器三、多拦截器的执行顺序四、过