安装配置:
从【
https://github.com/medcl/elasticsearch-analysis-ik/releases
】下载【
elasticsearch-analysis-ik-1.6.0.zip】文件。
里面值包含了代码,需要执行【mvn package】进行编译打包,生成【elasticsearch-analysis-ik-1.6.0.jar】文件。
拷贝【target/releases/elasticsearch-analysis-ik-1.6.0.zip】中的文件到【<ES_ROOT>/plugins/ik】目录中。
拷贝【elasticsearch-analysis-ik-1.6.0.zip】中的【config/ik】中的文件到【<ES_ROOT>/config/ik】目录中。
重启 ElasticSearch 即可。
分词器:
IK 包含两类分词器:
- ik_max_word: 会将文本做最细粒度的拆分,比如会将“中华人民共和国国歌”拆分为“中华人民共和国,中华人民,中华,华人,人民共和国,人民,人,民,共和国,共和,和,国国,国歌”,会穷尽各种可能的组合;
- ik_smart: 会做最粗粒度的拆分,比如会将“中华人民共和国国歌”拆分为“中华人民共和国,国歌”。
不同项目可以不同选择,个人是推荐使用【
ik_smart】,相对来说分出来的词更符合直观感受。
使用样例:
1. 先创建名叫【index】的索引:
curl -XPUT http://localhost:9200/index
2. 创建名为【fulltext】的
类型
,并设定其使用【IK】分词器。这一步很重要,必须在往索引中添加数据前完成。
curl -XPOST http://localhost:9200/index/fulltext/_mapping -d'{"fulltext": {"_all": {"analyzer": "ik_max_word","search_analyzer": "ik_max_word","term_vector": "no","store": "false"},"properties": {"content": {"type": "string","store": "no","term_vector": "with_positions_offsets","analyzer": "ik_max_word","search_analyzer": "ik_max_word","include_in_all": "true","boost": 8}}}}'
3. 往索引库中添加数据:
curl -XPOST http://localhost:9200/index/fulltext/1 -d'{"content":"美国留给伊拉克的是个烂摊子吗"}'curl -XPOST http://localhost:9200/index/fulltext/2 -d'{"content":"公安部:各地校车将享最高路权"}'curl -XPOST http://localhost:9200/index/fulltext/3 -d'{"content":"中韩渔警冲突调查:韩警平均每天扣1艘中国渔船"}'curl -XPOST http://localhost:9200/index/fulltext/4 -d'{"content":"中国驻洛杉矶领事馆遭亚裔男子枪击 嫌犯已自首"}'
4. 查询测试:
查询语句:
curl -XPOST http://localhost:9200/index/fulltext/_search -d'{"query" : { "term" : { "content" : "中国" }},"highlight" : {"pre_tags" : ["<tag1>", "<tag2>"],"post_tags" : ["</tag1>", "</tag2>"],"fields" : {"content" : {}}}}'
查询结果:
{"took": 14,"timed_out": false,"_shards": {"total": 5,"successful": 5,"failed": 0},"hits": {"total": 2,"max_score": 2,"hits": [{"_index": "index","_type": "fulltext","_id": "4","_score": 2,"_source": {"content": "中国驻洛杉矶领事馆遭亚裔男子枪击 嫌犯已自首"},"highlight": {"content": ["<tag1>中国</tag1>驻洛杉矶领事馆遭亚裔男子枪击 嫌犯已自首 "]}},{"_index": "index","_type": "fulltext","_id": "3","_score": 2,"_source": {"content": "中韩渔警冲突调查:韩警平均每天扣1艘中国渔船"},"highlight": {"content": ["均每天扣1艘<tag1>中国</tag1>渔船 "]}}]}}
参考:
- https://github.com/medcl/elasticsearch-analysis-ik
- 《Elasticsearch: The Definitive Guide》(电子书)
本文详细介绍了如何在Elasticsearch中安装、配置并使用IK分词器,包括下载、编译、安装步骤及分词器类型选择,并提供了使用样例和查询测试。
1349

被折叠的 条评论
为什么被折叠?



