Order Solr / sunspot Search Results by Geographical Location

I want to be able to order my search results by quantity and location. Each user in the database has lat / lot, and now I index:

location :coordinates do Sunspot::Util::Coordinates.new latlon[0], latlon[1] end 

The model I searched with is also indexed in the same way. Essentially, I'm trying to get results to be ordered by invoice, and then by location. Therefore, if I am looking for Walmart, I would like all Walmart to be ordered by geographic proximity to my location.

I remember that I read something about the new geo-sorting of solr, but I'm not sure if this is due to alpha and / or if sunspot implemented the wrapper.

What would you recommend?

+4
source share
1 answer

Due to the fact that Sunspot calculates the types of locations, you will need to do extra leg work so that it sorts by distance from your target. The way it works is that it creates a geo-hash for each point, and then performs a search using regular full-text search on that geo-hash. As a result, you probably won’t be able to determine if the point is 10 km further than the point located 5 km, but you can find out if the point is 50 km further than 1-2 km. The exact distances are arbitrary, but the result is that you probably won’t have the subtle result you want, and the search acts more as a way to filter out points that are in an acceptable proximity. After you filter your points using the built-in location search, there are three ways to accomplish what you want:

  • Go to Solr 3.1 or later and update your schema.xml to use the new spatial search columns . Then you need to make custom changes to Sunspot to create fields and orders that work with these new data types. As far as I know, they are not yet available in Sunspot, so you will need to make these connections yourself, and you will have to dig into Solr to perform some manual configurations.
  • Use the Spatial Solr Plugin . You will need to install the new JAR in your Solr directory and you will have to make some changes to Sunspot, but they are relatively painless and complete instructions can be found here .
  • Use your database if your database is also indexed in location columns, then you can use Sunspot's built-in location search to filter the results to a reasonable size. You can then query the database for these results and sort them by proximity to your location using your own distance function.
+6
source

All Articles