概述

这个分词器在对中文人名药品名 等检索的时候很犀利,无脑的将词分隔成成为几个字连接起来,比如配置一个分词器。
先将定义的my_tokenizer添加到索引my-index中,代码如下所示

  1. PUT my-index
  2. {
  3. "settings": {
  4. "analysis": {
  5. "analyzer": {
  6. "my_analyzer": {
  7. "tokenizer": "my_tokenizer"
  8. }
  9. },
  10. "tokenizer": {
  11. "my_tokenizer": {
  12. "type": "ngram",
  13. "min_gram": 2,
  14. "max_gram": 3,
  15. "token_chars": [
  16. "letter",
  17. "digit"
  18. ]}}}}}

其中my_tokenizer有 2 个参数min_gram 表示分词的最小分多少,max_gram表示分词最大分多少。

中文人名

接下来我们来测试下

  1. POST my-index/_analyze
  2. {
  3. "analyzer": "my_analyzer",
  4. "text": "梁朝伟"
  5. }

结果是

  1. {
  2. "tokens" : [
  3. {
  4. "token" : "梁朝",
  5. "start_offset" : 0,
  6. "end_offset" : 2,
  7. "type" : "word",
  8. "position" : 0
  9. },
  10. {
  11. "token" : "梁朝伟",
  12. "start_offset" : 0,
  13. "end_offset" : 3,
  14. "type" : "word",
  15. "position" : 1
  16. },
  17. {
  18. "token" : "朝伟",
  19. "start_offset" : 1,
  20. "end_offset" : 3,
  21. "type" : "word",
  22. "position" : 2
  23. }
  24. ]
  25. }

这种方式在检索名字的时候,特别有效。

药物名

  1. POST my-index/_analyze
  2. {
  3. "analyzer": "my_analyzer",
  4. "text": "阿莫西林克拉维酸钾"
  5. }

结果

  1. {
  2. "tokens" : [
  3. {
  4. "token" : "阿莫",
  5. "start_offset" : 0,
  6. "end_offset" : 2,
  7. "type" : "word",
  8. "position" : 0
  9. },
  10. {
  11. "token" : "阿莫西",
  12. "start_offset" : 0,
  13. "end_offset" : 3,
  14. "type" : "word",
  15. "position" : 1
  16. },
  17. {
  18. "token" : "莫西",
  19. "start_offset" : 1,
  20. "end_offset" : 3,
  21. "type" : "word",
  22. "position" : 2
  23. },
  24. {
  25. "token" : "莫西林",
  26. "start_offset" : 1,
  27. "end_offset" : 4,
  28. "type" : "word",
  29. "position" : 3
  30. },
  31. {
  32. "token" : "西林",
  33. "start_offset" : 2,
  34. "end_offset" : 4,
  35. "type" : "word",
  36. "position" : 4
  37. },
  38. {
  39. "token" : "西林克",
  40. "start_offset" : 2,
  41. "end_offset" : 5,
  42. "type" : "word",
  43. "position" : 5
  44. },
  45. {
  46. "token" : "林克",
  47. "start_offset" : 3,
  48. "end_offset" : 5,
  49. "type" : "word",
  50. "position" : 6
  51. },
  52. {
  53. "token" : "林克拉",
  54. "start_offset" : 3,
  55. "end_offset" : 6,
  56. "type" : "word",
  57. "position" : 7
  58. },
  59. {
  60. "token" : "克拉",
  61. "start_offset" : 4,
  62. "end_offset" : 6,
  63. "type" : "word",
  64. "position" : 8
  65. },
  66. {
  67. "token" : "克拉维",
  68. "start_offset" : 4,
  69. "end_offset" : 7,
  70. "type" : "word",
  71. "position" : 9
  72. },
  73. {
  74. "token" : "拉维",
  75. "start_offset" : 5,
  76. "end_offset" : 7,
  77. "type" : "word",
  78. "position" : 10
  79. },
  80. {
  81. "token" : "拉维酸",
  82. "start_offset" : 5,
  83. "end_offset" : 8,
  84. "type" : "word",
  85. "position" : 11
  86. },
  87. {
  88. "token" : "维酸",
  89. "start_offset" : 6,
  90. "end_offset" : 8,
  91. "type" : "word",
  92. "position" : 12
  93. },
  94. {
  95. "token" : "维酸钾",
  96. "start_offset" : 6,
  97. "end_offset" : 9,
  98. "type" : "word",
  99. "position" : 13
  100. },
  101. {
  102. "token" : "酸钾",
  103. "start_offset" : 7,
  104. "end_offset" : 9,
  105. "type" : "word",
  106. "position" : 14
  107. }
  108. ]
  109. }

这样搜索就非常简单了。