2
class Office < ActiveRecord::Base
    has_many :users

    searchable do
       text :name
       location :coordinates do
           Sunspot::Util::Coordinates.new(latitude, longitude) 
       end 
    end     
end

class User < ActiveRecord::Base
    belongs_to  :office

    searchable do
        text :name, :default_boost => 2
        text :description
    end
end

この種のセットアップでは、Rails で SunSpot (Solr 上) を使用して、特定の緯度/経度内のユーザーを検索するにはどうすればよいですか? たとえば、私はこれを行うことができるようにしたい:

  @search  = User.search() do
    fulltext(params[:q])
    with(:coordinates).near(@lat, @long, :precision => 5)
  end

以下は問題なく動作します。

  @search  = Office.search() do
    fulltext(params[:q])
    with(:coordinates).near(@lat, @long, :precision => 5)
  end

各ユーザーの緯度/経度が実際に Office クラスに存在することを考えると、これを達成するための最良の方法は何ですか?

4

1 に答える 1

4

office関連付けは、ユーザーの検索可能なブロック内のスコープ内にある必要があります。

それを考えると、ここから始めます(テストされていない、頭のてっぺんなど):

class User < ActiveRecord::Base
  belongs_to  :office

  searchable do
    text :name, :default_boost => 2
    text :description
    location :coordinates do
      Sunspot::Util::Coordinates.new(office.latitude, office.longitude)
    end
  end
end

このようなブロック内の関連付けられたオブジェクトの値を取得することは、Sunspot で非正規化を処理するための実際にはかなり一般的なパターンです。

于 2011-05-20T06:59:19.600 に答える