首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Lucene Spatial:按距离排序

Lucene Spatial:按距离排序
EN

Stack Overflow用户
提问于 2015-02-25 19:12:42
回答 1查看 781关注 0票数 3

也许我在这里遗漏了一些东西,但我不能让空间排序在Lucene spatial (10.3)中工作,正如SpatialExample.java中记录的那样。以下是我的代码(Scala -但将1:1转换为Java):

代码语言:javascript
复制
val searcher = placeSearcherManager.acquire()

val point = spatialCtx.makePoint(lat, lon)

val args =
  new SpatialArgs(SpatialOperation.Intersects,
    spatialCtx.makeCircle(lon, lat, 
    DistanceUtils.dist2Degrees(100, DistanceUtils.EARTH_MEAN_RADIUS_KM)))

val filter = spatialStrategy.makeFilter(args)

val valueSource = spatialStrategy.makeDistanceValueSource(point)

// Here's what's supposed to set up distance sorting
val distanceSort = new Sort(valueSource.getSortField(false)).rewrite(searcher)

try {  
  val topDocs = searcher.search(new MatchAllDocsQuery(), filter, limit, distanceSort) 
  val scoreDocs = topDocs.scoreDocs

  // Print the results
  scoreDocs.foreach(scoreDoc => {
    val doc = searcher.doc(scoreDoc.doc)
    val docPoint = spatialCtx.readShape(doc.get(spatialStrategy.getFieldName())).asInstanceOf[Point]
    val distance = spatialCtx.getDistCalc().distance(args.getShape.getCenter, docPoint)
    val distanceKM = DistanceUtils.degrees2Dist(distance, DistanceUtils.EARTH_EQUATORIAL_RADIUS_KM)
    Logger.info("distance: " + distanceKM)
  })
} finally {
  placeSearcherManager.release(searcher)
}

当我运行查询时,结果似乎完全不受排序的影响:

代码语言:javascript
复制
[info] application - distance: 406.01578203364323
[info] application - distance: 327.67269076509876
[info] application - distance: 218.94951150657565
[info] application - distance: 251.37927074183852
[info] application - distance: 140.6570939383426
[info] application - distance: 460.47502999630586
[info] application - distance: 462.37676932762116
[info] application - distance: 489.49001138999256
[info] application - distance: 392.0773262500455
[info] application - distance: 227.8864179949065

将排序顺序从升序更改为降序也不起作用。我看不出我正在做的事情和官方的例子有什么不同(除了SearcherManager的使用,但我已经检查过了,这没有什么区别)。任何提示都很感谢!

EN

回答 1

Stack Overflow用户

发布于 2015-02-26 17:55:26

啊。回到回答我自己的问题。以防有人再次遇到这种情况:在我的例子中,原因是我的代码使用了两个不同的策略对象进行读取和写入-而这两个对象对GeoHashPrefixTree的maxLevel参数使用了不同的设置。永远不要那样做!;-)

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/28717528

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档