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:
Ruby's single quoted strings are used to prevent possible string exploits
\ and ' are the only two characters escaped...
unicode escapes not used... data is written as raw UTF-8
nil used for null
=> used as the key/value separator in maps
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
acts_as_solr: A plugin to add full text search capabilities using Solr to ActiveRecord models.
nice acts_as_solr tutorial
acts_as_background_solr: An extension to acts_as_solr moving more processing to the background. Flare: A plugin adding faceted browsing, AJAX suggest and more to Rails controllers.
Searchable: A Rails plugin that provides search integration with ActiveRecord. (an alternative to acts_as_solr)
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)