Geo queries
Elasticsearch支持两种类型的 geo 数据: geo_point 字段支持 lat / lon 对,geo_shape 字段支持点、线、圆、多边形等。
查询语句如下:
- geo_bounding_box query:
- 查询出geoshapes或geopoints与指定的多边形相交的文档。
- geo_distance query:
- 查询出geoshapes或geopoints与中心点在指定距离内的文档。
- geo_grid query:
- ......
- geo_polygon query:
- 查询出geoshapes或geopoints与指定多边形相交的文档。
- geo_shape query:
- 查询出geoshapes或geopoints与指定的geoshape相关的文档。关联关系可以是:相交、包含、毗邻。
Geo-distance query
对于文档中的 geo_point 和 geo_shape, 判断是否在给定的geopoint距离之内。
Example
创建一个索引,叫做 my_locations
PUT /my_locations
{
"mappings": {
"properties": {
"pin": {
"properties": {
"location": {
"type": "geo_point"
}
}
}
}
}
}
插入一条id是1的记录
PUT /my_locations/_doc/1
{
"pin": {
"location": {
"lat": 40.12,
"lon": -71.34
}
}
}
使用 geo_distance 过滤器来匹配与某个geo_point在特定距离内的其他geo_point
GET /my_locations/_search
{
"query": {
"bool": {
"must": {
"match_all": {}
},
"filter": {
"geo_distance": {
"distance": "200km",
"pin.location": {
"lat": 40,
"lon": -70
}
}
}
}
}
}
Accepted formats
geo_point可以有不同的表现方式,过滤器都能很好的识别
Lat lon as properties
lat 和 lon都是作为属性,例子如下
GET /my_locations/_search
{
"query": {
"bool": {
"must": {
"match_all": {}
},
"filter": {
"geo_distance": {
"distance": "12km",
"pin.location": {
"lat": 40,
"lon": -70
}
}
}
}
}
}
Lat lon as array
格式 [lon, lat],
GET /my_locations/_search
{
"query": {
"bool": {
"must": {
"match_all": {}
},
"filter": {
"geo_distance": {
"distance": "12km",
"pin.location": [ -70, 40 ]
}
}
}
}
}
Lat lon as WKT string
GET /my_locations/_search
{
"query": {
"bool": {
"must": {
"match_all": {}
},
"filter": {
"geo_distance": {
"distance": "12km",
"pin.location": "POINT (-70 40)"
}
}
}
}
}
Geohash
GET /my_locations/_search
{
"query": {
"bool": {
"must": {
"match_all": {}
},
"filter": {
"geo_distance": {
"distance": "12km",
"pin.location": "drm3btev3e86"
}
}
}
}
}