ElasticSearch 在满足条件的情况下排除过滤结果

ElasticSearch exclude result from being filtered where a condition is met

我想在满足条件的情况下排除一些结果,使其不被 geo_distance 过滤。

例如,我根据地理距离过滤我的结果,但我想包括所有状态异常且满足 match_phrase 查询的结果(即使它在 geo_distance 之外)

        GET /drive/_search
        {
          "query": {
            "bool": {
              "should": [
                {
                  "match_phrase": {
                    "keywords": "wheels"
                  }
                },
                {
                  "match_phrase": {
                    "name": "car sale"
                  }
                }
              ],
              "filter": [
                {
                  "term": {
                    "status": "normal"
                  }
                },
                {
                  "geo_distance": {
                    "distance": "0.09km",
                    "address.coordinate": {
                      "lat": -33.703082,
                      "lon": 18.981069
                    }
                  }
                }
              ]
            }
          }
        }

我一直在阅读文档和谷歌搜索,但我认为我可能走错了方向。

如果您能为我指明正确的方向,或者向我解释更好的解决方案,我将不胜感激。

来自doc :

should

The clause (query) should appear in the matching document. If the bool query is in a query context and has a must or filter clause then a document will match the bool query even if none of the should queries match. In this case these clauses are only used to influence the score. If the bool query is in a filter context or has neither must or filter then at least one of the should queries must match a document for it to match the bool query. This behavior may be explicitly controlled by setting the minimum_should_match parameter.

所以在你的情况下,地理条件在“should”因为它可以是可选的,其余的在“filter" 是强制性的:状态和 match_phrase

试试这个:

{
  "query": {
    "bool": {
      "filter": [
        {
          "term": {
            "status": "normal"
          }
        },
        {
          "match_phrase": {
            "name": "car sale"
          }
        },

        {
          "match_phrase": {
            "keywords": "wheels"
          }
        }
      ],
      "should": [
        {
          "geo_distance": {
            "distance": "0.09km",
            "address.coordinate": {
              "lat": -33.703082,
              "lon": 18.981069
            }
          }
        }
      ]
    }
  }
}