ElasticSearch安装ik分词器

 

 

1. ElasticSearch默认的分词器

  ElasticSearch有默认的分词器,是可以直接使用的,但因为默认分词器只对英文单词比较友好,对中文的兼容非常差。

1.1 英文测试

  请求地址:http://127.0.0.1:9200/_analyze?analyzer=standard&text=hello world

  请求方式:GET

  结果:

{
    "tokens": [
        {
            "token": "hello",
            "start_offset": 0,
            "end_offset": 5,
            "type": "<ALPHANUM>",
            "position": 0
        },
        {
            "token": "world",
            "start_offset": 6,
            "end_offset": 11,
            "type": "<ALPHANUM>",
            "position": 1
        }
    ]
}

 

1.2 中文测试

  请求地址:http://127.0.0.1:9200/_analyze?analyzer=standard&text=我是谁

  请求方式:GET

  结果:

{
    "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
        }
    ]
}

 

1.3 异常

  如果你的get请求出现以下异常,是因为版本不同,书写格式发生了变化。

{
    "error": {
        "root_cause": [
            {
                "type": "parse_exception",
                "reason": "request body or source parameter is required"
            }
        ],
        "type": "parse_exception",
        "reason": "request body or source parameter is required"
    },
    "status": 400
}

  需要修改以下请求方式就可以了

 

 2. 安装ik分词器

  ik分词器下载地址:https://github.com/medcl/elasticsearch-analysis-ik

  官方提供了2种安装方法,我们使用插件命令安装。进入服务器的elasticsearch的安装目录,

  由于我使用的docker,操作步骤如下:

  (1)进入容器:docker exec -it elasticsearch /bin/bash

  (2)查看当前目录,命令:pwd ,  显示结果:/usr/share/elasticsearch

  (3)在线安装分词器(命令中的elasticsearch版本号要改成自己的安装版本号),命令:./bin/elasticsearch-plugin install https://github.com/medcl/elasticsearch-analysis-ik/releases/download/v6.5.0/elasticsearch-analysis-ik-6.5.0.zip  

  (4)安装成功,进入/usr/share/elasticsearch/plugins目录下,查看是否已经有了 analysis-ik 文件夹

  (5)exit 退出容器

  (6)重启容器 docker restart elasticsearch

 

 3. ik分词器的使用

  使用上面的请求方式,把analyzer的参数值改为 ik_max_word或ik_smart

  我的请求参数   :{"analyzer":"ik_max_word", "text":"中华人民共和国"}

  分词结果:

{
    "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
        }
    ]
}

 

4. ik_max_word 和 ik_smart 什么区别?

  ik_max_word: 会将文本做最细粒度的拆分,比如会将“中华人民共和国国歌”拆分为“中华人民共和国,中华人民,中华,华人,人民共和国,人民,人,民,共和国,共和,和,国国,国歌”,会穷尽各种可能的组合,适合 Term Query;

  ik_smart: 会做最粗粒度的拆分,比如会将“中华人民共和国国歌”拆分为“中华人民共和国,国歌”,适合 Phrase 查询。

 

posted @ 2020-04-07 21:53  闲人鹤  阅读(751)  评论(0编辑  收藏  举报