moving the solr-accessing stuff into helper module, which probably isn't the right place but it's better than having it in the controller, right?

This commit is contained in:
Dan Buch 2011-08-31 12:11:37 -04:00
parent d819d87268
commit b47a145ef6
2 changed files with 20 additions and 12 deletions

View File

@ -3,22 +3,13 @@ require 'rexml/document'
class SolrController < ApplicationController class SolrController < ApplicationController
$select_url = 'http://localhost:8983/solr/select/' include SolrHelper
def index def index
@search = request[:q] @search = request[:q]
if @search != nil
raw_results = open("#{$select_url}?q=#{@search}&indent=on").read
xml_results = REXML::Document.new raw_results
@results = [] @results = []
REXML::XPath.each(xml_results, '//result/doc') do |doc| if @search != nil
@results.push({ @results = get_results(@search)
:id => doc.elements['./str[@name="id"]'],
:description => doc.elements['./str[@name="name"]']
})
end
end end
end end

View File

@ -1,2 +1,19 @@
module SolrHelper module SolrHelper
$select_url = 'http://localhost:8983/solr/select/'
def get_results(search)
raw_results = open("#{$select_url}?q=#{search}").read
xml_results = REXML::Document.new raw_results
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 end