Baike.dev
All toolsOnline toolsAI codingTrendingOpen sourceNewsSubmit
Log in
< Back to tools
M

mongoid_search

> 编程语言
Open source

Simple full text search for Mongoid ORM

315 stars0 likes1 views
WebsiteGitHub

About

Simple full text search for Mongoid ORM

Mongoid Search

Mongoid Search is a simple full text search implementation for Mongoid ORM. It supports Mongoid 6, 7, and 8 and performs well for small data sets. If your searchable model is big (i.e. 1.000.000+ records), mongoid_fulltext, ElasticSearch, Solr or Sphinx may suit you better.

Installation

In your Gemfile:

ruby
gem 'mongoid_search'

Then:

bundle install

Examples

ruby
class Product
  include Mongoid::Document
  include Mongoid::Search
  field :brand
  field :name
  field :unit
  field :info, type: Hash

  has_many   :tags
  belongs_to :category

  search_in :brand, :name, tags: :name, category: :name, info: %i[summary description]
  search_in :unit, index: :_unit_keywords
end

class Tag
  include Mongoid::Document
  field :name

  belongs_to :product
end

class Category
  include Mongoid::Document
  field :name

  has_many :products
end

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

ruby
p = Product.new brand: 'Apple', name: 'iPhone', unit: 'kilogram', info: { summary: 'Info-summary', description: 'Info-description' }
p.tags  true
p._keywords
# => ["amazing", "apple", "awesome", "iphone", "superb", "Info-summary", "Info-description"]
p._unit_keywords
# => ["kilogram"]

Now you can run search, which will look in the _keywords field and return all matching results:

ruby
Product.full_text_search("apple iphone").size
# => 1

You can also search in "virtual" fields by defining them as methods. This can be useful when you have a method with dynamic fields (i.e. variable schema).

ruby
class ModelWithDynamicFields
  
  ...
  
  search_in :search_data

  def search_data
    # concatenate all String fields' values
    self.attributes.select{|k,v| v.is_a?(String) }.values.join(' ')
  end
end

Mongoid_search will run the method before save and use it's output to populate the _keywords field.

Of course, some models could have more than one index. For instance, two different searches with different fields, so you could even specify from which index should be searched:

ruby
Product.full_text_search("kilogram", index: :_unit_keywords).size
# => 1

Note that the search is case insensitive, and accept partial searching too:

ruby
Product.full_text_search('ipho').size
# => 1

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

ruby
category.products.where(:price.lt => 499).full_text_search('iphone').asc(:price)

To index or reindex all existing records, run this rake task

$ rake mongoid_search:index

Options

match

  • :any - match any occurrence
  • :all - match all occurrences

Default is :any.

ruby
Product.full_text_search('apple motorola', match: :any).size
# => 1

Product.full_text_search('apple motorola', match: :all).size
# => 0

allow_empty_search

  • true - will return Model.all
  • false - will return []

Default is false.

ruby
Product.full_text_search('', allow_empty_search: true).size
# => 1

relevant_search

  • true - adds relevance information to the results
  • false - no relevance information

Default is false.

ruby
Product.full_text_search('amazing apple', relevant_search: true)
# => [#]

Please note that relevant_search will return an Array and not a Criteria object. The search method should always be called in the end of the method chain.

index

Default is _keywords.

ruby
Product.full_text_search('amazing apple', index: :_keywords)
# => [#]

Product.full_text_search('kg', index: :_unit_keywords)
# => [#]

index enables to have two or more different searches, with different or same fields. It should be noted that indexes are exclusive per each one.

Initializer

Alternatively, you can create an initializer to setup those options:

…

Issues· 19 open

View all issuesOpen on GitHub

No open issues yet, or sync has not completed.

> Tags

Ruby

No comments yet. Be the first to share.

> Details

PublishedAug 1, 2026
UpdatedSep 18, 2026
Category编程语言
PricingOpen source

> Related tools

T
TypeScript
JavaScript 的超集,为前端与全栈提供静态类型
P
Python
通用编程语言,广泛用于 Web、数据与 AI
G
Go
Google 推出的简洁高效系统语言