Elasticsearch中IK分词器安装

Elasticsearch中,内置了很多分词器(analyzers),例如standard (标准分词器)、english (英文分词)和chinese (中文分词)。其中standard 就是无脑的一个一个词(汉字)切分,所以适用范围广,但是精准度低;english 对英文更加智能,可以识别单数负数,大小写,过滤stopwords(例如“the”这个词)等;chinese 效果很差;
1、elasticsearch官方默认的分词插件,对中文分词效果不理想。例如:

curl ‘http://172.16.32.69:9200/_analyze?pretty=true’ -d ‘{“text”:“这里是好记性不如烂笔头感叹号的博客园”}’

[html] view plain copy
{
“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
},
{
“token” : “如”,
“start_offset” : 7,
“end_offset” : 8,
“type” : “”,
“position” : 7
},
{
“token” : “烂”,
“start_offset” : 8,
“end_offset” : 9,
“type” : “”,
“position” : 8
},
{
“token” : “笔”,
“start_offset” : 9,
“end_offset” : 10,
“type” : “”,
“position” : 9
},
{
“token” : “头”,
“start_offset” : 10,
“end_offset” : 11,
“type” : “”,
“position” : 10
},
{
“token” : “感”,
“start_offset” : 11,
“end_offset” : 12,
“type” : “”,
“position” : 11
},
{
“token” : “叹”,
“start_offset” : 12,
“end_offset” : 13,
“type” : “”,
“position” : 12
},
{
“token” : “号”,
“start_offset” : 13,
“end_offset” : 14,
“type” : “”,
“position” : 13
},
{
“token” : “的”,
“start_offset” : 14,
“end_offset” : 15,
“type” : “”,
“position” : 14
},
{
“token” : “博”,
“start_offset” : 15,
“end_offset” : 16,
“type” : “”,
“position” : 15
},
{
“token” : “客”,
“start_offset” : 16,
“end_offset” : 17,
“type” : “”,
“position” : 16
},
{
“token” : “们”,
“start_offset” : 17,
“end_offset” : 18,
“type” : “”,
“position” : 17
}
]
}

总结

如何集成IK分词工具
第一步:下载es的IK插件:https://github.com/medcl/elasticsearch-analysis-ik/releases(选择对应的版本,IK版本与ES版本一致,建议直接 下载编译后的zip包;若是下载源码包,则需要自己编码打包mvn clean package生成zip包)如图:

第二步:将下载的zip包(或自己编译后的包)拷贝至ES_HOME/plugins/ik(ps:ik目录没有的话自己新建一个就好),然后使用unzip命令解压
第四步:测试分词效果:
#curl -H ‘Content-Type:application/json’ ‘http://192.168.127.128:9200/_analyze?’ -d ‘{“analyzer”:“ik_max_word”,“text”:“这里是好记性不如烂笔头感叹号的博客们”}’

出现以下情况,说明IK分词器安装生效。

[html] view plain copy
{
“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” : 1,
“end_offset” : 2,
“type” : “CN_WORD”,
“position” : 2
},
{
“token” : “好记”,
“start_offset” : 3,
“end_offset” : 5,
“type” : “CN_WORD”,
“position” : 3
},
{
“token” : “记性”,
“start_offset” : 4,
“end_offset” : 6,
“type” : “CN_WORD”,
“position” : 4
},
{
“token” : “不如”,
“start_offset” : 6,
“end_offset” : 8,
“type” : “CN_WORD”,
“position” : 5
},
{
“token” : “烂”,
“start_offset” : 8,
“end_offset” : 9,
“type” : “CN_CHAR”,
“position” : 6
},
{
“token” : “笔头”,
“start_offset” : 9,
“end_offset” : 11,
“type” : “CN_WORD”,
“position” : 7
},
{
“token” : “笔”,
“start_offset” : 9,
“end_offset” : 10,
“type” : “CN_WORD”,
“position” : 8
},
{
“token” : “头”,
“start_offset” : 10,
“end_offset” : 11,
“type” : “CN_CHAR”,
“position” : 9
},
{
“token” : “感叹号”,
“start_offset” : 11,
“end_offset” : 14,
“type” : “CN_WORD”,
“position” : 10
},
{
“token” : “感叹”,
“start_offset” : 11,
“end_offset” : 13,
“type” : “CN_WORD”,
“position” : 11
},
{
“token” : “叹号”,
“start_offset” : 12,
“end_offset” : 14,
“type” : “CN_WORD”,
“position” : 12
},
{
“token” : “叹”,
“start_offset” : 12,
“end_offset” : 13,
“type” : “CN_WORD”,
“position” : 13
},
{
“token” : “号”,
“start_offset” : 13,
“end_offset” : 14,
“type” : “CN_CHAR”,
“position” : 14
},
{
“token” : “博客”,
“start_offset” : 15,
“end_offset” : 17,
“type” : “CN_WORD”,
“position” : 15
},
{
“token” : “们”,
“start_offset” : 17,
“end_offset” : 18,
“type” : “CN_CHAR”,
“position” : 16
}
]
}

参数说明:
ik_max_word: 会将文本做最细粒度的拆分,比如会将“中华人民共和国国歌”拆分为“中华人民共和国,中华人民,中华,华人,人民共和国,人民,人,民,共和国,共和,和,国国,国歌”,会穷尽各种可能的组合;
ik_smart: 会做最粗粒度的拆分,比如会将“中华人民共和国国歌”拆分为“中华人民共和国,国歌”。
“token” : “记性”,
其中我们在做索引的时候,希望能将所有的句子切分的更详细,以便更好的搜索,所以ik_max_word更多的用在做索引的时候,但是在搜索的时候,对于用户所输入的query(查询)词,我们可能更希望得比较准确的结果,例如,我们搜索“无花果”的时候,更希望是作为一个词进行查询,而不是切分为"无",“花”,“果”三个词进行结果的召回,因此ik_smart更加常用语对于输入词的分析

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值