首页 > 解决方案 > Elasticsearch - 匹配短语时避免基于单词和基于模糊的匹配

问题描述

我有一个查询,它搜索短语、单词级别和搜索单词的模糊性。有没有一种方法可以在匹配精确短语时限制单词和模糊匹配?开发的查询是:

PUT test/_doc/1
{
  "field1": "this is a test query - query",
  "field2": "best rest vest pest"
}

GET test/_search
{
  "query": {
    "bool": {
      "should": [
        {
          "multi_match": {
            "query": "this is a test query",
            "type": "phrase",
            "slop": "2",
            "fields": []
          }
        },
        {
          "multi_match": {
            "query": "this is a test query",
            "analyzer": "whitespace",
            "fields": []
          }
        },
        {
          "multi_match": {
            "query": "this is a test query",
            "analyzer": "whitespace",
            "fuzziness": "AUTO",
            "fields": []
          }
        }
      ],
      "minimum_should_match": 1
    }
  },
  "highlight": {
    "type": "unified",
    "fields": {
      "*": {}
    }
  }
}

获得的结果(仅突出显示)是:

“field1”:[“ 一个 测试 查询-查询”],“field2”:[“最好的 休息 背心 害虫”]

当“这是一个测试查询”短语匹配时,我不希望突出显示“最佳”、“休息”、“背心”和“害虫”

标签: elasticsearch

解决方案


将您的短语搜索放在 a 中must,并将其他两个条件保留在should. 查询规划器应该足够聪明,可以为您解决这个问题。


推荐阅读