Skip to content


Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Added multiple field search example.

  • Loading branch information...
commit 90508fe0d59cc1d4d26f0ae13b0e2ae57af9cf48 1 parent 6d52e11
@jnunemaker authored
Showing with 38 additions and 0 deletions.
  1. +38 −0 examples/multiple.rb
38 examples/multiple.rb
@@ -0,0 +1,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
+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 ( 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'lost').count # 2
+pp'tv').count # 1
+# Multiple words work
+pp'mongo tv').count # 2
Please sign in to comment.
Something went wrong with that request. Please try again.