{"id":13484549,"url":"https://github.com/mongoid/mongoid_search","last_synced_at":"2025-05-15T16:04:12.906Z","repository":{"id":56884409,"uuid":"917044","full_name":"mongoid/mongoid_search","owner":"mongoid","description":"Simple full text search for Mongoid ORM","archived":false,"fork":false,"pushed_at":"2024-02-15T18:43:27.000Z","size":184,"stargazers_count":319,"open_issues_count":19,"forks_count":111,"subscribers_count":16,"default_branch":"master","last_synced_at":"2025-05-12T06:47:28.350Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":"Ruby","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/mongoid.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":"CONTRIBUTING.md","funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2010-09-16T22:49:56.000Z","updated_at":"2025-03-14T05:00:15.000Z","dependencies_parsed_at":"2024-01-05T21:50:10.343Z","dependency_job_id":"1cf11330-358a-4062-9271-aa74fdd20a94","html_url":"https://github.com/mongoid/mongoid_search","commit_stats":{"total_commits":170,"total_committers":42,"mean_commits":"4.0476190476190474","dds":0.6058823529411765,"last_synced_commit":"4a8a528ec8e436ff9c86b8c18948b4785099743c"},"previous_names":["mauriciozaffari/mongoid_search"],"tags_count":15,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mongoid%2Fmongoid_search","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mongoid%2Fmongoid_search/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mongoid%2Fmongoid_search/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mongoid%2Fmongoid_search/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/mongoid","download_url":"https://codeload.github.com/mongoid/mongoid_search/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":254374404,"owners_count":22060609,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-07-31T17:01:26.012Z","updated_at":"2025-05-15T16:04:12.847Z","avatar_url":"https://github.com/mongoid.png","language":"Ruby","readme":"# Mongoid Search\n\nMongoid Search is a simple full text search implementation for Mongoid ORM. It supports Mongoid 3, 4, 5 and 6 and performs well for small data sets. If your searchable model is big (i.e. 1.000.000+ records), [mongoid_fulltext](https://github.com/mongoid/mongoid_fulltext), ElasticSearch, Solr or Sphinx may suit you better.\n\n[![Build Status](https://travis-ci.org/mongoid/mongoid_search.svg?branch=master)](https://travis-ci.org/mongoid/mongoid_search)\n\n## Installation\n\nIn your Gemfile:\n\n```ruby\ngem 'mongoid_search'\n```\n\nThen:\n\n```\nbundle install\n```\n\n## Examples\n\n```ruby\nclass Product\n  include Mongoid::Document\n  include Mongoid::Search\n  field :brand\n  field :name\n  field :unit\n  field :info, type: Hash\n\n  has_many   :tags\n  belongs_to :category\n\n  search_in :brand, :name, tags: :name, category: :name, info: %i[summary description]\n  search_in :unit, index: :_unit_keywords\nend\n\nclass Tag\n  include Mongoid::Document\n  field :name\n\n  belongs_to :product\nend\n\nclass Category\n  include Mongoid::Document\n  field :name\n\n  has_many :products\nend\n```\n\nNow when you save a product, you get a `_keywords` field automatically:\n\n```ruby\np = Product.new brand: 'Apple', name: 'iPhone', unit: 'kilogram', info: { summary: 'Info-summary', description: 'Info-description' }\np.tags \u003c\u003c Tag.new(name: 'Amazing')\np.tags \u003c\u003c Tag.new(name: 'Awesome')\np.tags \u003c\u003c Tag.new(name: 'Superb')\np.save\n# =\u003e true\np._keywords\n# =\u003e [\"amazing\", \"apple\", \"awesome\", \"iphone\", \"superb\", \"Info-summary\", \"Info-description\"]\np._unit_keywords\n# =\u003e [\"kilogram\"]\n```\n\nNow you can run search, which will look in the `_keywords` field and return all matching results:\n\n```ruby\nProduct.full_text_search(\"apple iphone\").size\n# =\u003e 1\n```\n\nYou 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).\n```ruby\nclass ModelWithDynamicFields\n  \n  ...\n  \n  search_in :search_data\n\n  def search_data\n    # concatenate all String fields' values\n    self.attributes.select{|k,v| v.is_a?(String) }.values.join(' ')\n  end\nend\n```\nMongoid_search will run the method before save and use it's output to populate the `_keywords` field.\n\nOf 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:\n\n```ruby\nProduct.full_text_search(\"kilogram\", index: :_unit_keywords).size\n# =\u003e 1\n```\n\nNote that the search is case insensitive, and accept partial searching too:\n\n```ruby\nProduct.full_text_search('ipho').size\n# =\u003e 1\n```\n\nAssuming you have a category with multiple products you can use the following code to search for 'iphone' in products cheaper than $499.\n\n```ruby\ncategory.products.where(:price.lt =\u003e 499).full_text_search('iphone').asc(:price)\n```\n\nTo index or reindex all existing records, run this rake task\n\n```\n$ rake mongoid_search:index\n```\n\n## Options\n\n### match\n\n* `:any` - match any occurrence\n* `:all` - match all occurrences\n\nDefault is `:any`.\n\n```ruby\nProduct.full_text_search('apple motorola', match: :any).size\n# =\u003e 1\n\nProduct.full_text_search('apple motorola', match: :all).size\n# =\u003e 0\n```\n\n### allow\\_empty\\_search\n\n* `true` - will return `Model.all`\n* `false` - will return `[]`\n\nDefault is `false`.\n\n```ruby\nProduct.full_text_search('', allow_empty_search: true).size\n# =\u003e 1\n```\n\n### relevant_search\n\n* `true` - adds relevance information to the results\n* `false` - no relevance information\n\nDefault is `false`.\n\n```ruby\nProduct.full_text_search('amazing apple', relevant_search: true)\n# =\u003e [#\u003cProduct _id: 5016e7d16af54efe1c000001, _type: nil, brand: \"Apple\", name: \"iPhone\", attrs: nil, info: nil, category_id: nil, _keywords: [\"amazing\", \"apple\", \"awesome\", \"iphone\", \"superb\"], relevance: 2.0\u003e]\n```\n\nPlease 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.\n\n### index\n\nDefault is `_keywords`.\n\n```ruby\nProduct.full_text_search('amazing apple', index: :_keywords)\n# =\u003e [#\u003cProduct _id: 5016e7d16af54efe1c000001, _type: nil, brand: \"Apple\", name: \"iPhone\", unit: \"l\", attrs: nil, info: nil, category_id: nil, _keywords: [\"amazing\", \"apple\", \"awesome\", \"iphone\", \"superb\"], _unit_keywords: [\"l\"], relevance: 2.0\u003e]\n\nProduct.full_text_search('kg', index: :_unit_keywords)\n# =\u003e [#\u003cProduct _id: 5016e7d16af54efe1c000001, _type: nil, brand: \"Apple\", name: \"iPhone\", unit: \"kg\", attrs: nil, info: nil, category_id: nil, _keywords: [\"amazing\", \"apple\", \"awesome\", \"iphone\", \"superb\"], _unit_keywords: [\"kg\"], relevance: 2.0\u003e]\n```\n\nindex enables to have two or more different searches, with different or same fields. It should be noted that indexes are exclusive per each one.\n\n## Initializer\n\nAlternatively, you can create an initializer to setup those options:\n\n```ruby\nMongoid::Search.setup do |config|\n  ## Default matching type. Match :any or :all searched keywords\n  config.match = :any\n\n  ## If true, an empty search will return all objects\n  config.allow_empty_search = false\n\n  ## If true, will search with relevance information\n  config.relevant_search = false\n\n  ## Stem keywords\n  config.stem_keywords = false\n\n  ## Add a custom proc returning strings to replace the default stemmer\n  # For example using ruby-stemmer:\n  # config.stem_proc = Proc.new { |word| Lingua.stemmer(word, :language =\u003e 'nl') }\n\n  ## Words to ignore\n  config.ignore_list = []\n\n  ## An array of words\n  # config.ignore_list = %w{ a an to from as }\n\n  ## Or from a file\n  # config.ignore_list = YAML.load(File.open(File.dirname(__FILE__) + '/config/ignorelist.yml'))[\"ignorelist\"]\n\n  ## Search using regex (slower)\n  config.regex_search = true\n\n  ## Regex to search\n\n  ## Match partial words on both sides (slower)\n  config.regex = Proc.new { |query| /#{query}/ }\n\n  ## Match partial words on the beginning or in the end (slightly faster)\n  # config.regex = Proc.new { |query| /^#{query}/ }\n  # config.regex = Proc.new { |query| /#{query}$/ }\n\n  # Ligatures to be replaced\n  # http://en.wikipedia.org/wiki/Typographic_ligature\n  config.ligatures = { \"œ\"=\u003e\"oe\", \"æ\"=\u003e\"ae\" }\n\n  # Strip symbols regex to be replaced. These symbols will be replaced by space\n  config.strip_symbols = /[._:;'\\\"`,?|+={}()!@#%^\u0026*\u003c\u003e~\\$\\-\\\\\\/\\[\\]]/\n\n  # Strip accents regex to be replaced. These sybols will be removed after strip_symbols replacing\n  config.strip_accents = /[^\\s\\p{Alnum}]/\n\n  # Minimum word size. Words smaller than it won't be indexed\n  config.minimum_word_size = 2\nend\n```\n","funding_links":[],"categories":["Search"],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmongoid%2Fmongoid_search","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmongoid%2Fmongoid_search","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmongoid%2Fmongoid_search/lists"}