使用地理编码器和太阳黑子搜索?

Lea*_*RoR 2 ruby ruby-on-rails sunspot ruby-on-rails-3 rails-geocoder

我在我的应用程序中使用了GeocoderSunspot gem,我有一个叫做的字段:search_near_address,假设用户能够输入他们想在附近搜索的地址X amount of miles.我想要映射到的是我:address用于该:search_near_address领域的商店.这样,用户可以在:search_near_address现场输入地址(即451 University Avenue,Palo Alto,CA),它将在半径50英里的范围内搜索.

回答

太阳黑子1.2.1


class Store < ActiveRecord::Base
   attr_accessible :address, :latitude, :longitude
   has_many :products
   geocoded_by :address
   after_validation :geocode
   reverse_geocoded_by :latitude, :longitude
   after_validation :reverse_geocode      
end
Run Code Online (Sandbox Code Playgroud)
class Product < ActiveRecord::Base
   belongs_to :store

   searchable do # Searching with product model.
     string :search_near # For rake sunspot:reindex
     location :location
   end

   def search_near_address
 store.address if store # You may have to use the "if store".
   end

   def location
    # may need the "if store" after longitude)....
Sunspot::Util::Coordinates.new(store.latitude, store.longitude)
   end
end
Run Code Online (Sandbox Code Playgroud)
class SearchController < ApplicationController

  def index
    @search = Product.search do |q| # Search with sunspot
      q.fulltext params[:search]
      q.with(:location).near(*Geocoder.coordinates(params[:search_near_address]), :precision => 4) if params[:search_near_address].present?
    end 
    @products = @search.results # Return results from Product.search block.
  end
end
Run Code Online (Sandbox Code Playgroud)
# search/index/html.erb
<%= form_tag results_search_index_path, :method => 'get' do %>
    <%= text_field_tag :search, params[:search] %>
    <%= text_field_tag :search_near_address, params[:search_near_address] %>
    <%= submit_tag "Go", :name => nil %>
<% end %>
Run Code Online (Sandbox Code Playgroud)

bru*_*cat 7

首先,@ m_x说的是对的,你不能指望在#search对象附近分配Store#会起作用....

只需阅读https://github.com/sunspot/sunspot中关于地理空间的文档,您就可以清楚地看到您遗漏了一些内容:

您必须使用地理编码器所需的纬度和经度字段声明您的地理空间字段:

class Product < ActiveRecord::Base
   belongs_to :store

   searchable do # for Sunspot search.
     string :search_near
     latlon(:location) { 
       Sunspot::Util::Coordinates.new(category.latitude, category.longitude)
     }
   end

   def search_near
     #I changed this because business_store will not work out.
     store.address
   end
end
Run Code Online (Sandbox Code Playgroud)

然后你可以搜索(在索引之后)像这样:

class SearchController < ApplicationController

  def index
    # Search with sunspot
    @search = Sunspot.search(Product) do
      fulltext params[:search]

      # The "*" pop off the elements of the array that 
      # Geocoder.coordinates returns.
      with(:location).near(*Geocoder.coordinates(params[:search_near]), 
                           :precision => 6)

      # NOTE: You could also use the in_radius method but only in the pre-release version:
      # with(:location).in_radius(*Geocoder.coordinates(params[:search_near]), 100) if params[:search_near].present?
    end

    # Return results from Product.search block.
    @products = @search.results 
  end
end
Run Code Online (Sandbox Code Playgroud)

另请参阅这里的:precision选项http://sunspot.github.com/docs/Sunspot/DSL/RestrictionWithNear.html#near-instance_method

关于Geocoder https://github.com/alexreisner/geocoder