ruby-on-railsruby-on-rails-3solrruby-on-rails-pluginssunspot

Rails 3 Sunspot solr search within bounds


I have a question to ask regarding the new Sunspot/solr feature: restriction with near I have (via the google geocoding API) the viewport and the bounds which are latitude/longitude coordinates of the southwest and northeast corners of a bounding box. I want Sunspot/Solr to search within this bounding box but I haven't figured it out yet. So my question is: Is it possible to make Solr (or via any of the solr plugins) capable for searches within a bounding box? If yes, how?

Thank you


Solution

  • You can just create a lat and lng field of type trie double, and then do two range queries (one for a lat range, one for a lng range).

    class Product < ActiveRecord::Base
      seachable do
         double :latitude
         double :longitude
      end
    end
    
    # search
    Product.search do
      with :latitude, 36.5667..80.4553 
      with :longitude, 76.4556..67.9987
    end