1
votes

I have setup faceted search using Sunspot, SOLR. Like this:

for row in @search.facet(:facet_id).rows
    link_to row.instance.name, :url_for(:search => params[:q], :facet_id => row.value)

My issue is that when I execute the faceted search in the controller like this:

    @search = Sunspot.search(MyModel) do
      keywords search_text 
      facet    :facet_id
      with(:facet_id, params[:facet_id]) if params[:facet_id].present?
    end

The facet counts are now calculated based on the with(:facet_id, params[:facet_id]) condition. I want the facet counts to ALWAYS be calculated without this condition.

Do I need to execute two searches? One for the search without the conditions (to calculate the facet counts) and one with the condition to retrieve the results. Or is there a way to do this in one SOLR query.

Thanks

Hamish

1

1 Answers

8
votes

Solr has (and Sunspot supports) a concept called "multiselect facets", which is what you need here. Essentially you can tell Solr to ignore a condition (or multiple conditions) just for the purposes of calculating a certain facet. So what you'd want to do here is:

Sunspot.search(MyModel) do
  facet_restriction = with(:facet_id, params[:facet_id])
  facet(:facet_id, :exclude => facet_restriction)
end

Hope that helps.