elasticsearch distance_type说明(sloppy_arc、arc、plane) 作者:马育民 • 2022-03-30 10:55 • 阅读:10143 # distance_type 说明 两点之间的距离运算是 **非常耗时** 的,有时并 **不需要 非常精确** 的结果,可以指定计算距离的算法,我们可以根据需要从精度和性能之间做出权衡。 7.x官网有以下2个值: - `arc`:最慢但最准确的是arc计算,它将地面视为一个球体。精度仍然受到限制,因为地面并不是真正的球体。 - `plane`:plane将地面视为平坦世界,计算 **速度更快**,但 **准确性较低**。它在赤道处最精确,而在极点处变得不那么精确。 **注意:** **如果不写,不知道用哪个值,官网没说** ### 应用场景 - 可用于地理查询条件 - 地理位置排序 # 例子-查询条件 详见 [链接](https://www.malaoshi.top/show_1IX1rtSKRK6O.html "链接") ``` GET /meituan_v1/_search { "query": { "bool": { "must": { "match": { "name": "拉面" } }, "filter": { "geo_distance": { "distance": "200km", "distance_type": "arc", "location": { "lat": 77, "lon": 30 } } } } } } ``` # 例子-排序 搜索结果可以按照距离进行排序,详见 [链接](https://www.malaoshi.top/show_1IX1qOVnQaGX.html "链接") ``` GET /meituan_v1/_search { "query": { "bool": { "must": { "wildcard": { "name": "*面*" } }, "filter": { "geo_bounding_box": { "location": { "top_left": { "lat": 90, "lon": 31 }, "bottom_right": { "lat": 39, "lon": 118 } } } } } }, "sort": { "_geo_distance": { "location": { // 计算所有文档按照该指定位置的距离 "lat": 78, "lon": 36 }, "order": "asc", //从近到远 "unit": "km", // 将距离以 km 为单位写入每个返回结果的sort键中 "distance_type": "plane" // 按照平面计算 } } } ``` 原文出处:http://www.malaoshi.top/show_1IX32V25l1WQ.html