【问题标题】:similar sphinxsearch geodistance sorting in elasticsearchelasticsearch中类似的sphinxsearch地理距离排序
【发布时间】:2017-12-31 10:45:18
【问题描述】:

我已经对名称进行了聚合,但地理距离排序无法正常工作。 我已经实现了聚合和距离计算。但我不知道如何计算桶距离值。 请建议我如何实现?

映射:-

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"}

弹性搜索查询:

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
                          }
              }
    }
}
}
}
}
}
}

上面的查询给出了结果,但没有按距离排序。

【问题讨论】:

    标签: php sorting elasticsearch sphinx


    【解决方案1】:

    我可能看错了你,但你试图从另一个点获取 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 行,因为我只想要最接近的匹配,但是您可能想要在其他情况下更改此设置!

    【讨论】:

    • 我的查询给出了距离,但我只卡住了排序。但是我们手动实现了这一点,那时我们在排序后获取所有数据。我不知道在弹性搜索中该怎么做。
    • 啊,好吧,很明显这不是 MySQL,所以我想我真的帮不上什么忙了!
    • 对不起,我不能再使用了:-P
    猜你喜欢
    • 2017-04-08
    • 2020-01-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-07-27
    • 2018-10-24
    • 2017-07-29
    相关资源
    最近更新 更多