IK和pinyin分词器
一、拼音分词的应用
拼音分词在日常生活中其实很常见,也许你每天都在用。打开淘宝看一看吧,输入拼音”zhonghua”,下面会有包含”zhonghua”对应的中文”中华”的商品的提示:
拼音分词是根据输入的拼音提示对应的中文,通过拼音分词提升搜索体验、加快搜索速度。下面介绍如何在Elasticsearch 5.1.1中配置和实现pinyin+iK分词。
二、IK分词器下载与安装
关于IK分词器的介绍不再多少,一言以蔽之,IK分词是目前使用非常广泛分词效果比较好的中文分词器。做ES开发的,中文分词十有八九使用的都是IK分词器。
下载地址:https://github.com/medcl/elasticsearch-analysis-ik
配置之前关闭elasticsearch,配置完成以后再重启。
IK的版本要和当前ES的版本一致,README中有说明。我使用的是ES是5.1.1,IK的版本为5.1.1(你也许会奇怪为什么IK上一个版本是1.X,下一个版本一下升到5.X?是因为Elastic官方为了统一版本号,之前es的版本是2.x,logstash的版本是2.x,同时Kibana的版本是4.x,ik的版本是1.x,这样版本很混乱。5.0之后,统一版本号,这样你使用5.1.1的es,其它软件的版本也使用5.1.1就好了)。
下载之后进入到elasticsearch-analysis-pinyin-master目录,mvn打包(没有安装maven的自行安装),运行命令:
mvn package
- 1
打包成功以后,会生成一个target文件夹,在elasticsearch-analysis-ik-master/target/releases目录下,找到elasticsearch-analysis-ik-5.1.1.zip,这就是我们需要的安装文件。解压elasticsearch-analysis-ik-5.1.1.zip,得到下面内容:
commons-codec-1.9.jar
commons-logging-1.2.jar
config
elasticsearch-analysis-ik-5.1.1.jar
httpclient-4.5.2.jar
httpcore-4.4.4.jar
plugin-descriptor.properties
- 1
- 2
- 3
- 4
- 5
- 6
- 7
然后在elasticsearch-5.1.1/plugins目录下新建一个文件夹ik,把elasticsearch-analysis-ik-5.1.1.zip解压后的文件拷贝到elasticsearch-5.1.1/plugins/ik目录下.截图方便理解。
三、pinyin分词器下载与安装
pinyin分词器的下载地址:
https://github.com/medcl/elasticsearch-analysis-pinyin
安装过程和IK一样,下载、打包、加入ES。这里不在重复上述步骤,给出最后配置截图
四、分词测试
IK和pinyin分词配置完成以后,重启ES。如果重启过程中ES报错,说明安装有错误,没有报错说明配置成功。
4.1 IK分词测试
创建一个索引:
curl -XPUT "http://localhost:9200/index"
- 1
测试分词效果:
curl -XPOST "http://localhost:9200/index/_analyze?analyzer=ik_max_word&text=中华人民共和国"
- 1
分词结果:
{"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}]
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
使用ik_smart分词:
curl -XPOST "http://localhost:9200/index/_analyze?analyzer=ik_smart&text=中华人民共和国"
- 1
分词结果:
{"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}]
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
截图方便理解:
4.2拼音分词测试
测试拼音分词:
curl -XPOST "http://localhost:9200/index/_analyze?analyzer=pinyin&text=张学友"
- 1
分词结果:
{"tokens": [{"token": "zhang","start_offset": 0,"end_offset": 1,"type": "word","position": 0}, {"token": "xue","start_offset": 1,"end_offset": 2,"type": "word","position": 1}, {"token": "you","start_offset": 2,"end_offset": 3,"type": "word","position": 2}, {"token": "zxy","start_offset": 0,"end_offset": 3,"type": "word","position": 3}]
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
五、IK+pinyin分词配置
5.1创建索引与分析器设置
创建一个索引,并设置index分析器相关属性:
curl -XPUT "http://localhost:9200/medcl/" -d'
{"index": {"analysis": {"analyzer": {"ik_pinyin_analyzer": { "type": "custom", "tokenizer": "ik_smart", "filter": ["my_pinyin", "word_delimiter"] }},"filter": {"my_pinyin": { "type": "pinyin", "first_letter": "prefix", "padding_char": " " }}}}
}'
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
创建一个type并设置mapping:
curl -XPOST http://localhost:9200/medcl/folks/_mapping -d'
{"folks": {"properties": {"name": {"type": "keyword","fields": {"pinyin": {"type": "text","store": "no","term_vector": "with_positions_offsets","analyzer": "ik_pinyin_analyzer","boost": 10}}}}}
}'
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
5.2索引测试文档
索引2份测试文档。
文档1:
curl -XPOST http://localhost:9200/medcl/folks/andy -d'{"name":"刘德华"}'
- 1
文档2:
curl -XPOST http://localhost:9200/medcl/folks/tina -d'{"name":"中华人民共和国国歌"}'
- 1
5.3测试(1)拼音分词
下面四条命命令都可以匹配”刘德华”
curl -XPOST "http://localhost:9200/medcl/folks/_search?q=name.pinyin:liu"curl -XPOST "http://localhost:9200/medcl/folks/_search?q=name.pinyin:de"curl -XPOST "http://localhost:9200/medcl/folks/_search?q=name.pinyin:hua"curl -XPOST "http://localhost:9200/medcl/folks/_search?q=name.pinyin:ldh"
- 1
- 2
- 3
- 4
- 5
- 6
- 7
5.4测试(2)IK分词测试
curl -XPOST "http://localhost:9200/medcl/_search?pretty" -d'
{"query": {"match": {"name.pinyin": "国歌"}},"highlight": {"fields": {"name.pinyin": {}}}
}'
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
返回结果:
{"took" : 2,"timed_out" : false,"_shards" : {"total" : 5,"successful" : 5,"failed" : 0},"hits" : {"total" : 1,"max_score" : 16.698704,"hits" : [{"_index" : "medcl","_type" : "folks","_id" : "tina","_score" : 16.698704,"_source" : {"name" : "中华人民共和国国歌"},"highlight" : {"name.pinyin" : ["<em>中华人民共和国</em><em>国歌</em>"]}}]}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
说明IK分词器起到了效果。
5.3测试(4)pinyin+ik分词测试:
curl -XPOST "http://localhost:9200/medcl/_search?pretty" -d'
{"query": {"match": {"name.pinyin": "zhonghua"}},"highlight": {"fields": {"name.pinyin": {}}}
}'
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
返回结果:
{"took" : 3,"timed_out" : false,"_shards" : {"total" : 5,"successful" : 5,"failed" : 0},"hits" : {"total" : 2,"max_score" : 5.9814634,"hits" : [{"_index" : "medcl","_type" : "folks","_id" : "tina","_score" : 5.9814634,"_source" : {"name" : "中华人民共和国国歌"},"highlight" : {"name.pinyin" : ["<em>中华人民共和国</em>国歌"]}},{"_index" : "medcl","_type" : "folks","_id" : "andy","_score" : 2.2534127,"_source" : {"name" : "刘德华"},"highlight" : {"name.pinyin" : ["<em>刘德华</em>"]}}]}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
截图如下:
使用pinyin分词以后,原始的字段搜索要加上.pinyin后缀,搜索原始字段没有返回结果:
六、参考资料
- https://github.com/medcl/elasticsearch-analysis-ik
- https://github.com/medcl/elasticsearch-analysis-pinyin
- https://my.oschina.net/xiaohui249/blog/214505
IK和pinyin分词器相关推荐
- elasticsearch中文IK+Pinyin分词器
2019独角兽企业重金招聘Python工程师标准>>> 一.IK分词器安装 1.分词器的作用 分词顾名思义,就是把一句话分成一个一个的词.这个概念在搜索中很重要,比如 This is ...
- 【ES】ES 拼音 Pinyin 分词器
1.概述 转载:https://www.cnblogs.com/sanduzxcvbnm/p/12083606.html Elastic的Medcl提供了一种搜索Pinyin搜索的方法.拼音搜索在很多 ...
- ik与拼音分词器,拓展热词/停止词库
说明:本篇文章讲述elasticsearch分词器插件的安装,热词库停止词库的拓展,文章后面提到elasticsearch ,都是以es简称. 以下分词器的安装以ik分词器和pinyin分词器为例说明 ...
- Elasticsearch:Pinyin 分词器
Elastic 的 Medcl 提供了一种搜索 Pinyin 搜索的方法.拼音搜索在很多的应用场景中都有被用到.比如在百度搜索中,我们使用拼音就=可以出现汉字: 对于我们中国人来说,拼音搜索也是非常直 ...
- 采访IK Analyzer 中文分词器开源项目作者林良益(十三)
转自: http://www.iteye.com/magazines/43-ik-analyzer 众所周知,全文搜索几乎已经成为每个网站的必须提供的基本功能之一,用Lucene构造一个"索 ...
- elasticsearch-7.15.2 同时支持中文ik分词器和pinyin分词器
文章目录 1. 自定义分词器 2. 映射模型 3. 效果图 1. 自定义分词器 ES如何支持拼音和中文分词 ? 自定义分词器 支持拼音和中文分词 PUT /jd_goods {"settin ...
- elasticsearch-7.15.2 集成pinyin分词器
文章目录 1. 下载拼音分词器 2. es集成pinyin 3. 启动es 4. pinyin分词 5. 效果图 6. 开源项目 1. 下载拼音分词器 链接:https://github.com/me ...
- IK Analyzer 中文分词器
IK Analyzer是一个开源的,基于java语言开发的轻量级的中文分词工具包.从2006年12月推出1.0版开始, IKAnalyzer已经推出了3个大版本.最初,它是以开源项目Luence为应用 ...
- lucene6中配置IK Analyzer同义词分词器
首先需要基础知识 intellij idea中为lucene6配置IK Analyzer分词器 说到同义词分词器,从原理角度来说要了解了Analyzer Analyzer分词的原理 Analyzer类 ...
- ik php分词,ik中英文混合分词器
3.重启Elasticsearch 4.测试分词器 GET /_analyze { "text": "中华人民共和国国徽", # 两种:ik_smart较粗粒度 ...
最新文章
- python 清华镜像_Python pip conda 设置为清华镜像
- java 面试题汇总
- ubuntu 安装gcc 4.8.2
- a 中调用js的几种方法整理及使用推荐
- Less 命令技巧,从底部网上看
- days to_days_Java2Days 2012:Java EE
- 从容 IT 人生路,开发工具伴我行——“葡萄城 30 周年”征文
- 经典算法大全之河内之塔
- 触发器、锁存器、寄存器以及它们之间的区别
- day34 GIL锁,线程队列,线程池
- soapUI Pro 4.5.1的新破解方案
- PSPNet 使用问题
- java 毫秒数一天_java用毫秒数做日期计算的一个踩坑记录
- hdu 1757(矩阵快速幂)
- 微信内置浏览器网页刷新
- cad app android,迷你CAD免费手机版
- latex排版[4]:子公式分别编号
- 【2023 阿里云云计算工程师 ACP 认证练习题库】01、VPC 专有网络题库
- 未约定经济补偿的竞业禁止条款是否无效
- 算法设计与分析:蛮力法