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

修改 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更稳妥。

相关推荐

  1. Mybatis运行原理及定义

    2024-06-10 08:06:05       68 阅读
  2. vue3+ts定义

    2024-06-10 08:06:05       34 阅读
  3. Lucene 定义词库

    2024-06-10 08:06:05       21 阅读

最近更新

  1. TCP协议是安全的吗?

    2024-06-10 08:06:05       16 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-06-10 08:06:05       16 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-06-10 08:06:05       15 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-06-10 08:06:05       18 阅读

热门阅读

  1. JVM详解

    JVM详解

    2024-06-10 08:06:05      8 阅读
  2. OJ3829大石头的搬运工

    2024-06-10 08:06:05       9 阅读
  3. QML键盘事件的用法和示例

    2024-06-10 08:06:05       7 阅读
  4. FastJson

    FastJson

    2024-06-10 08:06:05      10 阅读
  5. Prompt实现简单英语单词教学

    2024-06-10 08:06:05       7 阅读
  6. 2024.6.9 六

    2024-06-10 08:06:05       7 阅读
  7. 单词记忆(第二周)

    2024-06-10 08:06:05       10 阅读