ElasticSearch IK的分词器安装与使用

安装

由于默认的分词器对中文支持不好,会将文字分割成一个个,我们需要安装IK分词器让es支持中文。下图可以看出默认的 standard 分词器能够将 hello world分词出来,而对于“你好世界”则分成了单字。

下面我们安装 ik 分词器让其支持中文;首先需要保证你的 es 版本跟 ik 分词器一致,es 与 kibana 的安装参照:Windows docker下安装elasticsearch与kibana,这里使用的版本为 7.4.2,可以直接这里 https://wwa.lanzous.com/iKQeOgpwxbc 下载,其他版本请自行到 https://github.com/medcl/elasticsearch-analysis-ik/releases 找到对应版本下载。

下载了zip包后只需要解压到 elasticsearch/plugins/ik 下,无需修改yml文件后重启 es即可。

 测试使用

重启后打开kibana,由于ik分词器支持两种 analyzer,ik_smart与ik_max_word,两者的区别是 :


    1、 ik_smart 会做最粗粒度的拆分,比如会将"人与人的体制不能一概而论"拆分为 "人与人之间"、"的"、"体制"、"不能"、"一概而论"。不会出现重复词。

 

2、 ik_max_word 会将文本做最细粒度的拆分,比如下图的 "人与人之间的体制不能一概而论"下面样子,截图不全,直接看json数据吧。它会作最细的分割。

{
  "tokens" : [
    {
      "token" : "人与人之间",
      "start_offset" : 0,
      "end_offset" : 5,
      "type" : "CN_WORD",
      "position" : 0
    },
    {
      "token" : "与人",
      "start_offset" : 1,
      "end_offset" : 3,
      "type" : "CN_WORD",
      "position" : 1
    },
    {
      "token" : "之间",
      "start_offset" : 3,
      "end_offset" : 5,
      "type" : "CN_WORD",
      "position" : 2
    },
    {
      "token" : "的",
      "start_offset" : 5,
      "end_offset" : 6,
      "type" : "CN_CHAR",
      "position" : 3
    },
    {
      "token" : "体质",
      "start_offset" : 6,
      "end_offset" : 8,
      "type" : "CN_WORD",
      "position" : 4
    },
    {
      "token" : "不能",
      "start_offset" : 8,
      "end_offset" : 10,
      "type" : "CN_WORD",
      "position" : 5
    },
    {
      "token" : "一概而论",
      "start_offset" : 10,
      "end_offset" : 14,
      "type" : "CN_WORD",
      "position" : 6
    },
    {
      "token" : "一概",
      "start_offset" : 10,
      "end_offset" : 12,
      "type" : "CN_WORD",
      "position" : 7
    },
    {
      "token" : "一",
      "start_offset" : 10,
      "end_offset" : 11,
      "type" : "TYPE_CNUM",
      "position" : 8
    },
    {
      "token" : "概",
      "start_offset" : 11,
      "end_offset" : 12,
      "type" : "CN_CHAR",
      "position" : 9
    },
    {
      "token" : "而论",
      "start_offset" : 12,
      "end_offset" : 14,
      "type" : "CN_WORD",
      "position" : 10
    }
  ]
}

使用哪个根据个人所需,一般使用 ik_max_word 就行了。

 

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值