ruby-on-railsrubysphinxfacetthinking-sphinx

How to do multi facets search in thinking sphinx rails


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?


Solution

  • I think this code does what you want - though you may need to tweak things to get exactly what you're after:

    Data.search(
      :select => "groupby() AS year, MIN(price) as min_price, count(DISTINCT sphinx_internal_id) as count",
      :group_by => :year,
      :max_matches => 1000,
      :middleware => ThinkingSphinx::Middlewares::RAW_ONLY
    )
    

    Each option explained: