ElasticSearch IK分词器配置远程词典

1.在线安装IK分词器

ElasticSearch中默认的分词器是standard,该分词器对中文按字分词,对英文按单词分词

GET /_analyze
{
  "text": "我是一个杠精,hello world!"
}

结果

{
  "tokens" : [
    {
      "token" : "我",
      "start_offset" : 0,
      "end_offset" : 1,
      "type" : "<IDEOGRAPHIC>",
      "position" : 0
    },
    {
      "token" : "是",
      "start_offset" : 1,
      "end_offset" : 2,
      "type" : "<IDEOGRAPHIC>",
      "position" : 1
    },
    {
      "token" : "一",
      "start_offset" : 2,
      "end_offset" : 3,
      "type" : "<IDEOGRAPHIC>",
      "position" : 2
    },
    {
      "token" : "个",
      "start_offset" : 3,
      "end_offset" : 4,
      "type" : "<IDEOGRAPHIC>",
      "position" : 3
    },
    {
      "token" : "杠",
      "start_offset" : 4,
      "end_offset" : 5,
      "type" : "<IDEOGRAPHIC>",
      "position" : 4
    },
    {
      "token" : "精",
      "start_offset" : 5,
      "end_offset" : 6,
      "type" : "<IDEOGRAPHIC>",
      "position" : 5
    },
    {
      "token" : "hello",
      "start_offset" : 7,
      "end_offset" : 12,
      "type" : "<ALPHANUM>",
      "position" : 6
    },
    {
      "token" : "world",
      "start_offset" : 13,
      "end_offset" : 18,
      "type" : "<ALPHANUM>",
      "position" : 7
    }
  ]
}

但是默认的分词器对中文网站不友好,因为对中文时按照单字分词,所以如果我们想按词语来搜索就搜不到内容;所以,我们使用IK分词器

在线安装IK分词器

在线安装IK (v5.5.1版本后开始支持在线安装 )

1、删除ES原始数据
因为之前可能使用的标准分词器,如果存在数据,现在又使用IK分词器会出现冲突

进入es安装目录中将data目录数据删除

rm -rf data

2、安装IK
在es安装的bin目录中执行如下命令

./elasticsearch-plugin install https://github.com/medcl/elasticsearch-analysis-ik/releases/download/v6.8.0/elasticsearch-analysis-ik-6.8.0.zip

3、查看IK
安装完成后会在plugins目录下,生成IK

[root@linux elasticsearch-6.8.0]$ ls plugins/
analysis-ik

4、重启es生效

IK分词器有两种模式
ik_max_word:最大限度分词


GET /_analyze
{
  "text": "中华人民共和国国歌,hello world!",
  "analyzer": "ik_max_word"
}
---------------
{
  "tokens" : [
    {
      "token" : "中华人民共和国",
      "start_offset" : 0,
      "end_offset" : 7,
      "type" : "CN_WORD",
      "position" : 0
    },
    {
      "token" : "中华人民",
      "start_offset" : 0,
      "end_offset" : 4,
      "type" : "CN_WORD",
      "position" : 1
    },
    {
      "token" : "中华",
      "start_offset" : 0,
      "end_offset" : 2,
      "type" : "CN_WORD",
      "position" : 2
    },
    {
      "token" : "华人",
      "start_offset" : 1,
      "end_offset" : 3,
      "type" : "CN_WORD",
      "position" : 3
    },
    {
      "token" : "人民共和国",
      "start_offset" : 2,
      "end_offset" : 7,
      "type" : "CN_WORD",
      "position" : 4
    },
    {
      "token" : "人民",
      "start_offset" : 2,
      "end_offset" : 4,
      "type" : "CN_WORD",
      "position" : 5
    },
    {
      "token" : "共和国",
      "start_offset" : 4,
      "end_offset" : 7,
      "type" : "CN_WORD",
      "position" : 6
    },
    {
      "token" : "共和",
      "start_offset" : 4,
      "end_offset" : 6,
      "type" : "CN_WORD",
      "position" : 7
    },
    {
      "token" : "国",
      "start_offset" : 6,
      "end_offset" : 7,
      "type" : "CN_CHAR",
      "position" : 8
    },
    {
      "token" : "国歌",
      "start_offset" : 7,
      "end_offset" : 9,
      "type" : "CN_WORD",
      "position" : 9
    },
    {
      "token" : "hello",
      "start_offset" : 10,
      "end_offset" : 15,
      "type" : "ENGLISH",
      "position" : 10
    },
    {
      "token" : "world",
      "start_offset" : 16,
      "end_offset" : 21,
      "type" : "ENGLISH",
      "position" : 11
    }
  ]
}

