Upcoming and OnDemand Webinars View full list

Edge Sunspot: Native Geospatial Searches

Andy Lindeman

Sunspot

Sunspot, a Ruby interface for Solr usually backed by ActiveRecord objects, recently introduced native geospatial searching capability in the master branch (currently a 2.0.0 prerelease).

While the functionality has not been released as a stable version yet, I felt that a blog post introducing the features is appropriate with the hope that folks will try it out and give feedback!

Read on for a short example.

Imagine a Rails application has an Office model that, among other things, describes the location of a company’s office. Through some other means (maybe geocoding), the application determines and stores the latitude and longitude of each office as #latitude and #longitude, respectively.

Setting up the Rails application to use Sunspot and Solr is easy, as described in the README, so this post won’t explictly cover it. To get these new features, though, make sure to use a 2.0.0 prerelease (e.g., gem 'sunspot_rails', '~>2.0.0.pre'). After setup, the Office model can be made searchable with:

When new offices are created and saved, they are also indexed in Solr. The #latitude and #longitude attributes are combined into a latlon typed field. latlon fields are specially indexed in Solr to respond efficiently to geospatial queries.

Now, it’s easy to search for offices within a specified area:

It’s also easy to sort results by closeness to a point:

There are a few more details in the README. To reiterate, these features are only in a prerelease version at this point (~>2.0.0.pre) and require Solr >= 3.1.

If you have any feedback, please file an issue.

Not Happy with Your Current App, or Digital Product?

Submit your event

Let's Discuss Your Project

Let's Discuss Your Project