地理距离查询 #
地理距离查询返回包含地理点的文档,这些地理点位于提供的地理点指定距离内。如果一个文档包含多个地理点,只要至少有一个地理点与查询匹配,该文档就符合查询条件。
搜索的文档字段必须映射为 geo_point 。
参考样例 #
创建一个映射,将 point
字段映射为 geo_point
:
PUT testindex1
{
"mappings": {
"properties": {
"point": {
"type": "geo_point"
}
}
}
}
索引一个地理点,指定其纬度和经度:
PUT testindex1/_doc/1
{
"point": {
"lat": 74.00,
"lon": 40.71
}
}
搜索距离指定的 point
内包含指定 distance
对象的文档:
GET /testindex1/_search
{
"query": {
"bool": {
"must": {
"match_all": {}
},
"filter": {
"geo_distance": {
"distance": "50mi",
"point": {
"lat": 73.5,
"lon": 40.5
}
}
}
}
}
}
返回内容包含匹配的文档:
{
"took": 5,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"skipped": 0,
"failed": 0
},
"hits": {
"total": {
"value": 1,
"relation": "eq"
},
"max_score": 1,
"hits": [
{
"_index": "testindex1",
"_id": "1",
"_score": 1,
"_source": {
"point": {
"lat": 74,
"lon": 40.71
}
}
}
]
}
}
参数说明 #
Geodistance 查询接受以下参数。
参数 | 数据类型 | 描述 |
---|---|---|
_name | String | 过滤器名称。可选。 |
distance | String | 匹配点的距离范围。此距离是以指定点为中心的圆的半径。有关支持的距离单位,请参阅距离单位。必需。 |
distance_type | String | 指定如何计算距离。有效值是 arc 或 plane (对于长距离或接近极点的点,速度快但不够准确)。可选。默认是 arc 。 |
validation_method | String | 验证方法。有效值是 IGNORE_MALFORMED (接受坐标无效的地理点)、 COERCE (尝试将坐标转换为有效值)和 STRICT (当坐标无效时返回错误)。可选。默认是 STRICT 。 |
ignore_unmapped | Boolean | 指定是否忽略未映射的字段。如果设置为 true ,则查询不返回包含未映射字段的任何文档。如果设置为 false ,则在字段未映射时抛出异常。可选。默认为 false 。 |