在距某个geopoint的给定距离内匹配“geo_point”和“geo_shape”值。
例如:
假设以下文档已编制索引:
PUT /my_locations
{
"mappings": {
"properties": {
"pin": {
"properties": {
"location": {
"type": "geo_point"
}
}
}
}
}
}
PUT /my_locations/_doc/1
{
"pin": {
"location": {
"lat": 40.12,
"lon": -71.34
}
}
}
PUT /my_geoshapes
{
"mappings": {
"properties": {
"pin": {
"properties": {
"location": {
"type": "geo_shape"
}
}
}
}
}
}
PUT /my_geoshapes/_doc/1
{
"pin": {
"location": {
"type" : "polygon",
"coordinates" : [[[13.0 ,51.5], [15.0, 51.5], [15.0, 54.0], [13.0, 54.0], [13.0 ,51.5]]]
}
}
}
使用geo_distance过滤器匹配另一个geopoint指定距离内的geo_point值:
GET /my_locations/_search
{
"query": {
"bool": {
"must": {
"match_all": {}
},
"filter": {
"geo_distance": {
"distance": "200km",
"pin.location": {
"lat": 40,
"lon": -70
}
}
}
}
}
}
使用相同的过滤器匹配给定距离内的geo_shape:
GET my_geoshapes/_search
{
"query": {
"bool": {
"must": {
"match_all": {}
},
"filter": {
"geo_distance": {
"distance": "200km",
"pin.location": {
"lat": 40,
"lon": -70
}
}
}
}
}
}
要同时匹配geo_point和geo_shape值,请搜索两个索引:
GET my_locations,my_geoshapes/_search
{
"query": {
"bool": {
"must": {
"match_all": {}
},
"filter": {
"geo_distance": {
"distance": "200km",
"pin.location": {
"lat": 40,
"lon": -70
}
}
}
}
}
}
Accepted Formats
与geo_point类型大致相同,可以接受geo point的不同表示方式,过滤器也可以接受它:
Lat Lon As Properties
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]格式,注意,此处lon/lat的顺序,以符合GeoJSON。
GET /my_locations/_search
{
"query": {
"bool": {
"must": {
"match_all": {}
},
"filter": {
"geo_distance": {
"distance": "12km",
"pin.location": [ -70, 40 ]
}
}
}
}
}
Lat Lon As String
格式为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"
}
}
}
}
}