diff --git a/lib/tanker.rb b/lib/tanker.rb index d33a2a4..80b7e0e 100644 --- a/lib/tanker.rb +++ b/lib/tanker.rb @@ -108,7 +108,7 @@ def search(models, query, options = {}) search_on_fields = models.map{|model| model.tanker_config.indexes.map{|arr| arr[0]}.uniq}.flatten.uniq.join(":(#{query.to_s}) OR ") - query = "#{search_on_fields}:(#{query.to_s}) OR __any:(#{query.to_s}) __type:(#{models.map(&:name).map {|name| "\"#{name.split('::').join(' ')}\"" }.join(' OR ')})" + query = "(#{search_on_fields}:(#{query.to_s}) OR __any:(#{query.to_s})) __type:(#{models.map(&:name).map {|name| "\"#{name.split('::').join(' ')}\"" }.join(' OR ')})" options = { :start => paginate[:per_page] * (paginate[:page] - 1), :len => paginate[:per_page] }.merge(options) if paginate results = index.search(query, options) categories = results['facets'] if results.has_key?('facets') diff --git a/spec/integration_spec.rb b/spec/integration_spec.rb index 78d6fa1..7d0983e 100644 --- a/spec/integration_spec.rb +++ b/spec/integration_spec.rb @@ -21,6 +21,9 @@ t.string :tags t.text :description end + create_table :companies do |t| + t.string :name + end end class Product < ActiveRecord::Base @@ -36,6 +39,15 @@ class Product < ActiveRecord::Base end end +class Company < ActiveRecord::Base + include Tanker + + tankit 'tanker_integration_tests' do + indexes :name + end +end + + describe 'An imaginary store' do before(:all) do @@ -69,6 +81,9 @@ class Product < ActiveRecord::Base @products_in_database = Product.all Product.tanker_reindex + + @apple = Company.create(:name => 'apple') + Company.tanker_reindex end describe 'basic searching' do @@ -108,6 +123,11 @@ class Product < ActiveRecord::Base results.should include(@iphone) results.should have(1).product end + + it "should not find a Company when searching Product" do + results = Product.search_tank("apple") + results.should_not include(@apple) + end end describe 'searching by tag' do diff --git a/spec/tanker_spec.rb b/spec/tanker_spec.rb index ab6ab78..81f0b43 100644 --- a/spec/tanker_spec.rb +++ b/spec/tanker_spec.rb @@ -271,7 +271,7 @@ def self.included(base) it 'should be able to use multi-value query phrases' do Person.tanker_index.should_receive(:search).with( - 'name:(hey! location_id:(1) location_id:(2)) OR last_name:(hey! location_id:(1) location_id:(2)) OR __any:(hey! location_id:(1) location_id:(2)) __type:("Person")', + '(name:(hey! location_id:(1) location_id:(2)) OR last_name:(hey! location_id:(1) location_id:(2)) OR __any:(hey! location_id:(1) location_id:(2))) __type:("Person")', anything ).and_return({'results' => [], 'matches' => 0})