On Jun 28, 2006, at 4:22 PM, Mike Baranczak wrote:
Erik Hatcher was recently talking about a Ruby-on-Rails interface
for Solr, but I don't know what the status is on that. As far as I
know, nobody has yet written interfaces for PHP or CF, but it
shouldn't be that hard; it's just a question of creating an XML
document and sending it over HTTP.
My current code has not been distilled into a clean API, but it's
usable. It all boils down to this:
def post_to_solr(body, mode = :search)
post = Net::HTTP::Post.new(mode == :search ? "/solr/select" : "/
solr/update")
post.body = body
post.content_type = 'application/x-www-form-urlencoded'
response = Net::HTTP.start(@url.host, @url.port) do |http|
http.request(post)
end
response_dom = Document.new(response.body)
end
though using REXML's Document is likely to be pulled out to something
more optimal for Ruby ingesting, such as YAML.
This sits in a Solr class and has utility methods like this:
def optimize
post_to_solr('<optimize waitFlush="false" waitSearcher="false"/
>', :update)
end
So I can use IRB (via Rails slick script/console) and do this:
solr = Solr.new
results = solr.search([{:field => "year", :value => "1865"}], 0, 20)
I'm tinkering around with various custom request handlers, custom
parameters, and faceted results so nothing has settled down into a
stable way to do things just yet, so I haven't felt the
generalization falling into place yet. I sincerely hope someone Solr/
Lucene/Java and Ruby savvier than I will eventually step up and build
a super slick Solr Ruby DSL :) But it needs to be more flexible than
just the standard request handler to be of use to me, so it's more
complex than meets the eye.
Erik