您的位置:首页 > 房产 > 家装 > elasticsearch hanlp插件自定义词典配置

elasticsearch hanlp插件自定义词典配置

2024/10/5 7:13:12 来源:https://blog.csdn.net/csdn565973850/article/details/139492520  浏览:    关键词: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更稳妥。

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com