elasticsearch Geo Distance Query

Geo Distance Query

过滤器文档只包括在一个特定距离内存在于一个地理点上的命中。假设下列映射和索引文档:

PUT /my_locations
{
    "mappings": {
        "_doc": {
            "properties": {
                "pin": {
                    "properties": {
                        "location": {
                            "type": "geo_point"
                        }
                    }
                }
            }
        }
    }
}

PUT /my_locations/_doc/1
{
    "pin" : {
        "location" : {
            "lat" : 40.12,
            "lon" : -71.34
        }
    }
}

然后,可以使用 geo_distance 过滤器来执行下列简单查询:

GET /my_locations/_search
{
    "query": {
        "bool" : {
            "must" : {
                "match_all" : {}
            },
            "filter" : {
                "geo_distance" : {
                    "distance" : "200km",
                    "pin.location" : {
                        "lat" : 40,
                        "lon" : -70
                    }
                }
            }
        }
    }
}

可以接受的格式

同样地,geo_point 类型可以接受地理点的不同表示,过滤器也可以接受它:

Lat Lon 作为属性

GET /my_locations/_search
{
    "query": {
        "bool" : {
            "must" : {
                "match_all" : {}
            },
            "filter" : {
                "geo_distance" : {
                    "distance" : "12km",
                    "pin.location" : {
                        "lat" : 40,
                        "lon" : -70
                    }
                }
            }
        }
    }
}

Lat Lon 作为数组

写成[lon, lat]格式,是为了符合GeoJson

GET /my_locations/_search
{
    "query": {
        "bool" : {
            "must" : {
                "match_all" : {}
            },
            "filter" : {
                "geo_distance" : {
                    "distance" : "12km",
                    "pin.location" : [-70, 40]
                }
            }
        }
    }
}

Lat Lon 作为字符串

GET /my_locations/_search
{
    "query": {
        "bool" : {
            "must" : {
                "match_all" : {}
            },
            "filter" : {
                "geo_distance" : {
                    "distance" : "12km",
                    "pin.location" : "40,-70"
                }
            }
        }
    }
}

GeoHash

GET /my_locations/_search
{
    "query": {
        "bool" : {
            "must" : {
                "match_all" : {}
            },
            "filter" : {
                "geo_distance" : {
                    "distance" : "12km",
                    "pin.location" : "drm3btev3e86"
                }
            }
        }
    }
}

参数

如下是用filter中允许使用的参数:

distance:圆的半径以指定的位置为中心。进入这个圆的点被认为是匹配的。可以在不同的单元中指定距离。

distance_type:如何计算距离。可以是arc(默认),也可以是平面(更快,但在长距离和靠近两极的地方是不准确的)。

_name:可选名称字段来识别查询

validation_method:设置为忽略不正确的纬度或经度的geo点,设置为强制再尝试并推断正确的坐标(默认是严格的)

geo_point Type

过滤器要求在相关字段上设置geo_point类型。

Multi Location Per Document

geo_distance过滤器可以处理每个文档的多个位置/点。一旦一个位置/点与过滤器匹配,文档就会被包含在过滤器中。

Ignore Unmapped

当设置为true时,无知的未映射选项将忽略一个未映射的字段,并且不会匹配该查询的任何文档。这在查询可能有不同映射的多个索引时非常有用。当设置为false(默认值)时,如果字段没有映射,查询将抛出异常。

猜你喜欢

转载自www.cnblogs.com/a-du/p/9233419.html
GEO