zoukankan      html  css  js  c++  java
  • ElasticSearch 中文分词插件ik 的使用

    下载

    IK 的版本要与 Elasticsearch 的版本一致,因此下载 7.1.0 版本。 

    安装

    1、中文分词插件下载地址:https://github.com/medcl/elasticsearch-analysis-ik

    2、拼音分词插件下载地址:https://github.com/medcl/elasticsearch-analysis-pinyin

    下载你对应的版本 

    将解压后的 IK 文件夹,放入 elasticsearch 文件夹下的 plugins/ik 目录下。

    启动 Elasticsearch 后,看到下图,表示启动成功。

    扩展本地词库

    在 pluginsikconfigcustom 目录下新增文件 hotwords.dic。如添加 洪荒之力 。每一个词语一行。

    在 pluginsikconfig 文件夹下的 IKAnalyzer.cfg.xml 文件配置本地词库。

    <!--用户可以在这里配置自己的扩展字典,如果多个字典,则用分号分隔 custom/mydict.dic;custom/single_word_low_freq.dic-->
    <entry key="ext_dict">custom/hotwords.dic</entry>


    重新启动 Elasticsearch 显示如下图,表示启动成功。

    文档的中文分词使用

    IK分词器有两种分词模式:ik_max_word和ik_smart模式。

    1、ik_max_word

    会将文本做最细粒度的拆分,比如会将“中华人民共和国人民大会堂”拆分为“中华人民共和国、中华人民、中华、华人、人民共和国、人民、共和国、大会堂、大会、会堂等词语。

    2、ik_smart
    会做最粗粒度的拆分,比如会将“中华人民共和国人民大会堂”拆分为中华人民共和国、人民大会堂。

    下面我们分别测试下。
    先测试ik_max_word,输入命令如下:

    POST http://localhost:9200/_analyze
    {  
        "analyzer": "ik_max_word",
        "text": "世界如此之大"  
    }

    响应结果如下:

    {
      "tokens": [
        {
          "token": "世界",
          "start_offset": 0,
          "end_offset": 2,
          "type": "CN_WORD",
          "position": 0
        },
        {
          "token": "如此之",
          "start_offset": 2,
          "end_offset": 5,
          "type": "CN_WORD",
          "position": 1
        },
        {
          "token": "如此",
          "start_offset": 2,
          "end_offset": 4,
          "type": "CN_WORD",
          "position": 2
        },
        {
          "token": "之大",
          "start_offset": 4,
          "end_offset": 6,
          "type": "CN_WORD",
          "position": 3
        }
      ]
    }

    再测试ik_smart,输入命令如下:

    POST http://localhost:9200/_analyze
    {  
        "analyzer": "ik_smart",
        "text": "世界如此之大"  
    }

    响应结果如下:

    {
      "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": 4,
          "end_offset": 6,
          "type": "CN_WORD",
          "position": 2
        }
      ]
    }

    通过Docker 安装elasticsearch-analysis-ik-6.4.5插件

    FROM docker.elastic.co/elasticsearch/elasticsearch:6.4.5
    ADD elasticsearch-analysis-ik-6.4.5 /usr/share/elasticsearch/plugins/elasticsearch-analysis-ik-6.4.5
    这里我将elasticsearch-analysis-ik-6.4.5.zip 下载都解压到了Dockerfile同目录下的elasticsearch-analysis-ik-6.4.5目录中,然后通过ADD指令将elasticsearch-analysis-ik-6.4.5目录拷贝到了docker中elasticsearch的plugins目录。
  • 相关阅读:
    过滤xml文件内容
    python接口自动化之通过接口模拟一通电话的多段对话
    浅谈python性能与优化
    监控之Linux系统监控命令大全
    mysql windows 5.7 安装版下载地址
    liunx 安装jdk
    下载文件,后台执行没问题,没下载文件
    spring boot thymeleaf
    spring security文档地址
    redis 可视化工具下载地址
  • 原文地址:https://www.cnblogs.com/zyulike/p/11276682.html
Copyright © 2011-2022 走看看