Search code examples
solrlucenesolrcloudbounding-box

Solr - Polygon Search - Sort results from center of the polygon by distance


We were previously using Lucene 4 core api, and recently moved to SolrCloud v 7.7.X.

I have the following field definition for doing spatial queries for polygons -

 <fieldType name="location_rpt"   class="solr.SpatialRecursivePrefixTreeFieldType"
               spatialContextFactory="JTS"
               autoIndex="true"
               validationRule="repairBuffer0"
               distErrPct="0.025"
               maxDistErr="0.001"
               geo="true" 
               distanceUnits="kilometers" /> 

and here is my field definition -

 <field name="LATLON" multiValued="true" type="location_rpt" indexed="true"/>

And here is my sample query -

q=*:*&fq={!field f=LATLON}Intersects(POLYGON(( -74.31737 40.61442, -73.48103 40.61442, -73.48103 41.24627, -74.31737 41.24627, -74.31737 40.61442)))&fq=TYPE:HOTEL&cache=true&fl=*,score&debug=false&rows=2000

The results are not returned in any particular order, but I want them to be always returned based on the center of the polygon by distance.

What are we missing?


Solution

  • You can use geofilt method something like this

    fq= "{!geofilt pt=" + latLng + " sfield=geoBoundary d=" + distance.get + " sort=geodist(geoBoundary," + latLng + ") asc}")
    
    • latlng = your location point
    • geoBoundary = your polygon