首页 > 解决方案 > ElasticSearch 匹配多词关键字标记化字段

问题描述

我们有以下先决条件: 在 ES 上索引的具有tags字段的文档是字符串数组,例如: ['visa', 'credit card'] 我们要在tags字段上搜索这些文档。

要求:

  1. 如果文档有 标签:['visa', 'credit card'],我们只想在用户写了'visa'或'credit card'的情况下返回它,我们不希望接受'card','credit'和类似的部分,所以组合词必须完全匹配。
  2. 我们希望在单项和复合项的标签字段搜索中具有模糊性。
  3. 我们希望在标签字段上使用同义词。

所以我实现了:

"tags_analyzer": {
  "filter": [
    "lowercase",
    "asciifolding",
    "synonyms_expand",
  ],
  "char_filter": [
    "quotes",
    "html_strip",
    "ampersand",
    "returns"
  ],
  "type": "custom",
  "tokenizer": "keyword"
},

"query_analyzer": {
  "filter": [
    "lowercase",
    "my_asciifolding",
    "shingle"
  ],
  "char_filter": [
    "quotes",
    "html_strip",
    "ampersand",
    "returns"
  ],
  "type": "custom",
  "tokenizer": "standard"
},

"synonyms_expand": {
  "ignore_case": "true",
  "expand": "true",
  "type": "synonym",
  "synonyms": [
    "visa, credit card",
    "maestro, debit card"
  ],
  "tokenizer": "keyword"
},

"shingle": {
  "max_shingle_size": "3",
  "min_shingle_size": "2",
  "output_unigrams": "true",
  "type": "shingle",
  "filler_token": ""
}

在索引时间使用 tags_analyzer,在查询时间使用 query_analyzer。但是这个解决方案不适用于模糊复合术语。有谁知道为什么或有其他解决方案?

标签: elasticsearch

解决方案


推荐阅读