diff --git a/lib/simple_solr.rb b/lib/simple_solr.rb index fbaa488..f80e9fd 100644 --- a/lib/simple_solr.rb +++ b/lib/simple_solr.rb @@ -1,4 +1,5 @@ require 'active_record' +require 'nokogiri' require 'httparty' require 'builder' diff --git a/lib/simple_solr/search.rb b/lib/simple_solr/search.rb index fc7872d..88a3d51 100644 --- a/lib/simple_solr/search.rb +++ b/lib/simple_solr/search.rb @@ -5,15 +5,16 @@ module Search # # Product.simple_search 'delicious', :fq => "category:fruit" # - # Returns a hash with the search results: - # - # { - # "lst" => [{"int"=>["0", "18"], "name"=>"responseHeader"},{"name"=>"highlighting"}], - # "result" => {"name"=>"response", "numFound"=>"0", "start"=>"0", "maxScore"=>"0.0"} - # } + # Returns a Nokogiri::XML::Document. def simple_search(query, params={}) query = {:q => query} - get(SimpleSolr.configuration.uri + "/select", :query => query.merge(params)).parsed_response['response'] + response = get(SimpleSolr.configuration.uri + "/select", :query => query.merge(params)) + Nokogiri::XML(response.body) + end + + # Returns all +doc+ elements, aka matching documents, from the search results in an array. + def simple_search_docs(query, params={}) + simple_search(query, params).css('doc') end end end \ No newline at end of file diff --git a/spec/simple_solr/search_spec.rb b/spec/simple_solr/search_spec.rb index 7cc3b1f..05bcd3a 100644 --- a/spec/simple_solr/search_spec.rb +++ b/spec/simple_solr/search_spec.rb @@ -9,32 +9,21 @@ ) describe SimpleSolr::Search do - let(:response) { stub("response")} - let(:httparty) { stub("httparty", :parsed_response => {'response' => response})} - + describe SimpleDocument do - it "responds to search" do - SimpleDocument.should respond_to(:simple_search) - end - it "gets results" do - SimpleDocument.should_receive(:get).with("http://test.local:8983/solr/select", :query => {:q => 'bonanza'}).and_return(httparty) - SimpleDocument.simple_search 'bonanza' - end - - it "allows parameters" do - SimpleDocument.should_receive(:get).with("http://test.local:8983/solr/select", :query => {:q => 'bonanza', :fq => "brand_site:www.example.com"}).and_return(httparty) - SimpleDocument.simple_search 'bonanza', :fq => "brand_site:www.example.com" + it "returns a Nokogiri::XML::Document" do + SimpleDocument.simple_search('bonanza').should be_a(Nokogiri::XML::Document) end - it "result is not empty" do - SimpleDocument.simple_search('bonanza')['result'].should_not be_empty + it "returns one document" do + SimpleDocument.simple_search('bonanza').css('doc').length.should eq 1 end - it "finds str results" do - str = SimpleDocument.simple_search('bonanza')['result']['doc']['str'] - str[0].should == 'www.zappelin.nl' - str[0].attributes.should == {"name" => "brand_site"} + it "returns one simple search result" do + SimpleDocument.simple_search_docs('bonanza').length.should eq 1 end + end + end