ruby-on-railsstatisticssunspot-solr

Group by stats in Solr using Sunspot


I have stats for some particular query that is calculating sum of prices for the entire set of results. I would like to have different stat for different group inside returned results.

For example, sum of prices for results with status = 'In progress' separated from sum of prices for results with status = 'Complete'.

Is something like this possible using sunspot?

Existing query:

 Residential.search do
     with(:zip_code, <zip_code>)

     stat(:list_price, :type => "min")
     stat(:list_price, :type => "max")
     stat(:list_price, :type => "mean")
     stat(:square_feet, :type => "min")
     stat(:square_feet, :type => "max")
     stat(:square_feet, :type => "mean")
 end

Solution

  • Have a look at the solr Stats Component page

    http://wiki.apache.org/solr/StatsComponent
    

    There you can see the stat component has a possibility to generate a stat for facet field.

    This means you can generate a solr query like

    q=*:*&stats=true&stats.field=list_price&stats.field=square_feet&stats.facet=status
    

    This will return the global stat and a stat for each status for the fields list_price and square_feet

    The resultset will look like the following:

    I am assuming you are using the sunspot stats library https://github.com/giovannelli/sunspot_stats

    And there they say that the param after your_field is the facet field

    stat(:your_field, :facet => :your_facet_filed, :type => "min")