Skip to content
This repository has been archived by the owner on Jun 30, 2020. It is now read-only.

Commit

Permalink
Added example for indexing one field and searching.
Browse files Browse the repository at this point in the history
  • Loading branch information
jnunemaker committed Dec 20, 2010
1 parent 5d949b8 commit 1b73501
Showing 1 changed file with 41 additions and 0 deletions.
41 changes: 41 additions & 0 deletions examples/single.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
$LOAD_PATH.unshift(File.expand_path('../../lib', __FILE__))
require 'pp'
require 'hunt'

MongoMapper.database = 'testing'

class Note
include MongoMapper::Document
plugin Hunt

key :title, String

# Declare which fields to search on
searches :title
end
Note.delete_all # clean the slate

# Note that the search terms are stored in a searches hash in the key default.
# This is so that future versions can allow different combinations of search
# fields and use different keys in searches.

# Also, meaningless words are stripped out and all words less than 3 characters
# long. The words are then stemmed (http://en.wikipedia.org/wiki/Stemming) so
# exact matches when searching are not necessary.

note = Note.create(:title => 'This is a test')
pp note
# #<Note searches: {"default"=>["test"]}, title: "This is a test", _id: BSON::ObjectId('...')>

note.update_attributes(:title => 'A different test')
pp note
# #<Note searches: {"default"=>["differ", "test"]}, title: "A different test", _id: BSON::ObjectId('...')>

# Also, you get a handy
pp Note.search('test').count # 1
pp Note.search('testing').count # 1
pp Note.search('testing').all
# [#<Note searches: {"default"=>["differ", "test"]}, title: "A different test", _id: BSON::ObjectId('...')>]

pp Note.search('test').paginate(:page => 1)
# [#<Note searches: {"default"=>["differ", "test"]}, title: "A different test", _id: BSON::ObjectId('...')>]

0 comments on commit 1b73501

Please sign in to comment.