一、Es插件配置及下载

1.IK分词器的下载安装

关于IK分词器的介绍不再多少,一言以蔽之,IK分词是目前使用非常广泛分词效果比较好的中文分词器。做ES开发的,中文分词十有八九使用的都是IK分词器。

下载地址:https://github.com/medcl/elasticsearch-analysis-ik

2.pinyin分词器的下载安装

可以在淘宝、京东的搜索框中输入pinyin就能查找到自己想要的结果,这就是拼音分词,拼音分词则是将中文分析成拼音格式,可以通过拼音分词分析出来的数据进行查找想要的结果。

下载地址:https://github.com/medcl/elasticsearch-analysis-pinyin

注:插件下载一定要和自己版本对应的Es版本一致,并且安装完插件后需重启Es,才能生效。

插件安装位置:(本人安装了三个插件,暂时先不介绍murmur3插件,可以暂时忽略)

插件配置成功,重启Es

二、拼音分词器和IK分词器的使用

1.IK中文分词器的使用

1.1 ik_smart: 会做最粗粒度的拆分

  1. GET /_analyze
  2. {
  3. "text":"中华人民共和国国徽",
  4. "analyzer":"ik_smart"
  5. }
  6.  
  7. 结果:
  8. {
  9. "tokens": [
  10. {
  11. "token": "中华人民共和国",
  12. "start_offset": 0,
  13. "end_offset": 7,
  14. "type": "CN_WORD",
  15. "position": 0
  16. },
  17. {
  18. "token": "国徽",
  19. "start_offset": 7,
  20. "end_offset": 9,
  21. "type": "CN_WORD",
  22. "position": 1
  23. }
  24. ]
  25. }

1.2  ik_max_word: 会将文本做最细粒度的拆分

  1. GET /_analyze
  2. {
  3. "text": "中华人民共和国国徽",
  4. "analyzer": "ik_max_word"
  5. }
  6.  
  7. 结果:
  8. {
  9. "tokens": [
  10. {
  11. "token": "中华人民共和国",
  12. "start_offset": 0,
  13. "end_offset": 7,
  14. "type": "CN_WORD",
  15. "position": 0
  16. },
  17. {
  18. "token": "中华人民",
  19. "start_offset": 0,
  20. "end_offset": 4,
  21. "type": "CN_WORD",
  22. "position": 1
  23. },
  24. {
  25. "token": "中华",
  26. "start_offset": 0,
  27. "end_offset": 2,
  28. "type": "CN_WORD",
  29. "position": 2
  30. },
  31. {
  32. "token": "华人",
  33. "start_offset": 1,
  34. "end_offset": 3,
  35. "type": "CN_WORD",
  36. "position": 3
  37. },
  38. {
  39. "token": "人民共和国",
  40. "start_offset": 2,
  41. "end_offset": 7,
  42. "type": "CN_WORD",
  43. "position": 4
  44. },
  45. {
  46. "token": "人民",
  47. "start_offset": 2,
  48. "end_offset": 4,
  49. "type": "CN_WORD",
  50. "position": 5
  51. },
  52. {
  53. "token": "共和国",
  54. "start_offset": 4,
  55. "end_offset": 7,
  56. "type": "CN_WORD",
  57. "position": 6
  58. },
  59. {
  60. "token": "共和",
  61. "start_offset": 4,
  62. "end_offset": 6,
  63. "type": "CN_WORD",
  64. "position": 7
  65. },
  66. {
  67. "token": "国",
  68. "start_offset": 6,
  69. "end_offset": 7,
  70. "type": "CN_CHAR",
  71. "position": 8
  72. },
  73. {
  74. "token": "国徽",
  75. "start_offset": 7,
  76. "end_offset": 9,
  77. "type": "CN_WORD",
  78. "position": 9
  79. }
  80. ]
  81. }

2.拼音分词器的使用

  1. GET /_analyze
  2. {
  3. "text":"刘德华",
  4. "analyzer": "pinyin"
  5. }
  6.  
  7. 结果:
  8. {
  9. "tokens": [
  10. {
  11. "token": "liu",
  12. "start_offset": 0,
  13. "end_offset": 1,
  14. "type": "word",
  15. "position": 0
  16. },
  17. {
  18. "token": "ldh",
  19. "start_offset": 0,
  20. "end_offset": 3,
  21. "type": "word",
  22. "position": 0
  23. },
  24. {
  25. "token": "de",
  26. "start_offset": 1,
  27. "end_offset": 2,
  28. "type": "word",
  29. "position": 1
  30. },
  31. {
  32. "token": "hua",
  33. "start_offset": 2,
  34. "end_offset": 3,
  35. "type": "word",
  36. "position": 2
  37. }
  38. ]
  39. }

