Reputation: 4606
I have this model:
class Job < ActiveRecord::Base
has_many :groups, class_name: 'JobGroup', inverse_of: :job, dependent: :destroy
has_many :placements, through: :groups
end
Group
model has size
attribute and Placement
has name
attribute. Then I have...
Job 1 with 3 groups:
And Job 2 with 2 groups:
With thinking-sphinx I want to group by placement_name and sum sizes. Something like this:
[
"placement_names",
{
"placement_name_1": 10,
"placement_name_2": 25,
"placement_name_3": 5
}
]
What do I need to put inside my ThinkingSphinx::Index.define :job, :with => :active_record do #... end
definition to make this work?
I know that I can put something like this inside my definition: indexes placements.name, as: :placement_names, facet: true
to get the job count by placement name, but I don't know how to sum values grouped by another attribute.
UPDATE:
With the Pat's answer the solution would be:
job_ids = Job.search_for_ids
q = JobGroup.select(['placements.name', 'placements.id as placement_id', 'SUM(job_groups.size) as size'])
q = q.joins(:placement)
q = q.where(job_id: job_ids)
q = q.group('placement_id')
Upvotes: 0
Views: 200
Reputation: 16226
What you're asking for isn't really possible with Sphinx, for two reasons:
An alternative could be to perform a search to get the job ids back matching whatever the query is, and then run the aggregation in your database instead for just those jobs? Thinking Sphinx has the search_for_ids
method which takes the same arguments as search
, but returns an array of ids instead of ActiveRecord objects (which can be useful from a performance perspective).
Upvotes: 1