当前位置:   article > 正文

Elasticsearch搜索中文分词优化-ik_max_word,和ik_smart模式_ik max word

ik max word

Elasticsearch 中文搜索时遇到几个问题:

    1. 当搜索关键词如:“人民币”时,如果分词将“人民币”分成“人”,“民”,“币”三个单字,那么搜索该关键词会匹配到很多包含该单字的无关内容,但是如果将该词分词成一个整词“人民币”,搜索单字如“人”字又不会匹配到包含“人民币”关键词的内容,怎么解决这个问题,既保证覆盖度又保证准确度?
    1. 搜索“RMB”时只会匹配到包含“RMB”关键词的内容,实际上,“RMB”和“人民币”是同义词,我们希望用户搜索“RMB”和“人民币”可以相互匹配,ES同义词怎么配置?
    1. 用户搜索拼音: 如"baidu",或者拼音首字母"bd",怎么匹配到"百度"这个关键词,又如用户输入"摆渡"这个词也能匹配到"百度"关键词,中文拼音匹配怎么做到?
    1. 怎么保证搜索关键词被正确分词,通常我们会采用自定义词典来做,那么怎么获取自定义词典?

1.IK 分词器

一、ik_max_word和 ik_smart介绍

学习过Solr或Elasticsearch的同学都知道IK分词器,它是一个针对中文的分词器。

IK分词器地址:medcl/elasticsearch-analysis-ik

IK分词器有两种分词模式:ik_max_word和ik_smart模式。

1、ik_max_word

会将文本做最细粒度的拆分,比如会将“中华人民共和国人民大会堂”拆分为“中华人民共和国、中华人民、中华、华人、人民共和国、人民、共和国、大会堂、大会、会堂等词语。

2、ik_smart

会做最粗粒度的拆分,比如会将“中华人民共和国人民大会堂”拆分为中华人民共和国、人民大会堂。

测试两种分词模式的效果:

发送:post localhost:9200/_analyze

测试ik_max_word

{“text”:“中华人民共和国人民大会堂”,“analyzer”:“ik_max_word” }

  1. {
  2. "tokens": [
  3. {
  4. "token": "中华人民共和国",
  5. "start_offset": 0,
  6. "end_offset": 7,
  7. "type": "CN_WORD",
  8. "position": 0
  9. }
  10. ,
  11. {
  12. "token": "中华人民",
  13. "start_offset": 0,
  14. "end_offset": 4,
  15. "type": "CN_WORD",
  16. "position": 1
  17. }
  18. ,
  19. {
  20. "token": "中华",
  21. "start_offset": 0,
  22. "end_offset": 2,
  23. "type": "CN_WORD",
  24. "position": 2
  25. }
  26. ,
  27. {
  28. "token": "华人",
  29. "start_offset": 1,
  30. "end_offset": 3,
  31. "type": "CN_WORD",
  32. "position": 3
  33. }
  34. ,
  35. {
  36. "token": "人民共和国",
  37. "start_offset": 2,
  38. "end_offset": 7,
  39. "type": "CN_WORD",
  40. "position": 4
  41. }
  42. ,
  43. {
  44. "token": "人民",
  45. "start_offset": 2,
  46. "end_offset": 4,
  47. "type": "CN_WORD",
  48. "position": 5
  49. }
  50. ,
  51. {
  52. "token": "共和国",
  53. "start_offset": 4,
  54. "end_offset": 7,
  55. "type": "CN_WORD",
  56. "position": 6
  57. }
  58. ,
  59. {
  60. "token": "共和",
  61. "start_offset": 4,
  62. "end_offset": 6,
  63. "type": "CN_WORD",
  64. "position": 7
  65. }
  66. ,
  67. {
  68. "token": "国人",
  69. "start_offset": 6,
  70. "end_offset": 8,
  71. "type": "CN_WORD",
  72. "position": 8
  73. }
  74. ,
  75. {
  76. "token": "人民大会堂",
  77. "start_offset": 7,
  78. "end_offset": 12,
  79. "type": "CN_WORD",
  80. "position": 9
  81. }
  82. ,
  83. {
  84. "token": "人民大会",
  85. "start_offset": 7,
  86. "end_offset": 11,
  87. "type": "CN_WORD",
  88. "position": 10
  89. }
  90. ,
  91. {
  92. "token": "人民",
  93. "start_offset": 7,
  94. "end_offset": 9,
  95. "type": "CN_WORD",
  96. "position": 11
  97. }
  98. ,
  99. {
  100. "token": "大会堂",
  101. "start_offset": 9,
  102. "end_offset": 12,
  103. "type": "CN_WORD",
  104. "position": 12
  105. }
  106. ,
  107. {
  108. "token": "大会",
  109. "start_offset": 9,
  110. "end_offset": 11,
  111. "type": "CN_WORD",
  112. "position": 13
  113. }
  114. ,
  115. {
  116. "token": "会堂",
  117. "start_offset": 10,
  118. "end_offset": 12,
  119. "type": "CN_WORD",
  120. "position": 14
  121. }
  122. ]
  123. }

