Skip to content
This repository has been archived by the owner on Sep 7, 2020. It is now read-only.

Commit

Permalink
add simple_search_docs shortcut method
Browse files Browse the repository at this point in the history
  • Loading branch information
tilsammans committed Sep 29, 2011
1 parent 3a06db0 commit 746cff3
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 27 deletions.
1 change: 1 addition & 0 deletions lib/simple_solr.rb
@@ -1,4 +1,5 @@
require 'active_record'
require 'nokogiri'
require 'httparty'
require 'builder'

Expand Down
15 changes: 8 additions & 7 deletions lib/simple_solr/search.rb
Expand Up @@ -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
29 changes: 9 additions & 20 deletions spec/simple_solr/search_spec.rb
Expand Up @@ -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

0 comments on commit 746cff3

Please sign in to comment.