Search

Edge Sunspot: Native Geospatial Searches

Andy Lindeman

2 min read

Apr 16, 2012

Edge Sunspot: Native Geospatial Searches

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.

Mark Dalrymple

Reviewer Big Nerd Ranch

MarkD is a long-time Unix and Mac developer, having worked at AOL, Google, and several start-ups over the years.  He’s the author of Advanced Mac OS X Programming: The Big Nerd Ranch Guide, over 100 blog posts for Big Nerd Ranch, and an occasional speaker at conferences. Believing in the power of community, he’s a co-founder of CocoaHeads, an international Mac and iPhone meetup, and runs the Pittsburgh PA chapter. In his spare time, he plays orchestral and swing band music.

Speak with a Nerd

Schedule a call today! Our team of Nerds are ready to help

Let's Talk

Related Posts

We are ready to discuss your needs.

Stay in Touch WITH Big Nerd Ranch News