{"id":19474589,"url":"https://github.com/tomasc/elasticsearch-model-mongoid_extensions","last_synced_at":"2026-06-06T23:31:42.072Z","repository":{"id":56844516,"uuid":"68219764","full_name":"tomasc/elasticsearch-model-mongoid_extensions","owner":"tomasc","description":"Elasticsearch::Model extensions for Mongoid adding support of single collection inheritance (by the way of multiple indexes), localized fields and mapped fields extraction.","archived":false,"fork":false,"pushed_at":"2018-10-09T13:56:55.000Z","size":49,"stargazers_count":0,"open_issues_count":1,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-10-22T12:00:02.769Z","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/tomasc.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2016-09-14T15:45:45.000Z","updated_at":"2018-10-09T13:56:25.000Z","dependencies_parsed_at":"2022-09-09T02:22:58.161Z","dependency_job_id":null,"html_url":"https://github.com/tomasc/elasticsearch-model-mongoid_extensions","commit_stats":null,"previous_names":[],"tags_count":10,"template":false,"template_full_name":null,"purl":"pkg:github/tomasc/elasticsearch-model-mongoid_extensions","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tomasc%2Felasticsearch-model-mongoid_extensions","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tomasc%2Felasticsearch-model-mongoid_extensions/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tomasc%2Felasticsearch-model-mongoid_extensions/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tomasc%2Felasticsearch-model-mongoid_extensions/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/tomasc","download_url":"https://codeload.github.com/tomasc/elasticsearch-model-mongoid_extensions/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tomasc%2Felasticsearch-model-mongoid_extensions/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":285152380,"owners_count":27123462,"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","status":"online","status_checked_at":"2025-11-18T02:00:05.759Z","response_time":61,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"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-11-10T19:25:45.074Z","updated_at":"2025-11-18T23:05:00.566Z","avatar_url":"https://github.com/tomasc.png","language":"Ruby","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Elasticsearch::Model::MongoidExtensions\n\n[![Build Status](https://travis-ci.org/tomasc/elasticsearch-model-mongoid_extensions.svg)](https://travis-ci.org/tomasc/elasticsearch-model-mongoid_extensions) [![Gem Version](https://badge.fury.io/rb/elasticsearch-model-mongoid_extensions.svg)](http://badge.fury.io/rb/elasticsearch-model-mongoid_extensions) [![Coverage Status](https://img.shields.io/coveralls/tomasc/elasticsearch-model-mongoid_extensions.svg)](https://coveralls.io/r/tomasc/elasticsearch-model-mongoid_extensions)\n\n[Elasticsearch::Model](https://github.com/elastic/elasticsearch-rails/tree/master/elasticsearch-model) extensions for Mongoid adding support of [single collection inheritance](#sci) (by the way of multiple indexes), [localized fields](#localized) and [mapped fields extraction](#fields).\n\n## Installation\n\nAdd this line to your application's Gemfile:\n\n```ruby\ngem 'elasticsearch-model-mongoid_extensions'\n```\n\nAnd then execute:\n\n    $ bundle\n\nOr install it yourself as:\n\n    $ gem install elasticsearch-model-mongoid_extensions\n\n## Usage\n\n### SCI\n\nUsing a separate index per each subclass is beneficial in case the field definitions vary across the subclasses (as it may in document oriented databases such as Mongoid). On the contrary sharing an index for all subclasses might lead into conflicts with different mappings of fields with same name.\n\nIf your subclass tree shares same field definitions, you might prefer use only one index (see the `inheritance_enabled` setting on [`ElasticSearch::Model`](https://github.com/elastic/elasticsearch-rails/tree/master/elasticsearch-model#settings)).\n\n#### Setup\n\n```ruby\nclass MyDoc\n  include Mongoid::Document\n  include Elasticsearch::Model::MongoidExtensions::SCI\n\n  field :field_1, type: String\n\n  mapping do\n    indexes :field_1\n  end\nend\n\nclass MyDoc1 \u003c MyDoc\n  field :field_2, type: String\n\n  mapping do\n    indexes :field_2\n  end\nend\n```\n\nThe `MyDoc` class will use index with name `my_docs`, the `MyDoc1` subclass will use `my_doc_1s`. If you wish to customize the index name (prepend application name, append Rails environment name etc.) supply an `index_name_template` that will be automatically evaluated in context of each of the subclasses. Similarly a template can be used for document type.\n\n```ruby\nclass MyDoc\n  # …\n  index_name_template -\u003e (cls) { ['elasticsearch-model-mongoid_extensions', cls.model_name.plural].join('-') }\n  document_type_template -\u003e (cls) { cls.model_name.singular }\n  # …\nend\n```\n\n#### Index creation \u0026 refresh\n\nUse the class methods defined on the base class to create/refresh indexes for all descending classes as well:\n\n```ruby\nMyDoc.create_index! # will trigger MyDoc1.create_index! as well\nMyDoc.refresh_index! # will trigger MyDoc1.refresh_index! as well\n```\n\n#### Importing\n\nImport on base class (here `MyDoc`) imports all documents of descending classes as well:\n\n```ruby\nMyDoc.import # will trigger MyDoc1.import as well\n```\n\n#### Indexing\n\nIndexing works as expected using the standard proxied methods:\n\n```ruby\nmy_doc.__elasticsearch__.index_document\nmy_doc.__elasticsearch__.update_document\nmy_doc.__elasticsearch__.delete_document\n```\n\n#### Search\n\nSearch on base class searches descendants as well:\n\n```ruby\nMyDoc.search('*') # will search MyDoc1 as well\n```\n\nUse the `type` option to limit the searched classes:\n\n```ruby\nMyDoc.search('*', type: [MyDoc.document_type]) # will search only MyDoc\n```\n\n### Localized\n\nBy including the `Localized` module, all Mongoid localized fields will be automatically mapped and serialized as objects:\n\n```ruby\nclass MyDoc3\n  # …\n  include Elasticsearch::Model::MongoidExtensions::Localized\n\n  field :field_3, type: String, localize: true\n\n  mapping do\n    indexes :field_3\n  end\n  # …\nend\n```\n\n#### Mapping\n\nThe mapping will be altered, so that fields that would be originally mapped as:\n\n```\n{ 'field_1' =\u003e { 'type' =\u003e 'string' } }\n```\n\nAre automatically transformed to:\n\n```\n{ 'field_1' =\u003e { 'type' =\u003e 'object', 'properties' =\u003e { 'en' =\u003e { 'type' =\u003e 'string' } } } }\n```\n\nThis happens for all `I18n.available_locales`.\n\n#### Serializing\n\nThe `:as_indexed_json` is automatically transformed behind the scenes to correspond with the mapping. Therefore it can be specified as usual:\n\n```ruby\ndef as_indexed_json(options = {})\n  super(only: %i(field_1))\nend\n```\n\nThe result automatically becoming:\n\n```\n{ 'field_1' =\u003e { 'en' =\u003e 'value_en', 'cs' =\u003e 'value_cs' } }\n```\n\nTODO: it might be helpful to add support for the I18n's fallbacks, so that missing value is correctly replaced by a fallback.\n\n### Fields\n\nThe gem add `to_fields` method on `mappings` that returns all fields names as they are mapped in Elasticsearch. This is especially useful in combination with the `Localized` mixin as it allows to select only fields for a particular locale and use those when searching.\n\nHaving document with the following mapping:\n\n```ruby\nclass MyDocFields\n  # …\n  mapping do\n    indexes :field_1\n    indexes :field_2 do\n      indexes :number, type: :integer, index: :not_analyzed\n      indexes :string, type: :string, index: :not_analyzed\n    end\n  end\n  # …\nend\n```\n\nThe `#to_fields` returns:\n\n```ruby\nMyDocFields.mapping.to_fields # =\u003e [\"field_1\", \"field_2\", \"field_2.number\", \"field_2.string\"]\n```\n\n## Development\n\nAfter checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.\n\nTo install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).\n\n## Contributing\n\nBug reports and pull requests are welcome on GitHub at https://github.com/tomasc/elasticsearch-model-mongoid_extensions.\n\n\n## License\n\nThe gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftomasc%2Felasticsearch-model-mongoid_extensions","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ftomasc%2Felasticsearch-model-mongoid_extensions","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftomasc%2Felasticsearch-model-mongoid_extensions/lists"}