IK分词器是关于中文的一个分词器,有IK分词器自然还有别的分词器,比如说pinyin分词器针对的是拼音分词,letter针对的是letter分词等,ES有一个很好的方式就是通过插件的形式来扩展这些功能。
一 IK分词器的下载和安装
其实安装很方便,首先要知道自己的ELK都是什么版本,然后到IK分词器的github地址上搜索相对应的版本。
IK分词器github地址
下载到对应的IK分词器版本,剩下的就是如何安装分词器。很简单就是对应的包解压到ElasticSearch的安装路径下plugins 文件夹下。如图:
最后一步,重启ES,就跟你idea安装新的插件一般也会提示你重启idea,重启成功后就可以使用分词器。
查看是否加载分词器,除了es启动可以看到,还可以通过bin目录下elasticsearch-plugin list 查看已经加载的插件。
二 IK分词器的使用和体验
我们现在已经安装了Kibana,kibana本身就可以做为一个不错的可视化插件,我们后续的操作可以在我们kibana上体验测试。
打开kibana的develop tool(中文是开发工具)
下面是ik分词器的两种分析器: max最细粒度 smart最粗粒度
ik_max_word 会将文本做最细粒度的拆分
- 比如会将「中华人民共和国国歌」拆分为:中华人民共和国、中华人民、中华、华人、人民共和国、人民、人、民、共和国、共和、和、国国、国歌,会穷尽各种可能的组合;
ik_smart 最粗粒度的拆分
- 比如会将「中华人民共和国国歌」拆分为:中华人民共和国、国歌。
简单的请求
max最大化最细
GET _analyze
{
"text": "中华人民共和国国歌",
"analyzer": "ik_max_word"
}
返回
{
"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
},
{
"token" : "国歌",
"start_offset" : 7,
"end_offset" : 9,
"type" : "CN_WORD",
"position" : 9
}
]
}
smart
GET _analyze
{
"text": "中华人民共和国国歌",
"analyzer": "ik_smart"
}
{
"tokens" : [
{
"token" : "中华人民共和国",
"start_offset" : 0,
"end_offset" : 7,
"type" : "CN_WORD",
"position" : 0
},
{
"token" : "国歌",
"start_offset" : 7,
"end_offset" : 9,
"type" : "CN_WORD",
"position" : 1
}
]
}
分词器有时候分词可能不会包含我们想要的,比如说我有一个昵称,ZP很帅,我觉得这几个字应该是一个整体,但是分词器现在给拆开了,需要我们自己添加分词设置。
比如IK分词器的配置添加如下:
先找到分词器的配置文件
IKAnalyzer.cfg.xml,然后添加词库(ext_dict是添加的分词,希望能用上,ext_stopwords是觉得不要分词分出来比如说乎,吗语气词,挥着其他不行让他出现的。)
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE properties SYSTEM "http://java.sun.com/dtd/properties.dtd">
<properties>
<comment>IK Analyzer 扩展配置</comment>
<!--用户可以在这里配置自己的扩展字典 -->
<entry key="ext_dict">zp.dic</entry>
<!--用户可以在这里配置自己的扩展停止词字典-->
<entry key="ext_stopwords"></entry>
<!--用户可以在这里配置远程扩展字典 -->
<!-- <entry key="remote_ext_dict">words_location</entry> -->
<!--用户可以在这里配置远程扩展停止词字典-->
<!-- <entry key="remote_ext_stopwords">words_location</entry> -->
</properties>
分词实战
我们要建立一个自己的索引,然后插入实验数据
(1)建立索引
我建立一个zp的索引
(2)导入数据
PUT /zp
{
"mappings": {
"my_type": {
"properties": {
"text": {
"type": "text",
"analyzer": "ik_max_word"
}
}
}
}
}
POST /zp/my_type/_bulk
{ "index": { "_id": "1"} }
{ "text": "男子偷上万元发红包求交女友 被抓获时仍然单身" }
{ "index": { "_id": "2"} }
{ "text": "16岁少女为结婚“变”22岁 7年后想离婚被法院拒绝" }
{ "index": { "_id": "3"} }
{ "text": "深圳女孩骑车逆行撞奔驰 遭索赔被吓哭(图)" }
{ "index": { "_id": "4"} }
{ "text": "女人对护肤品比对男票好?网友神怼" }
{ "index": { "_id": "5"} }
{ "text": "为什么国内的街道招牌用的都是红黄配?" }
GET /zp/my_type/_search
{
"query": {
"match": {
"text": "16岁少女结婚好还是单身好?"
}
}
}
查询
GET /zp/my_type/_search
{
"query": {
"match": {
"text": "16岁少女结婚好还是单身好?"
}
}
}
命中结果:
{
"took" : 646,
"timed_out" : false,
"_shards" : {
"total" : 5,
"successful" : 5,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 5,
"relation" : "eq"
},
"max_score" : 1.941854,
"hits" : [
{
"_index" : "zp",
"_type" : "my_type",
"_id" : "2",
"_score" : 1.941854,
"_source" : {
"text" : "16岁少女为结婚“变”22岁 7年后想离婚被法院拒绝"
}
},
{
"_index" : "zp",
"_type" : "my_type",
"_id" : "4",
"_score" : 0.8630463,
"_source" : {
"text" : "女人对护肤品比对男票好?网友神怼"
}
},
{
"_index" : "zp",
"_type" : "my_type",
"_id" : "1",
"_score" : 0.8630463,
"_source" : {
"text" : "男子偷上万元发红包求交女友 被抓获时仍然单身"
}
},
{
"_index" : "zp",
"_type" : "my_type",
"_id" : "5",
"_score" : 0.70134467,
"_source" : {
"text" : "为什么国内的街道招牌用的都是红黄配?"
}
},
{
"_index" : "zp",
"_type" : "my_type",
"_id" : "3",
"_score" : 0.685139,
"_source" : {
"text" : "深圳女孩骑车逆行撞奔驰 遭索赔被吓哭(图)"
}
}
]
}
}