【问题标题】:Fuzzy Multi Match not working as expected Elastic Search模糊多重匹配未按预期工作 Elastic Search
【发布时间】:2021-08-14 13:29:15
【问题描述】:

我正在尝试在您键入时添加模糊性来编写搜索查询。(弹性搜索 7.12)

{
"query": {
    "multi_match": {
        "query": "airl recl",
        "fields": [
            "tags",
            "display_text",
            "display_subtext"
        ],
        "type" : "most_fields",
        "operator": "and",
        "fuzziness": "AUTO:4,6",
        "prefix_length" :2 
    }
}

}

我插入了带有“airtel 充值”值的文档。我还对上述给定的 3 个字段以及空间分析器使用 edge n gram(1:50)。

  1. 如果我使用 airl 搜索 -> 效果很好,使用 airtel 关键字获取结果。
  2. 如果我用 recl 搜索 -> 它工作正常,得到 结果与充值关键字。
  3. 但是当我在查询中使用“airl recl”进行搜索时,没有得到任何结果。

空间分析器:

"words_with_spaces_analyzer" : {
          "filter" : [
            "lowercase",
            "asciifolding"
          ],
          "type" : "custom",
          "tokenizer" : "words_with_space"
        }
      },
      "tokenizer" : {
        "words_with_space" : {
          "pattern" : "([a-zA-Z0-9.-]+[\\s]*)",
          "type" : "pattern",
          "group" : "0"
        }
      }
    },

映射

   "display_text": {
                "type": "text",
                "fields": {
                    "raw": {
                        "type": "keyword"
                    }
                },
                "analyzer": "edge_nGram_analyzer",
                "search_analyzer": "words_with_spaces_analyzer"
            }

有人可以帮助我理解为什么上面给定的查询对于多令牌输入会以这种方式表现,而如果单独运行它们,两个令牌都会给出输出?

【问题讨论】:

    标签: elasticsearch elasticsearch-dsl


    【解决方案1】:

    您需要使用whitespace analyzer,作为search_analyzer。这会将搜索词 "airl recl" 分解为 airlrecl。然后,将对这些单独的令牌执行搜索。

    添加一个带有索引映射、搜索查询和搜索结果的工作示例

    索引映射:

     {
      "settings": {
        "analysis": {
          "analyzer": {
            "my_analyzer": {
              "tokenizer": "my_tokenizer"
            }
          },
          "tokenizer": {
            "my_tokenizer": {
              "type": "edge_ngram",
              "min_gram": 2,
              "max_gram": 50,
              "token_chars": [
                "letter",
                "digit"
              ]
            }
          }
        },
        "max_ngram_diff": 50
      },
      "mappings": {
        "properties": {
          "name": {
            "type": "text",
            "analyzer": "my_analyzer",
            "search_analyzer": "whitespace"
          }
        }
      }
    }
    

    索引数据:

    {
      "name": "airtel recharge"
    }
    

    搜索查询:

    {
      "query": {
        "multi_match": {
          "query": "airl recl",
          "fields": [
            "name"
          ],
          "type": "most_fields",
          "operator": "and",
          "fuzziness": "AUTO:4,6",
          "prefix_length": 2
        }
      }
    }
    

    搜索结果:

    "hits": [
          {
            "_index": "67702617",
            "_type": "_doc",
            "_id": "1",
            "_score": 0.22729424,
            "_source": {
              "name": "airtel recharge"
            }
          }
        ]
    

    【讨论】:

    • 非常感谢您的快速回复。我正在尝试您的分析仪,但请您检查一下我当前的空间分析仪是否有任何问题?
    • @BhagwatiMalav words_with_spaces_analyzer 分析器也可以使用,您只需将其用作search_analyzer,如上面的索引映射所示
    • 我也添加了映射供参考,你能检查一次吗? @ECoder
    • @BhagwatiMalav 您是否使用与上述问题中相同的索引映射?
    • 是的,我认为我的分析器结束索引/令牌开始重叠导致了问题,我现在正在检查你的分析器。
    猜你喜欢
    • 1970-01-01
    • 2018-03-02
    • 2022-11-30
    • 1970-01-01
    • 1970-01-01
    • 2023-04-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多