理解ElasticSearch的中文分词器【IK】(版本:1.6.0)

安装配置:

从【 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 包含两类分词器:
  1. ik_max_word: 会将文本做最细粒度的拆分,比如会将“中华人民共和国国歌”拆分为“中华人民共和国,中华人民,中华,华人,人民共和国,人民,人,民,共和国,共和,和,国国,国歌”,会穷尽各种可能的组合;
  2. ik_smart: 会做最粗粒度的拆分,比如会将“中华人民共和国国歌”拆分为“中华人民共和国,国歌”。
不同项目可以不同选择,个人是推荐使用【 ik_smart】,相对来说分出来的词更符合直观感受。

使用样例:

1. 先创建名叫【index】的索引:
   
   
  1. curl -XPUT http://localhost:9200/index

2. 创建名为【fulltext】的 类型 ,并设定其使用【IK】分词器。这一步很重要,必须在往索引中添加数据前完成。
   
   
  1. curl -XPOST http://localhost:9200/index/fulltext/_mapping -d'
  2. {
  3. "fulltext": {
  4. "_all": {
  5. "analyzer": "ik_max_word",
  6. "search_analyzer": "ik_max_word",
  7. "term_vector": "no",
  8. "store": "false"
  9. },
  10. "properties": {
  11. "content": {
  12. "type": "string",
  13. "store": "no",
  14. "term_vector": "with_positions_offsets",
  15. "analyzer": "ik_max_word",
  16. "search_analyzer": "ik_max_word",
  17. "include_in_all": "true",
  18. "boost": 8
  19. }
  20. }
  21. }
  22. }'

3. 往索引库中添加数据:
   
   
  1. curl -XPOST http://localhost:9200/index/fulltext/1 -d'{"content":"美国留给伊拉克的是个烂摊子吗"}'
  2. curl -XPOST http://localhost:9200/index/fulltext/2 -d'{"content":"公安部:各地校车将享最高路权"}'
  3. curl -XPOST http://localhost:9200/index/fulltext/3 -d'{"content":"中韩渔警冲突调查:韩警平均每天扣1艘中国渔船"}'
  4. curl -XPOST http://localhost:9200/index/fulltext/4 -d'{"content":"中国驻洛杉矶领事馆遭亚裔男子枪击 嫌犯已自首"}'

4. 查询测试:

查询语句:
   
   
  1. curl -XPOST http://localhost:9200/index/fulltext/_search -d'
  2. {
  3. "query" : { "term" : { "content" : "中国" }},
  4. "highlight" : {
  5. "pre_tags" : ["<tag1>", "<tag2>"],
  6. "post_tags" : ["</tag1>", "</tag2>"],
  7. "fields" : {
  8. "content" : {}
  9. }
  10. }
  11. }
  12. '

查询结果:
   
   
  1. {
  2. "took": 14,
  3. "timed_out": false,
  4. "_shards": {
  5. "total": 5,
  6. "successful": 5,
  7. "failed": 0
  8. },
  9. "hits": {
  10. "total": 2,
  11. "max_score": 2,
  12. "hits": [
  13. {
  14. "_index": "index",
  15. "_type": "fulltext",
  16. "_id": "4",
  17. "_score": 2,
  18. "_source": {
  19. "content": "中国驻洛杉矶领事馆遭亚裔男子枪击 嫌犯已自首"
  20. },
  21. "highlight": {
  22. "content": [
  23. "<tag1>中国</tag1>驻洛杉矶领事馆遭亚裔男子枪击 嫌犯已自首 "
  24. ]
  25. }
  26. },
  27. {
  28. "_index": "index",
  29. "_type": "fulltext",
  30. "_id": "3",
  31. "_score": 2,
  32. "_source": {
  33. "content": "中韩渔警冲突调查:韩警平均每天扣1艘中国渔船"
  34. },
  35. "highlight": {
  36. "content": [
  37. "均每天扣1艘<tag1>中国</tag1>渔船 "
  38. ]
  39. }
  40. }
  41. ]
  42. }
  43. }

参考:

  1. https://github.com/medcl/elasticsearch-analysis-ik
  2. 《Elasticsearch: The Definitive Guide》(电子书)
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值