如何用思维狮身人面像索引聚合关联字段

How to index aggregate association fields with thinking sphinx

所以,我想弄清楚如何在我的 sphinx 索引中包含聚合字段。 (计数和平均值)

我有一个名为 business_profile 的模型,它与 business_feedbacks 有 has_many 关联:

class BusinessProfile < ActiveRecord::Base
    has_many :business_feedbacks
    #...
end

正在尝试为计数和平均值添加聚合字段:

ThinkingSphinx::Index.define(:business_profile, with: :active_record, delta: ThinkingSphinx::Deltas::DatetimeDelta) do
    # indexes ... fields
    has 'COUNT(business_feedbacks.id)', as: :feedback_count
    has 'AVG(business_feedbacks.recommend)', as: :feedback_recommend
end 

我在尝试重建索引时遇到此错误 (rake ts:rebuild):

ThinkingSphinx::MissingColumnError: column COUNT(business_feedbacks.id) does not exist

如何将这些聚合字段添加到我的索引中?我希望能够按 feedback_count 或 feedback_recommend.

对结果进行排序

因为您没有直接引用列,所以您需要手动指定类型:

has 'COUNT(business_feedbacks.id)',      as: :feedback_count,     type: :integer
has 'AVG(business_feedbacks.recommend)', as: :feedback_recommend, type: :integer

...或者如果推荐列不是整数,您可以相应地更改平均类型。