elk笔记13--Queries-geo queries
1 geo查询简介
geo queries(地理位置查询) 支持2类geo数据,分别为geo_point 和geo_shape 2中类型;其中, gep_points 支持 lat/lon 对, geo_shape 支持点、线、圆、多边形和多个多边形等;
共对应四种查询,分别为 geo_bounding_box、geo_distance、geo_polygon 、geo_shape 。本文部分数据直接使用 kibana_sample_data_ecommerce 中的数据,具体案例如下:
Elasticsearch supports two types of geo data: geo_point fields which support lat/lon pairs, and geo_shape fields, which support points, lines, circles, polygons, multi-polygons, etc.
2 geo 查询案例
-
使用geo_bounding_box 和top_left、bottom_right 搜索左上和右下区域内的数据
GET kibana_sample_data_ecommerce/_search { "query": { "geo_bounding_box" : { "geoip.location" : { "top_left" : { "lat" : 40.73, "lon" : -74.1 }, "bottom_right" : { "lat" : 40.01, "lon" : -71.12 } } } } } 也可以使用这种方式搜索 "geoip.location" : { "top_left" : [-74.1, 40.73], "bottom_right" : [-71.12, 40.01] } 或者下面这种方式 "geoip.location" : { "top_left" : "40.73, -74.1", "bottom_right" : "40.01, -71.12" }
结果:
-
使用geo_distance 搜索特定地点指定距离内的数据
GET kibana_sample_data_ecommerce/_search { "query": { "geo_distance" : { "distance": "200km", "geoip.location" : { "lat" : 40.6, "lon" : -73 } } } }
-
使用 geo_polygon 搜索指定多边形区域内的数据
GET kibana_sample_data_ecommerce/_search { "query": { "geo_polygon" : { "geoip.location" : { "points" : [ {"lat" : 30, "lon" : -70}, {"lat" : 60, "lon" : -20}, {"lat" : 45, "lon" : -90} ] } } } } 也可以使用 "points" : [ [30,-70], [60,-20], [45,-90] ] 或者使用 "points" : [ "30,-70", "60,-20", "45,-90" ]
-
通过 geo_shape 来搜索某个范围内的数据
该方法与前3个不同之处在于:需要设置mapping类型为geo_shape, 而前三个mapping类型为geo_pointPUT /example { "mappings": { "properties": { "location": { "type": "geo_shape" } } } } POST /example/_doc?refresh { "name": "Wind & Wetter, Berlin, Germany", "location": { "type": "point", "coordinates": [13.400544, 52.530286] } } GET example/_search { "query": { "geo_shape": { "location": { "shape": { "type": "envelope", "coordinates" : [[13.0, 53.0], [14.0, 52.0]] }, "relation": "within" } } } }
-
统计不同范围段内的数据
通过aggs 和 geo_distance 来统计不同范围段的数据GET kibana_sample_data_ecommerce/_search { "size": 0, "aggs": { "agg_by_distance": { "geo_distance" : { "field": "geoip.location", "origin": { "lat" : 40.6, "lon" : -73 }, "unit": "mi", "distance_type": "plane", "ranges": [ {"to":100}, {"from": 100,"to":300}, {"from": 300} ] } } } }
结果:
此处单位为miles, 其它单位见 distance-units
3 说明
- 参考文档
- 测试环境
- 本文测试案例对应的 es 版本为7.2.1
- 测试数据为kibana自带数据,Home->Add data->Sample data->Sample eCommerce orders