How can I get distances from lucene geo (spatial) search with version 4.0.0?

I'm trying to use the geolocation search function with the latest version of Lucene (4.0.0), the requirement is simple: getting points inside the circle (center and radius are passed as a query condition). I can not find an API that displays the distance of each result to the center, I have to calculate the distance after I exit the latitude and longitude of each result. can anyone help? The code is shown below:

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

Easy to get to Lucene 3.X, anyone familiar with geospatial searches with Lucene 4.0.0?

+4
source share
1 answer

You have lat and lon from the field; Now you need to calculate the distance from the center point of the query circle. In your code, it will look like this:

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

Not bad; ehh? (ps I wrote most of Lucene 4 spaces)

+5
source

Source: https://habr.com/ru/post/1444034/


All Articles