结合过滤和布尔查询

时间:2014-10-14 16:10:43

标签: elasticsearch elastica

我正在尝试使用以下查询获取到当前位置的最近位置,并且运行得非常好。

{
  "from": 0,
  "size": 3,
  "query": {
    "filtered": {
      "query": {
        "matchAll": {}
      },
      "filter": {
        "exists": {
          "field": "pin"
        }
      }
    }
  },
  "sort": [
    {
      "_geo_distance": {
        "pin": {
          "lat": 52.51515,
          "lon": 13.38019
        },
        "order": "asc",
        "unit": "km",
        "distance_type":"arc"
      }
    }
  ]
}

此外,我想将上面的查询与bool查询结合起来,因为我不想在我的结果列表中拥有当前位置(这是最接近的位置,因为_geo_distance的数据是当前位置数据)。我知道我可以简单地设置为1,大小设置为4,但我想知道如何将上面的查询与bool one结合起来。

    "bool": {
      "must_not": [
        {
          "query": {
            "matchAll": {
              "slug": "adlon-hotel"
            }
          }
        }
      ]
    }

我刚刚阅读了文档,但我没有复制如何结合两个查询。

谢谢。

1 个答案:

答案 0 :(得分:1)

我刚才自己回答了

{
  "from": 0,
  "size": 3,
  "query": {
    "filtered": {
      "query": {
        "bool": {
          "must_not": [
            {
              "match": {
                "slug": "adlon-hotel"
              }
            }
          ]
        }
      },
      "filter": {
        "exists": {
          "field": "pin"
        }
      }
    }
  },
  "sort": [
    {
      "_geo_distance": {
        "pin": {
          "lat": 52.51515,
          "lon": 13.38019
        },
        "order": "asc",
        "unit": "km",
        "distance_type": "arc"
      }
    }
  ]
}

并确保' slug'没有分析。

相关问题