Elasticsearch 中的模糊匹配在两个不同的版本中给出不同的结果

Posted

技术标签:

【中文标题】Elasticsearch 中的模糊匹配在两个不同的版本中给出不同的结果【英文标题】:Fuzzy Matching in Elasticsearch gives different results in two different versions 【发布时间】:2022-01-12 06:29:22 【问题描述】:

我在 elasticsearch 中有一个带有标记器的字段分析器的映射:

"tokenizer": 
    "3gram_tokenizer": 
      "type": "nGram",
      "min_gram": "3",
      "max_gram": "3",
      "token_chars": [
        "letter",
        "digit"
      ]
    
  

现在我正在尝试在 Elasticsearch 中使用 query = "acinash" 搜索名称 = "avinash"

形成的查询是:


  "size": 5,
  "query": 
    "bool": 
      "must": [
        
          "multi_match": 
            "query": "acinash",
            "fields": [
              "name"
            ],
            "type": "best_fields",
            "operator": "AND",
            "slop": 0,
            "fuzziness": "1",
            "prefix_length": 0,
            "max_expansions": 50,
            "zero_terms_query": "NONE",
            "auto_generate_synonyms_phrase_query": false,
            "fuzzy_transpositions": false,
            "boost": 1.0
          
        
      ],
      "adjust_pure_negative": true,
      "boost": 1.0
    
  

但在 ES 版本 6.8 中,我得到了想要的结果(因为模糊性),即通过查询“acinash”得到“avinash”,但在 ES 版本 7.1 中我没有得到结果。

在 6.8 中尝试使用“avinaah”搜索“avinash”时也是如此,但在 7.1 中我没有得到结果

ES 所做的是将其转换为令牌 :[aci, cin, ina, nas, ash] 理想情况下应该与 ES 中带有令牌的令牌化倒排索引匹配: [avi, vin, ina, nas, ash]。

但是为什么在 7.1 中不匹配呢?

【问题讨论】:

【参考方案1】:

与ES版本无关。

将 max_expansions 更新为 50 以上。

max_expansions : Maximum number of variations created.

以 3 克字母和数字作为 token_chars,理想的 max_expansion 将是(26 个字母 + 10 位数字)* 3

【讨论】:

以上是关于Elasticsearch 中的模糊匹配在两个不同的版本中给出不同的结果的主要内容,如果未能解决你的问题,请参考以下文章

ElasticSearch如何使用 ElasticSearch 搜索单词的一部分 模糊搜索 正则匹配 前缀匹配

Elasticsearch系列---前缀搜索和模糊搜索

Elasticsearch系列---前缀搜索和模糊搜索

SQL两表模糊匹配查询

工作中的Elasticsearch-模糊检索

Elasticsearch 警惕使用 wildcard 检索!然后呢?