《Elastic Stack 实战手册》——三、产品能力——3.4.入门篇——3.4.2.Elasticsearch基础应用——3.4.2.3.Search通过Kibana(19) https://developer.aliyun.com/article/1231050
Geo-bounding box 查询
定义索引对象店铺信息:
PUT /my_shop_info { "mappings": { "properties": { "pin": { "properties": { "location": { "type": "geo_point" } } } } } } #添加2条测试数据 PUT /my_shop_info/_doc/1 { "pin": { "location": { "lat": 40.12, "lon": -71.34 } } } PUT /my_shop_info/_doc/2 { "pin": { "location": { "lat": 50.12, "lon": -61.34 } } } #查询指定范围内的数据 GET my_shop_info/_search { "query": { "bool": { "must": { "match_all": {} }, "filter": { "geo_bounding_box": { "pin.location": { "top_left": { "lat": 40.73, "lon": -74.1 }, "bottom_right": { "lat": 40.01, "lon": -71.12 } } } } } } } #返回 "hits" : { "total" : { "value" : 1, "relation" : "eq" }, "max_score" : 1.0, "hits" : [ { "_index" : "my_shop_info", "_type" : "_doc", "_id" : "1", "_score" : 1.0, "_source" : { "pin" : { "location" : { "lat" : 40.12, "lon" : -71.34 } } } } ] }
Geo-distance 查询
查询仅包含距某个地理点特定距离之内的匹配的坐标,如下所示,查询坐标:
#仍然以 my_shop_info 为例 GET /my_shop_info/_search { "query": { "bool": { "must": { "match_all": {} }, "filter": { "geo_distance": { "distance": "200km", "pin.location": { "lat": 40, "lon": -70 } } } } } }
创作人简介:
李增胜,Elasticsearch 认证工程师、PMP 项目管理认证,现就职于汇通达网络股份有限公司,任产业交易平台交易域技术经理,从事微服务架构、搜索架构方向开发与管理工作。技术关注:电商、产业互联网等领域。
博客:https://www.jianshu.com/u/59dceda66b57