Question

I have researched on this question but found nothing close. Thats why I decide to ask. Stackoverflow has been a tremendous help for me.

I have a jobs table with one attribute is location. Each job.location is either in New York, Boston, or both (New York & Boston).

In sunspot solr for rails, how do I create facet for this attribute so that if a job.location is in both cities, the job can be displayed in the results when either New York or Boston is selected in the facet?

Should it be like an array ['New York', 'Boston']?

Thanks!

Was it helpful?

Solution

You should change your design so that you have a separate Location class and set the Job class to have has_and_belongs_to_many :locations because it is a many-to-many relationship. Then you can create an integer scope in your Job class searchable block for the location:

integer :locations, :multiple => true

to allow multiple locations per job.

Now it is easy to add a facet(:locations) that will do exactly what you want.

The above works for any number of locations, not only two. However, if you don't want to create a new class/table and are sure you only have 1 or 2 locations, you can just create a query scope field:

integer :locations, :multiple => true do
  if location == 'NY & Boston'
    ['NY', 'Boston']
  else
    [location]
  end
end
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top