注:不管是拼音分词器还是IK分词器,当深入搜索一条数据是时,必须是通过分词器分析的数据,才能被搜索到,否则搜索不到

三、IK分词和拼音分词的组合使用

当我们创建索引时可以自定义分词器,通过指定映射去匹配自定义分词器

  1. PUT /my_index
  2. {
  3. "settings": {
  4. "analysis": {
  5. "analyzer": {
  6. "ik_smart_pinyin": {
  7. "type": "custom",
  8. "tokenizer": "ik_smart",
  9. "filter": ["my_pinyin", "word_delimiter"]
  10. },
  11. "ik_max_word_pinyin": {
  12. "type": "custom",
  13. "tokenizer": "ik_max_word",
  14. "filter": ["my_pinyin", "word_delimiter"]
  15. }
  16. },
  17. "filter": {
  18. "my_pinyin": {
  19. "type" : "pinyin",
  20. "keep_separate_first_letter" : true,
  21. "keep_full_pinyin" : true,
  22. "keep_original" : true,
  23. "limit_first_letter_length" : 16,
  24. "lowercase" : true,
  25. "remove_duplicated_term" : true
  26. }
  27. }
  28. }
  29. }
  30.  
  31. }

当我们建type时,需要在字段的analyzer属性填写自己的映射

  1. PUT /my_index/my_type/_mapping
  2. {
  3. "my_type":{
  4. "properties": {
  5. "id":{
  6. "type": "integer"
  7. },
  8. "name":{
  9. "type": "text",
  10. "analyzer": "ik_smart_pinyin"
  11. }
  12. }
  13. }
  14. }

测试,让我们先添加几条数据

  1. POST /my_index/my_type/_bulk
  2. { "index": { "_id":1}}
  3. { "name": "张三"}
  4. { "index": { "_id": 2}}
  5. { "name": "张四"}
  6. { "index": { "_id": 3}}
  7. { "name": "李四"}

IK分词查询

  1. GET /my_index/my_type/_search
  2. {
  3. "query": {
  4. "match": {
  5. "name": "李"
  6. }
  7. }
  8. }
  9.  
  10. 结果:
  11. {
  12. "took": 3,
  13. "timed_out": false,
  14. "_shards": {
  15. "total": 5,
  16. "successful": 5,
  17. "skipped": 0,
  18. "failed": 0
  19. },
  20. "hits": {
  21. "total": 1,
  22. "max_score": 0.47160998,
  23. "hits": [
  24. {
  25. "_index": "my_index",
  26. "_type": "my_type",
  27. "_id": "3",
  28. "_score": 0.47160998,
  29. "_source": {
  30. "name": "李四"
  31. }
  32. }
  33. ]
  34. }
  35. }

拼音分词查询:

  1. GET /my_index/my_type/_search
  2. {
  3. "query": {
  4. "match": {
  5. "name": "zhang"
  6. }
  7. }
  8. }
  9.  
  10. 结果:
  11. {
  12. "took": 1,
  13. "timed_out": false,
  14. "_shards": {
  15. "total": 5,
  16. "successful": 5,
  17. "skipped": 0,
  18. "failed": 0
  19. },
  20. "hits": {
  21. "total": 2,
  22. "max_score": 0.3758317,
  23. "hits": [
  24. {
  25. "_index": "my_index",
  26. "_type": "my_type",
  27. "_id": "2",
  28. "_score": 0.3758317,
  29. "_source": {
  30. "name": "张四"
  31. }
  32. },
  33. {
  34. "_index": "my_index",
  35. "_type": "my_type",
  36. "_id": "1",
  37. "_score": 0.3758317,
  38. "_source": {
  39. "name": "张三"
  40. }
  41. }
  42. ]
  43. }
  44. }

注:搜索时,先查看被搜索的词被分析成什么样的数据,如果你搜索该词输入没有被分析出的参数时,是查不到的!!!!