测试ik_smart

{“text”:“中华人民共和国人民大会堂”,“analyzer”:“ik_smart” }

  1. {
  2. "tokens": [
  3. {
  4. "token": "中华人民共和国",
  5. "start_offset": 0,
  6. "end_offset": 7,
  7. "type": "CN_WORD",
  8. "position": 0
  9. }
  10. ,
  11. {
  12. "token": "人民大会堂",
  13. "start_offset": 7,
  14. "end_offset": 12,
  15. "type": "CN_WORD",
  16. "position": 1
  17. }
  18. ]
  19. }

二、最佳实践

两种分词器使用的最佳实践是:索引时用ik_max_word,在搜索时用ik_smart。

即:索引时最大化的将文章内容分词,搜索时更精确的搜索到想要的结果

举个例子:

我是个用户,输入“华为手机”,我此时的想法是想搜索出“华为手机”的商品,而不是华为其它的商品,也就是商品信息中必须只有华为手机这个词。

此时使用ik_smart和ik_max_word都会将华为手机拆分为华为和手机两个词,那些只包括“华为”这个词的信息也被搜索出来了,我的目标是搜索只包含华为手机这个词的信息,这没有满足我的目标。

怎么解决呢?

我们可以将华为手机添加到自定义词库,添加后两个分词器的效果为:

 

ik_max_word 的分词效果:

 

{

“tokens”: [

{

“token”: “华为手机”,

“start_offset”: 0,

“end_offset”: 4,

“type”: “CN_WORD”,

“position”: 0

}

,

{

“token”: “华为”,

“start_offset”: 0,

“end_offset”: 2,

“type”: “CN_WORD”,

“position”: 1

}

,

{

“token”: “手机”,

“start_offset”: 2,

“end_offset”: 4,

“type”: “CN_WORD”,

“position”: 2

}

]

}

 

ik_smart的分词效果:

 

{

“tokens”: [

{

“token”: “华为手机”,

“start_offset”: 0,

“end_offset”: 4,

“type”: “CN_WORD”,

“position”: 0

}

]

}

 

看到两个分词器的区别了吧,因为华为手机是一个词,所以ik_smart不再细粒度分了。

此时,我们可以在索引时使用 ik_max_word,在搜索时用ik_smart。

 

当输入 华为手机 关键字,只搜索到 包含华为手机的信息,符合用户要求。

如果我想将包含华为 这个词的信息也搜索出来怎么办呢?

那就输入 “华为 华为手机”(注意华为后边有个空格),那就会将包含华为、华为手机的信息都搜索出来。

 

根据上边举的例子,可以思考下,我想搜索手机壳怎么办?用户肯定只想搜索出手机壳的信息,不想搜索出来一推手机。

根据上边举的例子,可以思考下,如果搜索时用ik_max_word会有什么结果。

 

 

 

 

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/你好赵伟/article/detail/966669
推荐阅读
相关标签
  

闽ICP备14008679号