Search code examples
solrlucenegeospatialspatialspatial-query

Lucene geo spatial ring query


I want use Lucene Spatial search to find points in a ring with an inner radius and outer radius. Query should return the points inside the outer radius, but outside the inner radius. How do you do this? Below code is my attempt at doing this. Is this the right way to do it?

SpatialContext ctx = SpatialContext.GEO;
Circle innerCircle = ctx.makeCircle(lng, Lat, innerRadius);     
SpatialArgs innerArgs = new SpatialArgs(SpatialOperation.IsDisjointTo, innerCircle);        
Filter filter = strategy.makeFilter(innerArgs);     

Circle outerCircle = ctx.makeCircle(lng, Lat, outerRadius);     
SpatialArgs outerArgs = new SpatialArgs(SpatialOperation.Intersects, outerCircle);      
Query query = strategy.makeQuery(outerArgs);        

IndexSearcher searcher = new IndexSearcher(indexReader);
TopDocs docs = searcher.search(query, filter, 50);      

Basically using Query and Filter to achieve an AND between the two spatial operations. Feels hacky though. Will this work? Is there a better way?


Solution

  • Since you are using simple circular ring in your case, you can use frange on the geoDist function, to achieve your desired result.

    For eg. to get the results between 2km and 3km of radius, you can user something like this,

        fq={!frange l=2 u=3}geodist()