#44: multivalue facet fields in acts_as_solr

Solr supports faceted search, which is good, but acts_as_solr has pretty terribly documentation, which is bad.

facet field type, use an array

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

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}}

Think you've got a better solution? Help 148c663fd665c53518538e093ce3c2ab_small kueda out by posting your solution