How do I set up a facet search with a many to many relationship using Sunspot? How do I set up a facet search with a many to many relationship using Sunspot? ruby-on-rails ruby-on-rails

How do I set up a facet search with a many to many relationship using Sunspot?


Anything you want to filter, facet, or order on, Sunspot needs to know about. So in your model:

searchable do  text :first_name, :surname  integer :skill_ids, :multiple => true, :references => Skillend

Your #search call in your controller looks right. In your view, you'd do something along these lines:

- @search.facet(:skill_ids).rows.each do |row|  = row.instance.name

row.instance will return the instance of Skill that the row's value refers to (that's what the :references option is doing in the searchable definition).

I'm not sure what you mean by "select multiple facets to search by" -- one can generate multiple facets (which give users choices for further search refinement) by calling the facet method multiple times in a search; and you can then use their facet choices with scope restrictions using the with method, which you can also call as many times as you'd like.

Speaking of wikis, most of this information is available (with more explanation) in the Sunspot wiki: