SolRuby

Using Solr's Ruby output

Solr has an optional Ruby response format that extends its JSON output in the following ways to allow the response to be safely eval'd by Ruby's interpreter:

Here is a simple example of how one may query Solr using the Ruby response format:

require 'net/http'

h = Net::HTTP.new('localhost', 8983)
hresp, data = h.get('/solr/select?q=iPod&wt=ruby', nil)
rsp = eval(data)

puts 'number of matches = ' + rsp['response']['numFound'].to_s
#print out the name field for each returned document
rsp['response']['docs'].each { |doc| puts 'name field = ' + doc['name'] }

Ruby on Rails


Searchable example:

    class SomeModel
        include Searchable
        index_attr :name, :boost => 2.0
        index_attr :children do |attr|
            attr.name :child
            attr.include :name
        end
    end

Now, to index:

    SomeModel.add_to_index # (happens automatically on save/update/destroy)

And to search:

    SomeModel.search("year:1865", :offset => 0, :limit 20)

CategoryQueryResponseWriter

last edited 2007-09-12 03:29:56 by mbryzek