Question

I have a model Item which has many tags (using acts-as-taggable-on)

Using sunspot, I search for items like so:

@search = Item.search do
  fulltext params[:search]
  paginate page: params[:page], per_page: 20
end

This works fine but, for the items returned from the search, I want to also be able to show a list of unique tags with a count of occurrences of each tag.

I want to be able to do something like this:

@serch.results.tag_counts 

(tag_counts is a class method of Item added by acts-as-taggable-on)

This would work fine if this was an ActiveRecord query, but obviously my search results have the class Sunspot::Search::PaginatedCollection rather than Item or ActiveRecord::Relation::ActiveRecord_Relation_Item - which means my class methods are not accessible.

Even if I did have access to the tag_counts class method, this would only return the results for my paginated collection - it would only work for the first 20 items.

Any ideas about how to achieve this for all matching search results?

Was it helpful?

Solution

If you want to implement such a functionality you should use a "facet".

In sunspot this looks something like this:

Post.search do
  [...]
  facet :category_ids, :author_id
end
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top