Please log into your user account or create a new one

#44: multivalue facet fields in acts_as_solr

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

facet field type, use an array

1
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

   1  
   2  acts_as_solr :fields => [
   3      :accepted_name,
   4      {:colors => :facet},
   5      {:size => :range_float}
   6    ], 
   7    :facets => [:colors]
   8  
   9  def colors
  10    colors.all.collect(&:name)
  11  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:

   1  
   2  >> results = Species.find_by_solr('rabbit', :facets => {
   3    :zeros => false, 
   4    :fields => [:colors], 
   5    :query => [
   6      'size:[0 TO 100]', 
   7      'size:[100 TO 500]', 
   8      'size:[500 TO 1000]'
   9    ]
  10  })
  11  
  12  >> results.facets
  13  => {"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