diff --git a/rails/hamster/app/controllers/solr_controller.rb b/rails/hamster/app/controllers/solr_controller.rb index b4a6e5d..57621c9 100644 --- a/rails/hamster/app/controllers/solr_controller.rb +++ b/rails/hamster/app/controllers/solr_controller.rb @@ -3,22 +3,13 @@ require 'rexml/document' class SolrController < ApplicationController - $select_url = 'http://localhost:8983/solr/select/' + include SolrHelper def index @search = request[:q] + @results = [] if @search != nil - raw_results = open("#{$select_url}?q=#{@search}&indent=on").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 - + @results = get_results(@search) end end diff --git a/rails/hamster/app/helpers/solr_helper.rb b/rails/hamster/app/helpers/solr_helper.rb index 18437f1..adea5ec 100644 --- a/rails/hamster/app/helpers/solr_helper.rb +++ b/rails/hamster/app/helpers/solr_helper.rb @@ -1,2 +1,19 @@ 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