4

Maybe I'm missing something here, but I can't get spatial sorting to work in Lucene Spatial (10.3), as documented in SpatialExample.java. Here's my code (Scala - but translates 1:1 into 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)
}

When I run the query, results don't seem to be affected by the sorting at all:

[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

Changing the sort order from ascending to descending has no effect either. I can't see any difference between what I'm doing and the offical example (except the use of the SearcherManager, but I've checked and that doesn't make a difference). Any hints appreciated!

1
  • Isn't the val point = spatialCtx.makePoint(lat, lon) call backwards, it should be lon, lat since it's expecting x/y ?
    – Shazwazza
    Sep 21, 2020 at 14:03

2 Answers 2

0

Argh. Back to answering my own questions. In case someone stumbles across this again: the reason in my case was that my code used two different strategy objects for read and write - and those used different settings for the maxLevel parameter of the GeoHashPrefixTree. Never do that! ;-)

0

i was implement in lucene v6, my code below can sort from nearest and if you want to sort from farest change value in valueSource.getSortField(true)

        SpatialContext ctx = SpatialContext.GEO;
        String[] c = cord.split(",");
        double lat = Double.parseDouble(prms.getValue(c[0],true));
        double lng = Double.parseDouble(prms.getValue(c[1], true));
        Point currPt = ctx.makePoint(Double.valueOf(lng), Double.valueOf(lat));
        
        SpatialPrefixTree grid = new GeohashPrefixTree(ctx, 22);
        SpatialStrategy strategy = new RecursivePrefixTreeStrategy(grid, "location");
        ValueSource valueSource = strategy.makeDistanceValueSource(currPt);

        Sort distanceSort = new Sort(valueSource.getSortField(false)).rewrite(searcher);
        TopDocs topDocs = searcher.search(baseQuery, Integer.MAX_VALUE,distanceSort);
        ScoreDoc[] scoreDocs = topDocs.scoreDocs;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.