首页 > 解决方案 > Elasticsearch - 过滤 geo_distance 查询

问题描述

我有一个包含两个字段的映射类型:位置(geo_point)和类型(短)。

我想按地理位置列出我的地点并使用这种查询

{
"query": {
    "bool": {
        "filter": {
            "geo_distance": {
                "distance": "20km",
                "location": {
                    "lat": 48.856614,
                    "lon": 2.3522219
                }
            }
        }
    }
},
"aggs": {
    "types": {
        "terms": {
            "field": "type"
        }
    }
},
"post_filter": [],
"page": 1,
"size": 50,
"sort": [
    {
        "_geo_distance": {
            "location": {
                "lat": 48.856614,
                "lon": 2.3522219
            },
            "order": "asc",
            "unit": "km",
            "distance_type": "plane"
        }
    }
]
}

有没有办法只包含特殊类型的前 2 个位置(例如 type=2)?

标签: elasticsearch

解决方案


向过滤器添加另一个子句,例如

{
"query": {
"bool": {
    "filter": [{
        "geo_distance": {
            "distance": "20km",
            "location": {
                "lat": 48.856614,
                "lon": 2.3522219
            }
        }
    },
    { 
     "term": {"type":"2"}
    }]
}
},
"aggs": {
"types": {
    "terms": {
        "field": "type"
    }
 }
},
"post_filter": [],
"page": 1,
"size": 2,
"sort": [
{
    "_geo_distance": {
        "location": {
            "lat": 48.856614,
            "lon": 2.3522219
        },
        "order": "asc",
        "unit": "km",
        "distance_type": "plane"
    }
}
]
}

推荐阅读