Monday, September 8, 2008

DelSolr - Solr Facets Made Easy in Ruby

delsolr - lightweight solr wrapper for ruby Here's an example of solr faceting made simple via delsolr:
c = DelSolr::Client.new(:server => 'solr1', :port => 8983)

# faceting by field...
rsp = c.query('dismax', :query => 'mp3 player',
                        :facets => [{:field => 'brand', :limit => 5, :mincount => 1}]

# now let's output the facet values and counts
rsp.facet_field_values('brand').each do |brand|
  puts "#{brand} - #{rsp.facet_field_count('brand', brand)}"
end

# faceting by query... with filtering...
rsp = c.query('dismax', :query => 'mp3 player',
                        :filters => {:onsale => true},
                        :facets => [{:query => "instock:true", :name => 'instock'},
                                    {:query => {:price => (0..50), :description => 'cool'}, :name => 'cool_and_cheap'}]) 

# now let's output some query facets
puts "Instock: #{rsp.facet_query_count_by_name('instock')}"
puts "Sweet: #{rsp.facet_query_count_by_name('cool_and_cheap')}"

We've been using this code internally for a bit, so we'd figure we'd open it up as a gem to see if other people find it useful. There are a number of other gems/plugins that do something similar, but we weren't satisfied with the balance of power and simplicity in any of them, particularly when it comes to faceting. The full documentation is available on the delsolr page

2 comments:

goodieboy said...

Hi, just wondering why you decided not to use the solr-ruby gem instead? Solr-ruby has all of the mapping, support for different query types and even indexing capabilities. Admittedly, the project needs some help and more developers, maybe you could consider looking at it, and submitting your ideas, patches etc.? Good work by the way!

Matt Mitchell

DETArmstrong said...

I've been looking into this as a lightweight alternative to acts_as_solr.
I have solr set up on tomcat for multiple instances, so my solr home is something like localhost/solr1.
Is there a configuration option to do this?