Find file
Fetching contributors…
Cannot retrieve contributors at this time
122 lines (80 sloc) 2.65 KB

Mongoid Search

Mongoid Search is a simple full text search implementation for Mongoid ORM.


In your Gemfile:

gem 'mongoid_search'


bundle install


class Product
  include Mongoid::Document
  include Mongoid::Search
  field :brand
  field :name

  references_many :tags
  refereced_in    :category

  search_in :brand, :name, :tags => :name, :category => :name

class Tag
  include Mongoid::Document
  field :name

  referenced_in :product

class Category
  include Mongoid::Document
  field :name

  references_many :products

Now when you save a product, you get a _keywords field automatically:

p = :brand => "Apple", :name => "iPhone"
p.tags << => "Amazing")
p.tags << => "Awesome")
p.tags << => "Superb")
=> true

Now you can run search, which will look in the _keywords 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

Assuming you have a category with multiple products you can now use the following code to search for 'iphone' in products cheaper than $499

@category.products.where( => 499).csearch('iphone').asc(:price)

In this case we have to use csearch, an alias for search, because since v2.0.0 Mongoid defines it's own method.


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

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

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

allow_empty_search: true - match any occurrence false - match all ocurrences Default is false.

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

ignore_list: Pass in an ignore list location. Keywords in that list will be ignored.

search_in :brand, :name, { :tags => :name }, { :ignore_list => Rails.root.join("config", "ignorelist.yml") }

The list should look like:

  a, an, to, from, as

You can include how many keywords you like.


  • Strip html with sanitize (
  • Rewrite and test relevant search
  • Move all configurations to a configuration file. Maybe /config/mongoid_search.yml.