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".)

1 comment:

evindha said...

By using acts_as_solr we can do :
Model.find_by_solr(query)

By using acts_as_ferret we can do :
Model.find_with_ferret(query)

How to search in any model by using DelSolr?

example:
I have two models, User and Address
I want to search User which have value "amanda", so I run this :

c = DelSolr::Client.new(:server => 'localhost', :port => 8982)
rsp = c.query('standard', :query => 'amanda')

And then I get 1 result.

How to search value "nevada" in Address model ?

If I run query like this :
rsp = c.query('standard', :query => 'amanda')
I have no results.

Please help me for this problem.

Thank you.