• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

关于java:elasticsearch使用ik中文分词器

java 搞代码 4年前 (2022-02-14) 48次浏览 已收录 0个评论
文章目录[隐藏]

一、背景

es自带了一堆的分词器,比方standardwhitespacelanguage(比方english)等分词器,然而都对中文分词的成果不太好,此处装置第三方分词器ik,来实现分词。

二、装置 ik 分词器

1、从 github 上找到和本次 es 版本匹配上的 分词器

<code class="bash"># 下载地址
https://github.com/medcl/elasticsearch-analysis-ik/releases

2、应用 es 自带的插件治理 elasticsearch-plugin 来进行装置

  • 间接从网络地址装置

    <code class="bash">cd /Users/huan/soft/elastic-stack/es/es02/bin
    # 下载插件
    ./elasticsearch-plugin -v install https://github.com/medcl/elasticsearch-analysis-ik/releases/download/v7.12.0/elasticsearch-analysis-ik-7.12.0.zip
    # 查看插件是否下载胜利
    ./elasticsearch-plugin list
  • 从本地装置

    <code class="bash">cd /Users/huan/soft/elastic-stack/es/es02/bin
    # 下载插件(file前面跟的是插件在本地的地址)
    ./elasticsearch-plugin install file:///path/to/plugin.zip

    留神:
    如果本地插件的门路中存在空格,须要应用双引号包装起来。

3、重启es

<code class="bash"># 查找es过程
jps -l | grep 'Elasticsearch'
# 杀掉es过程
kill pid
# 启动es
/Users/huan/soft/elastic-stack/es/es01/bin/elasticsearch -d -p pid01

三、测试 ik 分词

ik分词器提供了2种分词的模式

  1. ik_max_word: 将须要分词的文本做最小粒度的拆分,尽量分更多的词。
  2. ik_smart: 将须要分词的文本做最大粒度的拆分。

1、测试默认的分词成果

语句

<code class="bash">GET _analyze
{
  "analyzer": "default",
  "text": ["中文分词语"]
}

后果

<code class="json">{
  "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
    }
  ]
}

能够看到默认的分词器,对中文的分词齐全无奈达到咱们中文的分词的成果。

2、测试 ik_max_word 的分词成果

语句

<code class="bash">GET _analyze
{
  "analyzer": "ik_max_word",
  "text": ["中文分词语"]
}

后果

<code class="json">{
  "tokens" : [
    {
      "token" : "中文",
      "start_offset" : 0,
      "end_offset" : 2,
      "type" : "CN_WORD",
      "position" : 0
    },
    {
      "token" : "分词",
      "start_offset" : 2,
      "end_offset" : 4,
      "type" : "CN_WORD",
      "position" : 1
    },
    {
      "token" : "词语",
      "start_offset" : 3,
      "end_offset" : 5,
      "type" : "CN_WORD",
      "position" : 2
    }
  ]
}

能够看到基于ik分词能够达到咱们须要的分词成果。

3、测试 ik_smart 的分词成果

语句

<code class="bash">GET _analyze
{
  "analyzer": "ik_smart",
  "text": ["中文分词语"]
}

后果

<code class="json">{
  "tokens" : [
    {
      "token" : "中文",
      "start_offset" : 0,
      "end_offset" : 2,
      "type" : "CN_WORD",
      "position" : 0
    },
    {
      "token" : "分",
      "start_offset" : 2,
      "end_offset" : 3,
      "type" : "CN_CHAR",
      "position" : 1
    },
    {
      "token" : "词语",
      "start_offset" : 3,
      "end_offset" : 5,
      "type" : "CN_WORD",
      "position" : 2
    }
  ]
}

4、自定义 ik 的启用词和停用词

1、找到 ik 的配置目录

<code class="bash">${IK_HOME}/config/analysis-ik
/Users/huan/soft/elastic-stack/es/es01/config/analysis-ik

2、批改 IKAnalyzer.cfg.xml 文件

<code class="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">custom-ext.dic</entry>
     <!--用户能够在这里配置本人的扩大进行词字典-->
    <entry key="ext_stopwords">custom-stop.dic</entry>
    <!--用户能够在这里配置近程扩大字典 -->
    <!-- <entry key="remote_ext_dict">words_location</entry> -->
    <!--用户能够在这里配置近程扩大进行词字典-->
    <!-- <entry key="remote_ext_stopwords">words_location</entry> -->
</properties>

3、custom-ext.dic 和 custom-stop.dic 的内容

留神:
1、自定义分词的文件必须是UTF-8的编码。

4、配置文件残缺门路

5、查看分词后果

5、热更新IK分词

1、批改 IKAnalyzer.cfg.xml 文件,配置近程字典。

<code class="xml"> $ cat /Users/huan/soft/elastic-stack/es/es01/config/analysis-ik/IKAnalyzer.cfg.xml                                                                    11.87s    16.48G    2.68 
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE properties SYSTEM "http://java.sun.com/dtd/properties.dtd">
<properties>
    <comment>IK Analyzer 扩大配置</comment>
    <!--用户能够在这里配置近程扩大字典 -->
    <entry key="remote_ext_dict">http://localhost:8686/custom-ext.dic</entry>
    <!--用户能够在这里配置近程扩大进行词字典-->
    <entry key="remote_ext_stopwords"></entry>
</properties>

留神:
1、此处的 custom-ext.dic 文件在下方将会配置到 nginx中,保障能够拜访。

2、http 申请须要返回两个头部(header),一个是 Last-Modified,一个是 ETag,这两者都是字符串类型,只有有一个发生变化,该插件就会去抓取新的分词进而更新词库。
3、http 申请返回的内容格局是一行一个分词,换行符用 \n 即可。
4、在 nginx 的目录下搁置一个 custom-ext.dic 文件

屡次批改 custom-ext.dic 文件,能够看到分词的后果也会实时变动,如此就实现了分词的热更新。

五、参考地址

1、https://www.elastic.co/guide/en/elasticsearch/plugins/7.12/plugin-management-custom-url.html
2、https://github.com/medcl/elasticsearch-analysis-ik/releases 来源gao@dai!ma.com搞$代^码网
3、https://github.com/medcl/elasticsearch-analysis-ik


搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:关于java:elasticsearch使用ik中文分词器

喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址