gpt4 book ai didi

php - Elasticsearch 中类似的sphinxsearch地理距离排序

转载 作者:行者123 更新时间:2023-12-03 00:05:40 25 4
gpt4 key购买 nike

我已经完成了名称的汇总,但是地理距离排序无法正常工作。
我已经实现了聚合和距离计算。但我不知道如何分配铲斗距离值。
请建议我如何实现?

对应:

PUT /museums
{
"mappings": {
"doc": {
"properties": {
"location": {
"type": "geo_point"
}
}
}
}
}

数据值:
POST /museums/doc/_bulk?refresh
{"index":{"_id":1}}
{"location": "52.374081,4.912350", "name": "NEMO Science Museum"}
{"index":{"_id":2}}
{"location": "52.369219,4.901618", "name": "Museum Het Rembrandthuis"}
{"index":{"_id":3}}
{"location": "52.371667,4.914722", "name": "Nederlands Scheepvaartmuseum"}
{"index":{"_id":4}}
{"location": "51.222900,4.405200", "name": "Letterenhuis"}
{"index":{"_id":5}}
{"location": "48.861111,2.336389", "name": "Musée du Louvre"}
{"index":{"_id":6}}
{"location": "48.860000,2.327000", "name": "Musée d'Orsay"}
{"index":{"_id":7}}
{"location": "52.374081,4.912350", "name": "NEMO Science Museum"}
{"index":{"_id":8}}
{"location": "48.861111,2.336389", "name": "Musée du Louvre"}

flex 搜索查询:
POST /museums/_search?size=0
{
"query": {

},
"sort": {
"_geo_distance": {
"location": {
"lat": 52.3760,
"lon": 4.894
},
"order": "asc",
"unit": "km",
"distance_type": "arc"
}
},
"aggregations": {
"by_id": {
"terms": {
"field": "name.keyword",
"order": {
"_count": "asc"
},
"size": 20
},
"aggregations":{
"top":{
"top_hits":
{
"sort":{
"_geo_distance":{
"location":{"lat":19.143172,"lon":72.824966
}
}
}
}
}
}
}
}
}

以上查询给出结果,但不按距离排序。

最佳答案

我可能对您有误读,但您正试图从另一点获取X公里/英里以内的位置?

如果是这样,则存在一个称为Haversine公式的方程式,该方程式使用球面三角法计算一定距离内的面积!看起来像这样:

R = earth’s radius (mean radius = 6,371km)
Δlat = lat2− lat1
Δlong = long2− long1
a = sin²(Δlat/2) + cos(lat1).cos(lat2).sin²(Δlong/2)
c = 2.atan2(√a, √(1−a))
d = R.c
Angles need to be in radians to pass to Trigonometric functions

这对我来说是非常数学的。但是,在MySQL查询中,它看起来像这样:
SELECT *, 
( 3959 * acos( cos( radians(55.864237) ) * cos( radians( latitude ) )
* cos( radians( longitude ) - radians(-4.251806) ) + sin( radians(55.864237) )
* sin( radians( latitude ) ) ) ) AS distance
FROM postcodes HAVING distance < 20
ORDER BY distance LIMIT 1;

我在这里检查20英里以内的任何区域,但是您可以根据需要将其设置为短或长。查询开始处的3959数字是用于英里的数字,如果您使用公里,则应将此数字更改为6371。我将其限制为1行,因为我只想要最接近的匹配项,但是您可能想要在其他情况下更改此设置!

关于php - Elasticsearch 中类似的sphinxsearch地理距离排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45304453/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com