使用Thinking Sphinx定义关联模型的索引

Igo*_*lek 3 indexing ruby-on-rails thinking-sphinx ruby-on-rails-3

使用以下配置在关联模型上定义索引的正确方法是什么?

我的模型Localitylatlng属性和关联模型ProfileUser

class User < ActiveRecord::Base
  has_one :user_profile

  define_index do
    # This doesn't work :(
    has "RADIANS(user_profiles.localities.lat)", :as => :lat, :type => :float
    has "RADIANS(user_profiles.localities.lng)", :as => :lng, :type => :float
  end
end

end

class UserProfile < ActiveRecord::Base
  belongs_to :user
  belongs_to :locality
end

class Locality < ActiveRecord::Base
  has_many :user_profiles
end
Run Code Online (Sandbox Code Playgroud)

我需要为User模型定义索引,以便我可以对它进行地理搜索.

谢谢你的答案!

pat*_*pat 10

问题是双重的:

  • 思考Sphinx不知道您想加入user_profile和locality association.
  • SQL代码片段应该只是 - 标准SQL - 因此您无法在其中链接关联.

以下应该做的伎俩:

define_index do
  # force the join on the associations
  join user_profile.locality

  # normal SQL:
  has "RADIANS(localities.lat)", :as => :lat, :type => :float
  has "RADIANS(localities.lng)", :as => :lng, :type => :float
end
Run Code Online (Sandbox Code Playgroud)

Claudio在SQL中使用单一引用的观点是不正确的 - 在SQL中,您需要表名.但是你确实想要在连接调用中使用正确的关联引用(因此它们在那里是单数).

干杯