带有范围和存在过滤器的弹性搜索

时间:2016-06-27 09:46:09

标签: elasticsearch

我有一个ElasticSearch查询来获取设定范围内的所有产品。我想添加一个过滤器,只选择具有属性" products"的文档。我必须存在的测试始终存在错误。

/zipcodes_at/zipcode/_search

{
  "_source": [
    "products"
  ],
  "filter": {
    "geo_distance": {
      "distance": "100km",
      "location": {
        "lat": 48.232361,
        "lon": 16.324659
      }
    }
  },
  "sort": [
    {
      "_geo_distance": {
        "location": {
          "lat": 48.232361,
          "lon": 16.324695

        },
        "order": "asc",
        "unit": "km",
        "distance_type": "plane"
      }
    }
  ]
}

2 个答案:

答案 0 :(得分:3)

试试这个:

POST /zipcodes_at/zipcode/_search
{
  "_source": [
    "products"
  ],
  "query": {
    "bool": {
      "filter": [
        {
          "exists": {
            "field": "products"
          }
        },
        {
          "geo_distance": {
            "distance": "100km",
            "location": {
              "lat": 48.232361,
              "lon": 16.324659
            }
          }
        }
      ]
    }
  },
  "sort": [
    {
      "_geo_distance": {
        "location": {
          "lat": 48.232361,
          "lon": 16.324695
        },
        "order": "asc",
        "unit": "km",
        "distance_type": "plane"
      }
    }
  ]
}

答案 1 :(得分:1)

您应该使用bool过滤器,并将地理距离过滤器与现有过滤器结合使用。

{
    "_source": ["products"],
    "query": {
        "filtered": {
            "filter": {
                "bool": {
                    "must": [{
                        "exists": {
                            "field": "products"
                        }
                    }, {
                        "geo_distance_range": {
                            "from": 0,
                            "to": 100,
                            "distance_unit": "km",
                            "location": {
                                "lat": 40.73,
                                "lon": -74.1
                            }
                        }
                    }]
                }
            }
        }
    },
    "sort": [{
        "_geo_distance": {
            "location": {
                "lat": 48.232361,
                "lon": 16.324695

            },
            "order": "asc",
            "unit": "km",
            "distance_type": "plane"
        }
    }]
}