精确整词匹配,用于等于运算

ybzsozfc  于 2021-06-13  发布在  ElasticSearch
关注(0)|答案(1)|浏览(368)

我正在寻找一种方法,通过匹配从elasticsearch准确的整个词的结果。这是用于ui中的“eq”(“=”)操作。

  1. {
  2. "_index": "docs",
  3. "_type": "_doc",
  4. "_id": "1",
  5. "_score": 1.0,
  6. "_source": {
  7. "DocId": 1,
  8. "DocDate": "2020-07-24T10:16:44.0000000Z",
  9. "Conversation": "I just need to know how frequently I should remind you"
  10. }
  11. },
  12. {
  13. "_index": "docs",
  14. "_type": "_doc",
  15. "_id": "2",
  16. "_score": 1.0,
  17. "_source": {
  18. "DocId": 2,
  19. "DocDate": "2020-07-25T10:16:45.0000000Z",
  20. "Conversation": "Building a work culture in your firm"
  21. }
  22. }

在这里,当用“我只需要知道我应该多久提醒你一次”来查询会话时,只有es应该返回docid 1数据。即使查询像“我只需要知道我应该多久提醒一次”,那么它应该返回空的。
我尝试了这些es查询,但没有找到答案。

  1. GET docs/_search
  2. {
  3. "query": {
  4. "bool": {
  5. "must": [
  6. {"match_phrase": {
  7. "Conversation": "just need to know"
  8. }}
  9. ]
  10. }
  11. }
  12. }
  13. GET docs/_search
  14. {
  15. "query": {
  16. "query_string": {
  17. "default_field": "Conversation",
  18. "query": "\"just need to know\""
  19. }
  20. }
  21. }
  22. GET docs/_search
  23. {
  24. "query": {
  25. "bool": {
  26. "must": [
  27. {
  28. "match": {
  29. "Conversation": {"query": "just need to know",
  30. "operator": "and"
  31. }
  32. }
  33. }
  34. ]
  35. }
  36. }
  37. }
1szpjjfi

1szpjjfi1#

你需要加上 .keywordConversation 现场。这将使用关键字分析器而不是标准分析器(注意在 Conversation 字段)。
使用标准分析仪时

  1. GET /_analyze
  2. {
  3. "analyzer" : "standard",
  4. "text" : "I just need to know how frequently I should remind you"
  5. }

生成以下令牌:

  1. {
  2. "tokens": [
  3. {
  4. "token": "i",
  5. "start_offset": 0,
  6. "end_offset": 1,
  7. "type": "<ALPHANUM>",
  8. "position": 0
  9. },
  10. {
  11. "token": "just",
  12. "start_offset": 2,
  13. "end_offset": 6,
  14. "type": "<ALPHANUM>",
  15. "position": 1
  16. },
  17. {
  18. "token": "need",
  19. "start_offset": 7,
  20. "end_offset": 11,
  21. "type": "<ALPHANUM>",
  22. "position": 2
  23. },
  24. {
  25. "token": "to",
  26. "start_offset": 12,
  27. "end_offset": 14,
  28. "type": "<ALPHANUM>",
  29. "position": 3
  30. },
  31. {
  32. "token": "know",
  33. "start_offset": 15,
  34. "end_offset": 19,
  35. "type": "<ALPHANUM>",
  36. "position": 4
  37. },
  38. {
  39. "token": "how",
  40. "start_offset": 20,
  41. "end_offset": 23,
  42. "type": "<ALPHANUM>",
  43. "position": 5
  44. },
  45. {
  46. "token": "frequently",
  47. "start_offset": 24,
  48. "end_offset": 34,
  49. "type": "<ALPHANUM>",
  50. "position": 6
  51. },
  52. {
  53. "token": "i",
  54. "start_offset": 35,
  55. "end_offset": 36,
  56. "type": "<ALPHANUM>",
  57. "position": 7
  58. },
  59. {
  60. "token": "should",
  61. "start_offset": 37,
  62. "end_offset": 43,
  63. "type": "<ALPHANUM>",
  64. "position": 8
  65. },
  66. {
  67. "token": "remind",
  68. "start_offset": 44,
  69. "end_offset": 50,
  70. "type": "<ALPHANUM>",
  71. "position": 9
  72. },
  73. {
  74. "token": "you",
  75. "start_offset": 51,
  76. "end_offset": 54,
  77. "type": "<ALPHANUM>",
  78. "position": 10
  79. }
  80. ]
  81. }

而关键字分析器将整个输入字符串作为单个标记返回。如果尚未定义任何显式Map,则修改的搜索查询将是:

  1. {
  2. "query": {
  3. "match": {
  4. "Conversation.keyword": "I just need to know how frequently I should remind you"
  5. }
  6. }
  7. }

您甚至可以通过以下方式更改索引Map:

  1. {
  2. "mappings": {
  3. "properties": {
  4. "Conversation": {
  5. "type": "keyword"
  6. }
  7. }
  8. }
  9. }
展开查看全部

相关问题