So, I'm trying to figure out how to have aggregate fields in my sphinx index. (Count, and AVG)
I have a model named business_profile, which has a has_many association to business_feedbacks:
class BusinessProfile < ActiveRecord::Base
has_many :business_feedbacks
#...
end
Trying to add aggregate fields for count, and avg:
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
I get this error when I try to rebuild my index (rake ts:rebuild):
ThinkingSphinx::MissingColumnError: column COUNT(business_feedbacks.id) does not exist
What can I do to add these aggregate fields to my index? I would like to be able to sort results by feedback_count, or feedback_recommend.
Because you're not referring to columns directly, you'll need to specify the type manually:
has 'COUNT(business_feedbacks.id)', as: :feedback_count, type: :integer
has 'AVG(business_feedbacks.recommend)', as: :feedback_recommend, type: :integer
… or if the recommend column is something other than an integer, you can change the average type accordingly.