java - Lucene 空间 : Sorting by Distance

标签 java scala sorting lucene

也许我在这里遗漏了一些东西,但我无法在 Lucene Spatial (10.3) 中进行空间排序,如 SpatialExample.java 中所述。 。这是我的代码(Scala - 但将 1:1 转换为 Java):

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

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

[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 ,但我已经检查过,这没有什么区别)。任何提示表示赞赏!

最佳答案

啊啊。回到回答我自己的问题。万一有人再次偶然发现这个问题:在我的例子中,原因是我的代码使用了两个不同的策略对象进行读取和写入 - 并且这些对象对 GeoHashPrefixTree 的 maxLevel 参数使用了不同的设置。千万不要那样做! ;-)

关于java - Lucene 空间 : Sorting by Distance,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28717528/

相关文章:

java - 有时会使用唯一索引插入重复数据

scala - 记录到 Scala/akka 中的文件 : ClassNotFoundException: akka. event.slf4j.Slf4jLoggingFilter

scala - 使用play-json库捕获Json异常

python - 最好使用的团队制作算法?

java - 过渡 fragment 调用退出过渡但动画没有发生

java - 使用 href 作为定位器很难单击 anchor 标记

scala - 使用 Akka IO 通过 Tcp 客户端发送非 ByteString

javascript - 除数字外,如何按字母顺序对对象数组进行排序?

java - 从文件读取的数据在冒泡排序方法中不起作用

java - 初级 Java : nextLine Dilema