当前位置:   article > 正文

ElasticSearch安装ik分词器

elasticsearch安装ik分词器

1、查看es版本

        直接请求9200端口可以查到基本信息

2、获取插件

https://github.com/medcl/elasticsearch-analysis-ik/releases

下载对应版本的插件zip文件

3、安装

        在es安装目录中bin二进制文件目录中执行安装命令

./elasticsearch-plugin install file:///home/manshow/elasticsearch-analysis-ik-8.7.0.zip 

   

 4、重启es

systemctl restart elasticsearch

 5、验证

    建立一个测试索引:

curl -XPUT http://localhost:9200/test

普通分词器: 

  1. curl -H "Content-Type: application/json" -XPOST 'http://localhost:9200/test/_analyze?pretty' -d '{"text":"我们都练习了两年半,是真ikun"}'
  2. 返回:
  3. {
  4. "tokens" : [
  5. {
  6. "token" : "我",
  7. "start_offset" : 0,
  8. "end_offset" : 1,
  9. "type" : "<IDEOGRAPHIC>",
  10. "position" : 0
  11. },
  12. {
  13. "token" : "们",
  14. "start_offset" : 1,
  15. "end_offset" : 2,
  16. "type" : "<IDEOGRAPHIC>",
  17. "position" : 1
  18. },
  19. {
  20. "token" : "都",
  21. "start_offset" : 2,
  22. "end_offset" : 3,
  23. "type" : "<IDEOGRAPHIC>",
  24. "position" : 2
  25. },
  26. {
  27. "token" : "练",
  28. "start_offset" : 3,
  29. "end_offset" : 4,
  30. "type" : "<IDEOGRAPHIC>",
  31. "position" : 3
  32. },
  33. {
  34. "token" : "习",
  35. "start_offset" : 4,
  36. "end_offset" : 5,
  37. "type" : "<IDEOGRAPHIC>",
  38. "position" : 4
  39. },
  40. {
  41. "token" : "了",
  42. "start_offset" : 5,
  43. "end_offset" : 6,
  44. "type" : "<IDEOGRAPHIC>",
  45. "position" : 5
  46. },
  47. {
  48. "token" : "两",
  49. "start_offset" : 6,
  50. "end_offset" : 7,
  51. "type" : "<IDEOGRAPHIC>",
  52. "position" : 6
  53. },
  54. {
  55. "token" : "年",
  56. "start_offset" : 7,
  57. "end_offset" : 8,
  58. "type" : "<IDEOGRAPHIC>",
  59. "position" : 7
  60. },
  61. {
  62. "token" : "半",
  63. "start_offset" : 8,
  64. "end_offset" : 9,
  65. "type" : "<IDEOGRAPHIC>",
  66. "position" : 8
  67. },
  68. {
  69. "token" : "是",
  70. "start_offset" : 10,
  71. "end_offset" : 11,
  72. "type" : "<IDEOGRAPHIC>",
  73. "position" : 9
  74. },
  75. {
  76. "token" : "真",
  77. "start_offset" : 11,
  78. "end_offset" : 12,
  79. "type" : "<IDEOGRAPHIC>",
  80. "position" : 10
  81. },
  82. {
  83. "token" : "ikun",
  84. "start_offset" : 12,
  85. "end_offset" : 16,
  86. "type" : "<ALPHANUM>",
  87. "position" : 11
  88. }
  89. ]
  90. }

ik分词器

  1. curl -H "Content-Type: application/json" -XPOST 'http://localhost:9200/test/_analyze?pretty' -d '{"text":"我们都练习了两年半,是真ikun","tokenizer":"ik_max_word"}'
  2. 返回:
  3. {
  4. "tokens" : [
  5. {
  6. "token" : "我们",
  7. "start_offset" : 0,
  8. "end_offset" : 2,
  9. "type" : "CN_WORD",
  10. "position" : 0
  11. },
  12. {
  13. "token" : "都",
  14. "start_offset" : 2,
  15. "end_offset" : 3,
  16. "type" : "CN_CHAR",
  17. "position" : 1
  18. },
  19. {
  20. "token" : "练习",
  21. "start_offset" : 3,
  22. "end_offset" : 5,
  23. "type" : "CN_WORD",
  24. "position" : 2
  25. },
  26. {
  27. "token" : "了",
  28. "start_offset" : 5,
  29. "end_offset" : 6,
  30. "type" : "CN_CHAR",
  31. "position" : 3
  32. },
  33. {
  34. "token" : "两年",
  35. "start_offset" : 6,
  36. "end_offset" : 8,
  37. "type" : "CN_WORD",
  38. "position" : 4
  39. },
  40. {
  41. "token" : "两",
  42. "start_offset" : 6,
  43. "end_offset" : 7,
  44. "type" : "COUNT",
  45. "position" : 5
  46. },
  47. {
  48. "token" : "年半",
  49. "start_offset" : 7,
  50. "end_offset" : 9,
  51. "type" : "CN_WORD",
  52. "position" : 6
  53. },
  54. {
  55. "token" : "是",
  56. "start_offset" : 10,
  57. "end_offset" : 11,
  58. "type" : "CN_CHAR",
  59. "position" : 7
  60. },
  61. {
  62. "token" : "真",
  63. "start_offset" : 11,
  64. "end_offset" : 12,
  65. "type" : "CN_CHAR",
  66. "position" : 8
  67. },
  68. {
  69. "token" : "ikun",
  70. "start_offset" : 12,
  71. "end_offset" : 16,
  72. "type" : "ENGLISH",
  73. "position" : 9
  74. }
  75. ]
  76. }

6、使用IK分词器创建索引格式

  1. PUT请求 http://localhost:9200/books
  2. 请求参数如下(注意是json格式的参数)
  3. {
  4. "mappings":{ #定义mappings属性,替换创建索引时对应的mappings属性
  5. "properties":{ #定义索引中包含的属性设置
  6. "id":{ #设置索引中包含id属性
  7. "type":"keyword" #当前属性可以被直接搜索
  8. },
  9. "name":{ #设置索引中包含name属性
  10. "type":"text", #当前属性是文本信息,参与分词
  11. "analyzer":"ik_max_word", #使用IK分词器进行分词
  12. "copy_to":"all" #分词结果拷贝到all属性中
  13. },
  14. "type":{
  15. "type":"keyword"
  16. },
  17. "description":{
  18. "type":"text",
  19. "analyzer":"ik_max_word",
  20. "copy_to":"all"
  21. },
  22. "all":{ #定义属性,用来描述多个字段的分词结果集合,当前属性可以参与查询
  23. "type":"text",
  24. "analyzer":"ik_max_word"
  25. }
  26. }
  27. }
  28. }

声明:本文内容由网友自发贡献,转载请注明出处:【wpsshop】
推荐阅读
相关标签
  

闽ICP备14008679号