Skip to content

Instantly share code, notes, and snippets.

@royw
Created October 19, 2009 18:26
Show Gist options
  • Save royw/213584 to your computer and use it in GitHub Desktop.
Save royw/213584 to your computer and use it in GitHub Desktop.
require 'mongo_mapper'
require 'spec'
MongoMapper.connection = Mongo::Connection.new('localhost')
MongoMapper.database = 'tmp'
class Book
include MongoMapper::Document
key :title, String
key :_keywords, Array, :index => true
before_save do |record|
record._keywords = record.title.split(' ').uniq
end
end
describe "testing text search" do
before :each do
Book.collection.clear
book = Book.create :title => 'Something Wicked This Way Comes'
book = Book.create :title => 'The Dragonriders of Pern'
book = Book.create :title => "Starpilot's Grave"
book = Book.create :title => "The Man Who Sold The Moon"
book = Book.create :title => "The Man With The Golden Gun"
puts "\n:_keywords => " + book._keywords.inspect
end
it "should allow combining search results" do
a = Book.all(:conditions => {:_keywords => ['Man', 'Grave']})
b = Book.all(:conditions => {:_keywords => 'The'})
a.size.should == 3
b.size.should == 3
c = (a + b).uniq
c.size.should == 4 # fails with result 6
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment