Using thinking-sphinx for facets in my app and define two facets price and year.
I want to fetch facets nested results.
Now I am getting facets by year and it give count against year.
years = records[:year]
Along with the count of records along with year I also want to get min price in that year.
Now I getting it like
years.map do |year,count|
price = Data.where(:year=>year).minimum(:price)
{count: count,cheapest_price: price}
end
But its like LazyLoading as N+1 against year. I want to fetch prices along within year facets.
Is there any way to do it?
I think this code does what you want - though you may need to tweak things to get exactly what you're after:
Each option explained:
selectspecifies the appropriate aggregations you're after, andgroupby()is a function that refers to the attribute this query is grouping by.group_byspecifies the attribute you want the query to group by. Sphinx's equivalent ofGROUP BYin SQL, though in the past it has been more limited.max_matchesshould be set to 1000, unless you've configured it to be a higher value in your Sphinx configuration (viaconfig/thinking_sphinx.yml). Using the highest possible value ensures the facet results scan as many documents as possible. You could also set:limitto the same value for the specific number of rows being returned here.middlewareuses a limited set of Thinking Sphinx middleware classes, to avoid translating results into ActiveRecord objects - instead, you'll get an array of hashes with the Sphinx query data.