Showing posts with label delsolr. Show all posts
Showing posts with label delsolr. Show all posts

Wednesday, October 22, 2008

DelSolr 0.0.2 Released - Added Support for Updates/Delete

A while ago I posted on the first version of delsolr (0.0.1) which provided support for querying solr and not much else :). We just released an update to delsolr (0.0.2). The major change is added support for updating/deleting documents. It's still fairly untested, so, standard disclaimer applies.
c = DelSolr::Client.new(:server => 'solr', :port => 8983)

# updating one document...
doc = DelSolr::Document.new
doc.add_field('id', 'ABC-123')
doc.add_field('name', '8 Gig Shuffle')
doc.add_field('description', 'A crazy-tiny MP3 player')
c.update!(doc) # posts new document to solr

# updating a whole bunch and batching the post to solr
models = SomeModel.find(:all)
models.each do |model|
  doc = DelSolr::Document.new
  doc.add_field('id', model.id)
  doc.add_field('name', model.name)
  doc.add_field('description', model.description)
  c.update(doc)
end
c.post_update! # post all the updates in one batch

c.commit! # post a commit to solr
I used a rails example, but delsolr is really intended for ruby and not necessarily rails. Acts_as_solr and the like might suit some better (but not us). This project came out of a piece of code we use internally to manage solr connections. My goal is to get delsolr to the point where we can replace our old implementation. It's not there yet, but it's getting closer. (It's slow going as this is largely done in our "free time".)

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