ElasticSearch IK分词器配置远程词典

ElasticSearch IK分词器配置远程词典

  • 1.在线安装IK分词器
  • 2.IK分词器的弊端
  • 3.解决措施,配置远程词典实时更新
  • 补充

1.在线安装IK分词器

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

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

结果

{
  "tokens" : [
    {
      "token" : "我",
      "start_offset" : 0,
      "end_offset" : 1,
      "type" : "",
      "position" : 0
    },
    {
      "token" : "是",
      "start_offset" : 1,
      "end_offset" : 2,
      "type" : "",
      "position" : 1
    },
    {
      "token" : "一",
      "start_offset" : 2,
      "end_offset" : 3,
      "type" : "",
      "position" : 2
    },
    {
      "token" : "个",
      "start_offset" : 3,
      "end_offset" : 4,
      "type" : "",
      "position" : 3
    },
    {
      "token" : "杠",
      "start_offset" : 4,
      "end_offset" : 5,
      "type" : "",
      "position" : 4
    },
    {
      "token" : "精",
      "start_offset" : 5,
      "end_offset" : 6,
      "type" : "",
      "position" : 5
    },
    {
      "token" : "hello",
      "start_offset" : 7,
      "end_offset" : 12,
      "type" : "",
      "position" : 6
    },
    {
      "token" : "world",
      "start_offset" : 13,
      "end_offset" : 18,
      "type" : "",
      "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会对这些词进行单字分词;
ElasticSearch IK分词器配置远程词典_第1张图片
所以,我们搜索"碰瓷",""“杠精"等字眼时就搜索不到”

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

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

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

新建一个springboot应用,新建ext.txt文件,启动项目
ElasticSearch IK分词器配置远程词典_第4张图片
把项目ip配置到xml文件中


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.txtentry>
        
        <entry key="remote_ext_stopwords">http://192.168.101.100:8082/stop.txtentry>
properties>

重启ES服务
发现已经重新加载词典
在这里插入图片描述
测试,发现已经成功!!
ElasticSearch IK分词器配置远程词典_第5张图片
而且以后如果我们在项目中更改了词典,ES这边会自动更新
ElasticSearch IK分词器配置远程词典_第6张图片

补充

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

你可能感兴趣的:(elasticsearch,elasticsearch,IK分词器,Redis)