Skip to content

Commit

Permalink
Merge pull request kidpollo#41 from slothbear/multimodel
Browse files Browse the repository at this point in the history
Searching one model can return other models
  • Loading branch information
kidpollo committed Aug 17, 2011
2 parents 8be83b5 + fe404a0 commit bb73b05
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 2 deletions.
2 changes: 1 addition & 1 deletion lib/tanker.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down
20 changes: 20 additions & 0 deletions spec/integration_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion spec/tanker_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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})

Expand Down

0 comments on commit bb73b05

Please sign in to comment.