ruby-on-rails - 使用 Thinking Sphinx 定义关联模型的索引

标签 ruby-on-rails ruby-on-rails-3 thinking-sphinx indexing

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

我有型号 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

我需要为 User 模型定义索引,以便我可以对其执行地理搜索。

谢谢你的回答!

最佳答案

问题是双重的:

  • Thinking Sphinx 不知道您要加入 user_profile 和 locality 关联。
  • 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
    

    Claudio 在 SQL 中使用单数引用的观点是不正确的 - 在 SQL 中,您需要表名。但是您确实希望在 join 调用中使用正确的关联引用(因此它们在那里是单数)。

    干杯

    关于ruby-on-rails - 使用 Thinking Sphinx 定义关联模型的索引,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4993939/

    相关文章:

    ruby-on-rails - 如何仅在登录时运行 setInterval ajax 调用?

    ruby-on-rails - 思考 sphinx rails 不运行

    ruby-on-rails - 如何在仅 API 的 Rails 应用程序中添加助手

    ruby-on-rails - 错误:执行 gem 时 ... (Gem::FilePermissionError) 您没有/Library/Ruby/Gems/2.6.0 目录的写权限

    mysql - 为什么我的表的编码错误?

    ruby-on-rails - EventMachine 与 Node.js

    ruby-on-rails - rails 翻译丢失错误

    ruby-on-rails - 如何使用 Rspec 测试嵌套资源的 Controller ?

    ruby-on-rails - Sphinx - 何时对字段使用 'has' 和 'indexes'

    mysql - ThinkingSphinx::SphinxError 失去与 MySQL 服务器的连接 ( 'reading initial communication packet')