solrsolr8

Solr - return documents that are within the radius of multiple points?


I have a set of data, where each entry has a lat/lon coordinate associated with it.

I have the following form:

I would like to return all data that has a lat/lon coordinate within a 50 mile radius of any of the lat/lon points selected in the form. What is the recommended approach to doing this with Solr?


Solution

  • You can use a field type LatLonPointSpatialField for indexing the lat/lon. coordinates. For example, Solr default configset defines the following in its schema, which is sufficient to enable spatial search :

    <!-- A specialized field for geospatial search filters and distance sorting. -->
    <fieldType name="location" class="solr.LatLonPointSpatialField" docValues="true"/>
    <dynamicField name="*_p"  type="location" indexed="true" stored="true"/>
    

    LLPSF supports toggling indexed, stored, docValues, and multiValued. LLPSF internally uses a 2-dimensional Lucene "Points" (BDK tree) index when "indexed" is enabled (the default). When "docValues" is enabled, a latitude and longitudes pair are bit-interleaved into 64 bits and put into Lucene DocValues. The accuracy of the docValues data is about a centimeter.

    Then for querying, use the geofilt query parser in a filter query. Matching entries within a circle of the given radius around any of the (multiple) lat/lon points selected means you need to OR multiple {!geofilt} queries, one for each pt. Note that the radial distance d must be specified in kilometers, eg. :

     q=*:*&sfield=coord_p&d=80.4672&fq=({!geofilt pt=x1,y1} OR {!geofilt pt=x2,y2} OR ...)