Please log into your user account or create a new one
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
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}}
