首页 > 解决方案 > 模糊多重匹配未按预期工作 Elastic Search

问题描述

我正在尝试在您键入时添加模糊性来编写搜索查询。(弹性搜索 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"
            }

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

标签: elasticsearchelasticsearch-dsl

解决方案


您需要使用空白分析器,作为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"
        }
      }
    ]

推荐阅读