Elasticsearch拼音分词和IK分词的安装及使用的更多相关文章

  1. 使用Docker 安装Elasticsearch、Elasticsearch-head、IK分词器 和使用

    原文:使用Docker 安装Elasticsearch.Elasticsearch-head.IK分词器 和使用 Elasticsearch的安装 一.elasticsearch的安装 1.镜像拉取 ...

  2. ElasticSearch已经配置好ik分词和mmseg分词(转)

    ElasticSearch是一个基于Lucene构建的开源,分布式,RESTful搜索引擎.设计用于云计算中,能够达到实时搜索,稳定,可靠,快速,安装使用方便.支持通过HTTP使用JSON进行数据索引 ...

  3. Elasticsearch集群使用ik分词器

    IK分词插件的安装 ES集群环境 VMWare下三台虚拟机Ubuntu 14.04.2 LTS JDK 1.8.0_66 Elasticsearch 2.3.1 elasticsearch-jdbc- ...

  4. ElasticSearch中文分词器-IK分词器的使用

    IK分词器的使用 首先我们通过Postman发送GET请求查询分词效果 GET http://localhost:9200/_analyze { "text":"农业银行 ...

  5. ElasticSearch(六):IK分词器的安装与使用IK分词器创建索引

    之前我们创建索引,查询数据,都是使用的默认的分词器,分词效果不太理想,会把text的字段分成一个一个汉字,然后搜索的时候也会把搜索的句子进行分词,所以这里就需要更加智能的分词器IK分词器了. 1. i ...

  6. Windows10安装Elasticsearch IK分词插件

    安装插件 cmd切换到Elasticsearch安装目录下 C:\Users\Administrator>D: D:\>cd D:\Program Files\Elastic\Elasti ...

  7. Docker 下Elasticsearch 的安装 和ik分词器

    (1)docker镜像下载 docker pull elasticsearch:5.6.8 (2)安装es容器 docker run -di --name=changgou_elasticsearch ...

  8. Elasticsearch拼音和ik分词器的结合应用

    一.创建索引时,自定义拼音分词和ik分词 PUT /my_index { "index": { "analysis": { "analyzer&quo ...

  9. ElasticSearch 中文分词插件ik 的使用

    下载 IK 的版本要与 Elasticsearch 的版本一致,因此下载 7.1.0 版本. 安装 1.中文分词插件下载地址:https://github.com/medcl/elasticsearc ...

随机推荐

  1. 【详解】Tomcat是如何监控并删除超时Session的?

    前言 偶然发现Tomcat会话时间的半小时,并不是说会话创建后,只有半小时的有效使用时间,而是说会话空闲半小时后,会被删除.索性就翻了一下源码.做了一番整理. 注:空闲时间,指的是同一个会话两次请求之 ...

  2. IDEA的几个常用配置,日常开发必备。

    用了IDEA有很长时间了,身边的同事朋友也都慢慢的开始都从Eclipse切换到IDEA了,其实无论是Eclipse还是IntelliJ IDEA都是开发工具而已,各自都有优点.但是刚从Eclipse切 ...

  3. Visual Studio 2017 - Windows应用程序打包成exe文件(2)- Advanced Installer

    Advanced Installer :Free for 30 days. All features. 下载地址:https://www.advancedinstaller.com/download. ...

  4. NGUI 做局部2d卷轴

    网上找到的都是做整个背景的卷轴动画,通常是改变纹理位置或者背景图片的x坐标 没有提到在UI界面里某个部分做卷轴动画,找了很久,才发现NGUI的Panel里的Clipping属性可以裁剪Panel的大小 ...

  5. [PHP]算法-队列结构的PHP实现

    题目描述 用两个栈来实现一个队列,完成队列的Push和Pop操作. 队列中的元素为int类型. 思路: 1.php数组完全就能实现 2.array_push 从尾部往里压入元素 3.array_shi ...

  6. springMVC_07乱码及restful风格

    乱码的解决 通过过滤器解决乱码问题:CharacterEncodingFilter 配置web.xml文件 <filter> <filter-name>encoding< ...

  7. 【转】Mybatis源码解读-设计模式总结

    原文:http://www.crazyant.net/2022.html?jqbmtw=b90da1&gsjulo=kpzaa1 虽然我们都知道有26个设计模式,但是大多停留在概念层面,真实开 ...

  8. Root(hdu5777+扩展欧几里得+原根)2015 Multi-University Training Contest 7

    Root Time Limit: 30000/15000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others)Total Su ...

  9. Aquarium Tank(csu1634+几何+二分)Contest2087 - 湖南多校对抗赛(2015.05.24)-G

    Aquarium Tank Time Limit: 1 Sec  Memory Limit: 128 MBSubmit: 15  Solved: 4[Submit][Status][Web Board ...

  10. JavaScript是如何工作的:Service Worker的生命周期及使用场景

    摘要: 理解Service Worker. 原文:JavaScript 是如何工作的:Service Worker 的生命周期及使用场景 作者:前端小智 Fundebug经授权转载,版权归原作者所有. ...