多应用+插件架构,代码干净,二开方便,首家独创一键云编译技术,文档视频完善,免费商用码云13.8K 广告
# Geo Distance Query(地理距离查询) **dsfs** 原文链接 : [https://www.elastic.co/guide/en/elasticsearch/reference/5.4/query-dsl-geo-distance-query.html](https://www.elastic.co/guide/en/elasticsearch/reference/5.4/query-dsl-geo-distance-query.html) 译文链接 : [Geo Distance Query(地理距离查询)](/pages/viewpage.action?pageId=10029641) 贡献者 : [@yangbin](http://www.apache.wiki/display/~yangbin), [ApacheCN](/display/~apachecn),[Apache中文网](/display/~apachechina) 过滤仅包含距离地理位置特定距离内的命中的文档。 假设以下映射和索引文档: ```  PUT /my_locations { "mappings": { "location": { "properties": { "pin": { "properties": { "location": { "type": "geo_point" } } } } } } } PUT /my_locations/location/1 { "pin" : { "location" : { "lat" : 40.12, "lon" : -71.34 } } } ``` 然后可以使用 **geo_distance **过滤器执行以下简单查询: ``` GET /my_locations/location/_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/location/_search { "query": { "bool" : { "must" : { "match_all" : {} }, "filter" : { "geo_distance" : { "distance" : "12km", "pin.location" : { "lat" : 40, "lon" : -70 } } } } } } ``` ### Lat Lon As Array (Lat Lon作为阵列) Format in** `[lon, lat]`**, note, the order of lon/lat here in order to conform with [GeoJSON](http://geojson.org/). ``` GET /my_locations/location/_search { "query": { "bool" : { "must" : { "match_all" : {} }, "filter" : { "geo_distance" : { "distance" : "12km", "pin.location" : [-70, 40] } } } } } ``` ### Lat Lon As String(Lat Lon作为字符串) Format in **`lat,lon`.   ** ``` GET /my_locations/location/_search { "query": { "bool" : { "must" : { "match_all" : {} }, "filter" : { "geo_distance" : { "distance" : "12km", "pin.location" : "40,-70" } } } } } ``` ### Geohash(地理散列) ``` GET /my_locations/location/_search { "query": { "bool" : { "must" : { "match_all" : {} }, "filter" : { "geo_distance" : { "distance" : "12km", "pin.location" : "drm3btev3e86" } } } } } ``` ### Options(选项) 以下是过滤器上允许的选项: | `**distance**(距离)` | 以指定位置为中心的圆的半径。 落入此圈子的点被认为是匹配的。 距离可以用各种单位指定。 看 [the section called “Distance Units](https://www.elastic.co/guide/en/elasticsearch/reference/5.4/common-options.html#distance-units "Distance Unitsedit")[edit](https://github.com/elastic/elasticsearch/edit/5.4/docs/reference/api-conventions.asciidoc "Edit this page on GitHub")”. | | **distance_type**(距离类型) | 如何计算距离。 可以是弧(默认)或平面(更快,但长距离不准确,靠近极点)。 | | **optimize_bbox**(优化bbox) | 是否在距离检查之前首先运行边框检查使用优化。 默认在内存检查中执行的内存。 也可以使用索引值来使用索引值检查(在这种情况下确保**geo_pointtype**索引**lat lon**),否则禁用边界框优化。[~~2.2~~] | | **_name**(名称) | 可选名称字段来标识查询。 | | `**ignore_malformed**(忽略格式错误)` | [~~<sub>5.0.0</sub>~~] 设置为 **true** 以接受无效纬度或经度的地理点(默认值为假)。 | | `**validation_method**(验证方法)` | 设置为 **IGNORE_MALFORMED** 以接受无效纬度或经度的地理点,设置为 **COERCE** 以额外尝试推断正确的坐标(默认为 **STRICT**)。 | ### geo_point Type(geo_point类型) 过滤器需要在相关字段上设置 **geo_point** 类型。 ### Multi Location Per Document(每个文档的多个位置) **geo_distance** 过滤器可以处理每个文档的多个位置/点。 一旦单个位置/点与过滤器匹配,文档将被包含在过滤器中。 ### Ignore Unmapped(忽略未映射) 当设置为 **true** 时,**ignore_unmapped **选项将忽略未映射字段,并且将不会匹配此查询的任何文档。 当查询可能具有不同映射的多个索引时,可能很有用。 当设置为 **false**(默认值)时,如果字段未映射,则查询将抛出异常。