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'] }
Advertisements