2

私は自分のアプリケーションで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

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

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

# 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 %>

4

2 に答える 2

7

最初に @m_x が言ったことは正しいです。Store#near の結果を @search オブジェクトに割り当てることは期待できません....

https://github.com/sunspot/sunspotのGeospatial に関するドキュメントを読むだけで、いくつか不足していることがわかります。

ジオコーダーが必要とする緯度と経度のフィールドを使用して、地理空間フィールドを宣言する必要があります。

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

次に、次のように (インデックスを作成した後に) 検索できます。

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

ここで :precision オプションについても読んでください http://sunspot.github.com/docs/Sunspot/DSL/RestrictionWithNear.html#near-instance_method

ジオコーダーについてhttps://github.com/alexreisner/geocoder

于 2011-12-17T19:42:41.380 に答える
3

上記であなたが何をしようとしているのかを完全に理解しているかどうかはわかりませんが、ジオコーダーと黒点を一緒に再生しようとする際に同様の状況があります.

これはまだテストされていませんが、これが私の現在の考えです....

Event.search do
      keywords  search_terms
      any_of do
        with(:id).any_of(Event.near(coordinates,50).map(&:id))
      end
end.results

基本的に、2 つの結果セットを組み合わせて、距離とキーワードの両方の用語に一致する結果のみを戻そうとしています。

これが少なくとも誰かの助けになることを願っています。

于 2011-12-14T22:51:53.693 に答える