地理距离查询
编辑地理距离查询编辑
匹配给定地理点一定距离内的 geo_point
和 geo_shape
值。
示例编辑
假设以下文档已索引
response = client.indices.create( index: 'my_locations', body: { mappings: { properties: { pin: { properties: { location: { type: 'geo_point' } } } } } } ) puts response response = client.index( index: 'my_locations', id: 1, body: { pin: { location: { lat: 40.12, lon: -71.34 } } } ) puts response response = client.indices.create( index: 'my_geoshapes', body: { mappings: { properties: { pin: { properties: { location: { type: 'geo_shape' } } } } } } ) puts response response = client.index( index: 'my_geoshapes', id: 1, body: { pin: { location: { type: 'polygon', coordinates: [ [ [ 13, 51.5 ], [ 15, 51.5 ], [ 15, 54 ], [ 13, 54 ], [ 13, 51.5 ] ] ] } } } ) puts response
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
过滤器匹配指定地理点一定距离内的 geo_point
值
response = client.search( index: 'my_locations', body: { query: { bool: { must: { match_all: {} }, filter: { geo_distance: { distance: '200km', 'pin.location' => { lat: 40, lon: -70 } } } } } } ) puts response
GET /my_locations/_search { "query": { "bool": { "must": { "match_all": {} }, "filter": { "geo_distance": { "distance": "200km", "pin.location": { "lat": 40, "lon": -70 } } } } } }
使用相同的过滤器匹配给定距离内的 geo_shape
值
response = client.search( index: 'my_geoshapes', body: { query: { bool: { must: { match_all: {} }, filter: { geo_distance: { distance: '200km', 'pin.location' => { lat: 40, lon: -70 } } } } } } ) puts response
GET my_geoshapes/_search { "query": { "bool": { "must": { "match_all": {} }, "filter": { "geo_distance": { "distance": "200km", "pin.location": { "lat": 40, "lon": -70 } } } } } }
要匹配 geo_point
和 geo_shape
值,请搜索两个索引
response = client.search( index: 'my_locations,my_geoshapes', body: { query: { bool: { must: { match_all: {} }, filter: { geo_distance: { distance: '200km', 'pin.location' => { lat: 40, lon: -70 } } } } } } ) puts response
GET my_locations,my_geoshapes/_search { "query": { "bool": { "must": { "match_all": {} }, "filter": { "geo_distance": { "distance": "200km", "pin.location": { "lat": 40, "lon": -70 } } } } } }
接受的格式编辑
与 geo_point
类型可以接受地理点的不同表示方式类似,过滤器也可以接受它
纬度经度作为属性编辑
response = client.search( index: 'my_locations', body: { query: { bool: { must: { match_all: {} }, filter: { geo_distance: { distance: '12km', 'pin.location' => { lat: 40, lon: -70 } } } } } } ) puts response
GET /my_locations/_search { "query": { "bool": { "must": { "match_all": {} }, "filter": { "geo_distance": { "distance": "12km", "pin.location": { "lat": 40, "lon": -70 } } } } } }
纬度经度作为数组编辑
格式为 [lon, lat]
,请注意,这里经度/纬度的顺序是为了符合 GeoJSON。
response = client.search( index: 'my_locations', body: { query: { bool: { must: { match_all: {} }, filter: { geo_distance: { distance: '12km', 'pin.location' => [ -70, 40 ] } } } } } ) puts response
GET /my_locations/_search { "query": { "bool": { "must": { "match_all": {} }, "filter": { "geo_distance": { "distance": "12km", "pin.location": [ -70, 40 ] } } } } }
纬度经度作为 WKT 字符串编辑
格式为 Well-Known Text。
response = client.search( index: 'my_locations', body: { query: { bool: { must: { match_all: {} }, filter: { geo_distance: { distance: '12km', 'pin.location' => 'POINT (-70 40)' } } } } } ) puts response
GET /my_locations/_search { "query": { "bool": { "must": { "match_all": {} }, "filter": { "geo_distance": { "distance": "12km", "pin.location": "POINT (-70 40)" } } } } }
Geohash编辑
response = client.search( index: 'my_locations', body: { query: { bool: { must: { match_all: {} }, filter: { geo_distance: { distance: '12km', 'pin.location' => 'drm3btev3e86' } } } } } ) puts response
GET /my_locations/_search { "query": { "bool": { "must": { "match_all": {} }, "filter": { "geo_distance": { "distance": "12km", "pin.location": "drm3btev3e86" } } } } }
选项编辑
以下是过滤器允许的选项
|
以指定位置为中心的圆的半径。落在该圆内的点被视为匹配。 |
|
如何计算距离。可以是 |
|
可选名称字段,用于标识查询 |
|
设置为 |
每个文档多个位置编辑
geo_distance
过滤器可以与每个文档中的多个位置/点一起使用。只要单个位置/点匹配过滤器,文档就会包含在过滤器中。
忽略未映射编辑
当设置为 true
时,ignore_unmapped
选项将忽略未映射的字段,并且不会为该查询匹配任何文档。这在查询可能具有不同映射的多个索引时很有用。当设置为 false
(默认值)时,如果字段未映射,查询将抛出异常。