如何从版本4.0.0获取lucene geo(空间)搜索的距离?

时间:2012-11-05 04:08:24

标签: search lucene geospatial

我正在尝试使用最新版本的Lucene(4.0.0)进行地理搜索功能,要求很简单:获取圆内的点(中心和半径作为查询条件传入)。我找不到将每个结果的距离输出到中心的API,我必须计算出每个结果的纬度和经度后的距离。谁有人可以帮忙?代码如下:

SpatialContext sc = SpatialContext.GEO;
SpatialArgs args = new SpatialArgs(SpatialOperation.Intersects,
              sc.makeCircle(lo, la, DistanceUtils.dist2Degrees(dist, DistanceUtils.EARTH_MEAN_RADIUS_KM)));
Filter geo_filter = strategy.makeFilter(args);
try {
    Sort chainedSort = new Sort(sfArray).rewrite(searcher);
    TopDocs docs = searcher.search(new MatchAllDocsQuery(), geo_filter, 10000, chainedSort);
    logger.debug("search finished, num: " + docs.totalHits);
    for (ScoreDoc scoreDoc : docs.scoreDocs){
        Document doc = searcher.doc(scoreDoc.doc);
        double la1 = Double.parseDouble(doc.get("la"));
        double lo1 = Double.parseDouble(doc.get("lo"));
        double distance = getDistance(la1, lo1, la, lo); // have to calc distance by myself here, not cool
    }
} catch (IOException e) {
    logger.error("fail to get the search result!", e);
}

任何熟悉Lucene 4.0.0地理(空间)搜索的人都很容易与Lucene 3.X保持距离?

1 个答案:

答案 0 :(得分:5)

你有lat&来自田野;现在您需要计算距离查询圆的中心点的距离。在您的代码中,这看起来像:

double distDEG = sc.getDistCalc().distance(args.getShape().getCenter(), lo1, la1);
double distKM = DistanceUtils.degrees2Dist(distDEG, DistanceUtils.EARTH_MEAN_RADIUS_KM);

不错; EHH? (p.s.我写了很多Lucene 4空间)