【问题标题】:Elasticsearch displaying incorrect distance [duplicate]Elasticsearch显示不正确的距离[重复]
【发布时间】:2014-02-25 08:57:58
【问题描述】:

我有以下弹性搜索查询:

{
"from": 0,
"size": 10,
"fields": ["returnresults"],
"script_fields": {
    "distance": {
        "params": {
            "lat": -41.166670,
            "lon": 146.350000
        },
        "script": "doc[\u0027location\u0027].distanceInKm(lat,lon)"
    }
},
"track_scores": true,
"sort": [{
    "_geo_distance": {
        "searchindex.location": [146.350000,
        -41.166670],
        "order": "asc",
        "unit": "km"
    },
    "_score": {
        "order": "desc"
    }
}],
"query": {
    "query_string": {
        "query": "7383492",
        "fields": ["total"]
    }
},
"filter": {
    "and": [{
        "query": {
            "range": {
                "expiry": {
                    "gt": 1393285386
                }
            }
        }
    },
]
}
}

当我运行查询时,它找到了一些匹配的结果,但是它报告的距离是错误的。

海外地理编码 coord:37.445796966553,-122.16209411621

它返回的结果是距离 31145 公里。这是不可能的。

我在查询中做错了什么,或者这是弹性搜索本身的问题。我似乎对短距离没有问题,但这可能是它关闭的百分比只是很小的,因为它是短距离。

我目前使用的是 Elasticsearch 0.90.2

任何帮助将不胜感激。

【问题讨论】:

    标签: elasticsearch


    【解决方案1】:

    我很好奇在 Wrong distance returned by ElasticSearch distanceInKm 中回答的问题是否导致了这个问题。

    我对这两种算法的快速而肮脏的测试得出了平面距离 = 19352 英里(31144 公里)和弧距 = 7915.7 英里(12739 公里),因此它似乎很好地解释了所述差异。无论如何我觉得它很有趣,所以如果其他人感兴趣:

    public static void main(String []args){
        double targetLongitude = 146.350000;
        double sourceLongitude = -122.16209411621;
        double targetLatitude = -41.166670;
        double sourceLatitude = 37.445796966553;
        double DISTANCE_PER_DEGREE = 69.16944444;
        double EARTH_RADIUS = 3958.76;
    
        //Plane distance
        double px = targetLongitude - sourceLongitude;
        double py = targetLatitude - sourceLatitude;
        double distanceMiles = Math.sqrt(px * px + py * py) * DISTANCE_PER_DEGREE;
    
        System.out.println(distanceMiles);
    
    
        //Arc distance
        double longitudeDifference = targetLongitude - sourceLongitude;
        double a = Math.toRadians(90D - sourceLatitude);
        double c = Math.toRadians(90D - targetLatitude);
        double factor = (Math.cos(a) * Math.cos(c)) + (Math.sin(a) * Math.sin(c) * Math.cos(Math.toRadians(longitudeDifference)));
    
        if (factor < -1D) {
            System.out.println( Math.PI * EARTH_RADIUS);
        } else if (factor >= 1D) {
            System.out.println(  0);
        } else {
            System.out.println(  Math.acos(factor) * EARTH_RADIUS);
        }
    }
    

    【讨论】:

    • 是的,这就是问题所在,谢谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-07-27
    • 2011-04-11
    • 2022-11-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-10-18
    相关资源
    最近更新 更多