ik_smart ik 分词器扩展字典配置方法

  1. 官方文档https://github.com/medcl/elasticsearch-analysis-ik
  2. 参考链接:https://blog.csdn.net/shihuacai/article/details/8494360/

我是用手工安装的插件,然后准备本地配置下扩展字典。步骤如下:

  1. https://github.com/medcl/elasticsearch-analysis-ik/releases 下载和自己的 es 版本相匹配的 ik 分词器。

  2. 解压 elasticsearch-analysis-ik-7.12.0.zip ,然后把解压文件夹拷贝到 /xxxxxx/elasticsearch-7.12.0/plugins/ 下。

  3. 然后把 elasticsearch-analysis-ik-7.12.0 文件夹重命名为 ik 。

  4. 然后编辑 /xxxxxx/elasticsearch-7.12.0/plugins/ik/config/IKAnalyzer.cfg.xml 配置文件。内容如下:

    <properties>
        <comment>IK Analyzer 扩展配置</comment>
        <!--用户可以在这里配置自己的扩展字典 -->
        <entry key="ext_dict">/xxxxxx/elasticsearch-7.12.0/plugins/ik/shfq_ext_dic.dic</entry>
        <!--用户可以在这里配置自己的扩展停止词字典-->
        <entry key="ext_stopwords"></entry>
        <!--用户可以在这里配置远程扩展字典 -->
        <!-- <entry key="remote_ext_dict">words_location</entry> -->
        <!--用户可以在这里配置远程扩展停止词字典-->
        <!-- <entry key="remote_ext_stopwords">words_location</entry> -->
    </properties>
    
  5. shfq_ext_dic.dic 扩展字典内容如下:

    民共
    程隆
    程隆腾达
    
  6. 重启 es ,然后在 kibana 对 “民共” 进行分词,结果没生效。

    GET _analyze
    {
        "analyzer": "ik_max_word",
        "text" : "民共"
    }
    分词结果:
    {
    "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
       	}
       ]
    }
    

百思不得其解,后来重新启动 es ,在启动日志里看到了下面的信息:

[Ext Loading] file not found: /xxxxxx/elasticsearch-7.12.0/plugins/ik/config/xxxxxx/elasticsearch-7.12.0/plugins/ik/shfq_ext_dic.dic

我在 IKAnalyzer.cfg.xml 配置文件中配置的扩展字典的地址是绝对地址,实际上只能用相对地址,必须把扩展字典文件放到 /xxxxxx/elasticsearch-7.12.0/plugins/ik/config/ 目录下 。

修改之后,再对 “民共” 分词之后,结果如下:

{
  "tokens" : [
    {
      "token" : "民共",
      "start_offset" : 0,
      "end_offset" : 2,
      "type" : "CN_WORD",
      "position" : 0
    }
  ]
}

符合预期。

有两点需要注意:

  1. 字典文件要求是 utf-8 编码格式的。
  2. 字典的路径是相对的,不能随便放,必须放到 config 目录下。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值