MongoMapper Search

MongoMapper Search is a simple full text search implementation for MongoMapper ODM based on mongoid_search (


In your Gemfile:

gem 'mongomapper_search'


bundle install


class Product
  include MongoMapper::Document
  include MongoMapper::Search
  key :brand, String
  key :name, String

  many :tags
  belongs_to :category

  search_in :brand, :name, {:tags => :name} => 1, {:category => :name} => 2

class Tag
  include MongoMapper::Document
  key :name, String

  belongs_to :product

class Category
  include MongoMapper::Document
  key :name, String

  many :products


search_in :brand, :name => 3, {:tags => :name} => 1

The search will be done using fields named as the symbols passed. You can pass a boost parameter to smooth your search like in:

:name => 3 #It means that keywords found on name is 3 times more important than keywords found on :brand

The default boost is 1. For while, complex attributes like => :name must be declared with a boost value.

Now you can run search, which will look in the search field and return all matching results:"apple iphone").size
=> 1

Note that the search is case insensitive, and accept partial searching too:"ipho").size
=> 1

You can use search in a chainable way:

Product.where(:brand => "Apple").search('iphone')


match: :any - match any occurrence :all - match all ocurrences Default is :any.

search_in :brand, :name, { :tags => :name } => 1, { :match => :any }"apple motorola").size
=> 1

search_in :brand, :name, { :tags => :name } => 1, { :match => :all }"apple motorola").size
=> 0

allowempty_search: _true - match any occurrence false - match all ocurrences Default is false.

search_in :brand, :name, { :tags => :name } => 1, { :allow_empty_search => true }"").size
=> 1


  • Create a ignore list so the search can ignore some words