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

问题描述 投票:1回答:1

我希望排除某些结果,使其在满足条件的情况下通过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
                    }
                  }
                }
              ]
            }
          }
        }

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

如果你可以指出我正确的方向,或向我解释一下,更好的解决办法是什么,我将非常感激。

elasticsearch elasticsearch-5
1个回答
1
投票

来自doc

应该

子句(查询)应出现在匹配的文档中。如果bool查询在查询上下文中并且具有must或filter子句,则文档将匹配bool查询,即使这些查询都不匹配。在这种情况下,这些条款仅用于影响分数。如果bool查询位于过滤器上下文中,或者既没有必须也没有过滤,那么至少有一个should查询必须与文档匹配才能匹配bool查询。可以通过设置minimum_should_match参数来显式控制此行为。

所以在你的情况下,geo条件是“should”,因为它可以是可选的,而“filter”中的其余部分是必需的:status和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
            }
          }
        }
      ]
    }
  }
}
© www.soinside.com 2019 - 2024. All rights reserved.