elasticsearch5.x中IK分词器怎么用

本篇文章为大家展示了elasticsearch 5.x中IK分词器怎么用,内容简明扼要并且容易理解,绝对能使你眼前一亮,通过这篇文章的详细介绍希望你能有所收获。

创新互联是专业的潘集网站建设公司,潘集接单;提供成都做网站、网站制作,网页设计,网站设计,建网站,PHP网站建设等专业做网站服务;采用PHP框架,可快速的进行潘集网站开发网页制作和功能扩展;专业做搜索引擎喜爱的网站,专业的做网站团队,希望更多企业前来合作!

ik分词器的地址 https://github.com/medcl/elasticsearch-analysis-ik/releases ,分词器插件需要和ES版本匹配

由于es是5.6.16版本,所有我们下载5.6.16

https://github.com/medcl/elasticsearch-analysis-ik/releases/download/v5.6.16/elasticsearch-analysis-ik-5.6.16.zip

解压后,把安装包放在ES节点的plugins目录,包名重命名为ik

elasticsearch 5.x中IK分词器怎么用

重启ES,测试下IK分词效果

(1)无分词器下的效果

GET _analyze?pretty
{
  "text":"安徽省长江流域"
}

返回结果。

{
  "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": "域",
      "start_offset": 6,
      "end_offset": 7,
      "type": "",
      "position": 6
    }
  ]
}

可见 “安徽省长江流域” 每个字都分成了一个词

(2)IK分词器下的效果,ik_smart分词器

GET _analyze?pretty
{
  "analyzer": "ik_smart",
  "text":"安徽省长江流域"
}

结果

{
  "tokens": [
    {
      "token": "安徽省",
      "start_offset": 0,
      "end_offset": 3,
      "type": "CN_WORD",
      "position": 0
    },
    {
      "token": "长江流域",
      "start_offset": 3,
      "end_offset": 7,
      "type": "CN_WORD",
      "position": 1
    }
  ]
}

(3)IK分词器下的效果,ik_smart分词器

GET _analyze?pretty
{
  "analyzer": "ik_max_word",
  "text":"安徽省长江流域"
}

结果

{
  "tokens": [
    {
      "token": "安徽省",
      "start_offset": 0,
      "end_offset": 3,
      "type": "CN_WORD",
      "position": 0
    },
    {
      "token": "安徽",
      "start_offset": 0,
      "end_offset": 2,
      "type": "CN_WORD",
      "position": 1
    },
    {
      "token": "省长",
      "start_offset": 2,
      "end_offset": 4,
      "type": "CN_WORD",
      "position": 2
    },
    {
      "token": "长江流域",
      "start_offset": 3,
      "end_offset": 7,
      "type": "CN_WORD",
      "position": 3
    },
    {
      "token": "长江",
      "start_offset": 3,
      "end_offset": 5,
      "type": "CN_WORD",
      "position": 4
    },
    {
      "token": "江流",
      "start_offset": 4,
      "end_offset": 6,
      "type": "CN_WORD",
      "position": 5
    },
    {
      "token": "流域",
      "start_offset": 5,
      "end_offset": 7,
      "type": "CN_WORD",
      "position": 6
    }
  ]
}

为什么IK分词器能分析中文词汇呢?因为在它的config目录内置了一些词典。

那么如果我们需要识别一些新的词汇怎么办?例如一部连续剧 “权利的游戏”

自定义词典

在IK插件的config目录下创建tv目录,新建 tv.dic 文件(注意,一定要UTF-8无BOM的格式)

然后在 IKAnalyzer.cfg.xml 文件在添加配置

重启ES、Kibana ,试下效果

GET _analyze?pretty
{
  "analyzer": "ik_smart",
  "text":"权利的游戏"
}

分词结果

{
  "tokens": [
    {
      "token": "权利的游戏",
      "start_offset": 0,
      "end_offset": 5,
      "type": "CN_WORD",
      "position": 0
    }
  ]
}

上述内容就是elasticsearch 5.x中IK分词器怎么用,你们学到知识或技能了吗?如果还想学到更多技能或者丰富自己的知识储备,欢迎关注创新互联行业资讯频道。


网站栏目:elasticsearch5.x中IK分词器怎么用
文章源于:http://scyanting.com/article/jhhpsd.html