box-o-sand/RubyFun/rails/hamster/app/helpers/solr_helper.rb
Dan Buch 0a9428093c Add 'RubyFun/' from commit 'b01c6826131196ba58b5288a3182f2526c89c249'
git-subtree-dir: RubyFun
git-subtree-mainline: a04a502787
git-subtree-split: b01c682613
2013-01-09 23:50:14 -05:00

31 lines
794 B
Ruby

module SolrHelper
$select_url = 'http://localhost:8983/solr/select/'
$redis = '/home/me/repos/redis.git/src/redis-cli'
def get_results(search)
cached = `#{$redis} GET "query:#{search}"`
if cached.strip().length > 0
# puts "cached=#{cached}"
xml_results = REXML::Document.new cached
else
raw_results = open("#{$select_url}?q=#{search}").read
# puts "caching '#{raw_results}'"
`#{$redis} SET "query:#{search}" '#{raw_results}'`
xml_results = REXML::Document.new raw_results
end
results = []
REXML::XPath.each(xml_results, '//result/doc') do |doc|
results.push({
:id => doc.elements['./str[@name="id"]'],
:description => doc.elements['./str[@name="name"]']
})
end
return results
end
end