Solution for: #44: multivalue facet fields in acts_as_solr
facet field type, use an array
- kueda on May 11, 2009, 07:02 PM UTC
After digging through docs and a few emails, this seems to work
Let's assume you have a Species model that has many associated Colors.
In species.rb
The thing that was throwing me was that I wasn't specifying the type of the colors field, or I was specifying it to be a string, and my colors method was returning a joined string of all the colors. Turns out acts_as_solr does the right thing if you just give it a Ruby array (though that doesn't seem to be documented anywhere).
And to perform the query:
Let's assume you have a Species model that has many associated Colors.
In species.rb
acts_as_solr :fields => [
:accepted_name,
{:colors => :facet},
{:size => :range_float}
],
:facets => [:colors]
def colors
colors.all.collect(&:name)
end
The thing that was throwing me was that I wasn't specifying the type of the colors field, or I was specifying it to be a string, and my colors method was returning a joined string of all the colors. Turns out acts_as_solr does the right thing if you just give it a Ruby array (though that doesn't seem to be documented anywhere).
And to perform the query:
>> results = Species.find_by_solr('rabbit', :facets => {
:zeros => false,
:fields => [:colors],
:query => [
'size:[0 TO 100]',
'size:[100 TO 500]',
'size:[500 TO 1000]'
]
})
>> results.facets
=> {"facet_fields"=>{"colors_facet"=>{""=>61, "red"=>5, "green"=>2, "blue"=>1, "yellow"=>1, "green"=>1, "orange"=>3, "pink"=>3, "magenta"=>1}}, "facet_dates"=>{}, "facet_queries"=>{"size_rf:[0 TO 100]"=>16, "size_rf:[100 TO 500]"=>34, "size_rf:[500 TO 1000]"=>17}}