ik_smart:尽量以词语的形式分词

GET /_analyze
{
  "text": "中华人民共和国国歌,hello world!",
  "analyzer": "ik_smart"
}

--------
{
  "tokens" : [
    {
      "token" : "中华人民共和国",
      "start_offset" : 0,
      "end_offset" : 7,
      "type" : "CN_WORD",
      "position" : 0
    },
    {
      "token" : "国歌",
      "start_offset" : 7,
      "end_offset" : 9,
      "type" : "CN_WORD",
      "position" : 1
    },
    {
      "token" : "hello",
      "start_offset" : 10,
      "end_offset" : 15,
      "type" : "ENGLISH",
      "position" : 2
    },
    {
      "token" : "world",
      "start_offset" : 16,
      "end_offset" : 21,
      "type" : "ENGLISH",
      "position" : 3
    }
  ]

2.IK分词器的弊端

IK分词器虽然优于ES提供的默认分词器,但是还是存在一个弊端
那就是,不能识别网络中的热词,即不会把网络中流行的热词进行分词,
比如杠精,蓝瘦香菇…,这些词语,我们搜索时是希望搜索到内容的,但是IK会对这些词进行单字分词;
在这里插入图片描述
所以,我们搜索"碰瓷",""“杠精"等字眼时就搜索不到”

3.解决措施,配置远程词典实时更新

这些都是IK的本地词典,本地词典写入的词一旦写入,后面再想去填入或者停用,比较麻烦,所有这里我们配置远程词典
在这里插入图片描述
编辑 elasticsearch/config/analysis-ik/cat IKAnalyzer.cfg.xml文件
在这里插入图片描述
在此配置远程字典和停用词典

<!-- <entry key="remote_ext_dict">words_location</entry> -->

新建一个springboot应用,新建ext.txt文件,启动项目
在这里插入图片描述
把项目ip配置到xml文件中

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE properties SYSTEM "http://java.sun.com/dtd/properties.dtd">
<properties>
        <comment>IK Analyzer 扩展配置</comment>
        <!--用户可以在这里配置自己的扩展字典 -->
        <entry key="ext_dict"></entry>
         <!--用户可以在这里配置自己的扩展停止词字典-->
        <entry key="ext_stopwords"></entry>
        <!--用户可以在这里配置远程扩展字典 -->
        <entry key="remote_ext_dict">http://192.168.101.100:8082/ext.txt</entry>
        <!--用户可以在这里配置远程扩展停止词字典-->
        <entry key="remote_ext_stopwords">http://192.168.101.100:8082/stop.txt</entry>
</properties>

重启ES服务
发现已经重新加载词典
在这里插入图片描述
测试,发现已经成功!!
在这里插入图片描述
而且以后如果我们在项目中更改了词典,ES这边会自动更新
在这里插入图片描述

补充

以后,我们项目中的的ext.txt和stop.txt词典可以结合Redis来实现动态更新
Redis中可以统计某个词的搜索次数,每隔一段时间,统计TOPn的词,更新到txt中,然后ES监听到该url对应的资源发生了变化就会进行自动拉取
在这里插入图片描述
至此,IK分词器配置远程词典并实现自动更新完成~!

  • 6
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值