Created
October 4, 2009 22:07
-
-
Save royw/201659 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
royw-macbook:tmp royw$ cat search_indexing.rb | |
require 'mongomapper' | |
require 'spec' | |
MongoMapper.connection = XGen::Mongo::Driver::Connection.new('localhost') | |
MongoMapper.database = 'tmp' | |
class Person | |
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 | |
Person.collection.clear | |
person = Person.create :title => 'Something Wicked This Way Comes' | |
puts "\n:_keywords => " + person._keywords.inspect | |
end | |
it "should be searchable by whole words" do | |
result = Person.first(:conditions => {:_keywords => 'Wicked'}) | |
puts "\nresult => " + result.inspect | |
result.should_not be_nil | |
Person.first(:conditions => {:_keywords => 'Foobar'}).should be_nil | |
end | |
it "should be searchable by array of words" do | |
result = Person.first(:conditions => {:_keywords => ['Foobar', 'Wicked']}) | |
puts "\nresult => " + result.inspect | |
result.should_not be_nil | |
Person.first(:conditions => {:_keywords => ['Foo', 'Bar']}).should be_nil | |
end | |
it "should be searchable by regex" do | |
result = Person.first(:conditions => {:_keywords => /wick/i}) | |
puts "\nresult => " + result.inspect | |
result.should_not be_nil | |
Person.first(:conditions => {:_keywords => /foo/i}).should be_nil | |
end | |
end | |
royw-macbook:tmp royw$ spec search_indexing.rb | |
:_keywords => ["Something", "Wicked", "This", "Way", "Comes"] | |
result => #<Person _keywords: SomethingWickedThisWayComes, title: Something Wicked This Way Comes, _id: 4ac91c5e6eb0140fbd000001> | |
. | |
:_keywords => ["Something", "Wicked", "This", "Way", "Comes"] | |
result => #<Person _keywords: SomethingWickedThisWayComes, title: Something Wicked This Way Comes, _id: 4ac91c5e6eb0140fbd000002> | |
. | |
:_keywords => ["Something", "Wicked", "This", "Way", "Comes"] | |
result => #<Person _keywords: SomethingWickedThisWayComes, title: Something Wicked This Way Comes, _id: 4ac91c5e6eb0140fbd000003> | |
. | |
Finished in 0.029 seconds | |
3 examples, 0 failures |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment