Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
branch: master
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 38 lines (28 sloc) 1.362 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
$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
  key :tags, Array

  # Declare which fields to search on.
  # No problem declaring an array key.
  searches :title, :tags
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.

pp Note.create(:title => 'I am lost without Mongo!', :tags => %w(mongo database nosql))
#<Note searches: {"default"=>["test", "mongo", "databas", "nosql"]}, title: "This is a test", _id: BSON::ObjectId('...'), tags: ["mongo", "database", "nosql"]>

pp Note.create(:title => 'Lost is a tv show', :tags => %w(lost tv))
#<Note searches: {"default"=>["lost", "tv", "show"]}, title: "Lost is a tv show", _id: BSON::ObjectId('...'), tags: ["lost", "tv"]>

pp Note.search('lost').count # 2
pp Note.search('tv').count # 1

# Multiple words work
pp Note.search('mongo tv').count # 2
Something went wrong with that request. Please try again.