Skip to content

Commit

Permalink
Searching across multiple fields correctly. Cleaned up comments. Read…
Browse files Browse the repository at this point in the history
…y for merge.
  • Loading branch information
coryschires committed Jun 24, 2011
1 parent d3d85d0 commit d246e35
Show file tree
Hide file tree
Showing 4 changed files with 5 additions and 32 deletions.
1 change: 0 additions & 1 deletion .rvmrc

This file was deleted.

1 change: 0 additions & 1 deletion Gemfile
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
source "http://rubygems.org"

gem 'json', '>= 1.5.1'
gem 'awesome_print'

group :test do
gem 'rspec', '>= 2.5.0'
Expand Down
27 changes: 1 addition & 26 deletions lib/tanker.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
begin
require "rubygems"
require "bundler"
require "awesome_print"

Bundler.setup :default
rescue => e
Expand Down Expand Up @@ -105,33 +104,9 @@ def search(models, query, options = {})
options[:snippet] = snippets.join(',') if snippets

search_on_fields = models.map{|model| model.tanker_config.indexes.map{|arr| arr[0]}.uniq}.flatten.uniq.join(":(#{query.to_s}) OR ")

# original
# query = "#{search_on_fields}:(#{query.to_s}) __type:(#{models.map(&:name).map {|name| "\"#{name.split('::').join(' ')}\"" }.join(' OR ')})"
# => "name:(hey! location_id:(1) location_id:(2)) OR last_name:(hey! location_id:(1) location_id:(2)) __type:(\"Person\")"


# fix
query = "#{search_on_fields}:(#{query.to_s}) OR __any:(#{query.to_s}) __type:(#{models.map(&:name).map {|name| "\"#{name.split('::').join(' ')}\"" }.join(' OR ')})"
# => "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)) __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\")"




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

puts "!A"*88
ap query
puts
puts
puts
ap options
puts "!B"*88

results = index.search(query, options)

instantiated_results = if (fetch || snippets)
Expand Down
8 changes: 4 additions & 4 deletions spec/integration_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ class Product < ActiveRecord::Base
(@results - [@catapult, @tnt, @cat]).should be_empty
@results[0].id.should_not be_nil
end

it 'should find all catapults' do
@results = Product.search_tank('catapult')
(@results - [@catapult]).should be_empty
Expand All @@ -105,19 +105,19 @@ class Product < ActiveRecord::Base
(@results - [@catapult, @cat]).should be_empty
end
end

describe 'advanced searching' do
it 'should search multiple words from the same field' do
@results = Product.search_tank('Acme catapult')
@results.should include(@catapult)
end

it "should search across multiple fields" do
@results = Product.search_tank('catapult google')
@results.should include(@catapult)
end
end

describe 'filtering dogs' do

before(:all) do
Expand Down

0 comments on commit d246e35

Please sign in to comment.