{"id":13879466,"url":"https://github.com/jrgns/sequel-elasticsearch","last_synced_at":"2025-07-16T15:32:28.266Z","repository":{"id":56895001,"uuid":"117751428","full_name":"jrgns/sequel-elasticsearch","owner":"jrgns","description":"A plugin for the Sequel gem to sync your Models to Elasticsearch","archived":false,"fork":false,"pushed_at":"2020-12-03T12:09:35.000Z","size":94,"stargazers_count":19,"open_issues_count":0,"forks_count":7,"subscribers_count":1,"default_branch":"master","last_synced_at":"2024-11-15T12:14:32.609Z","etag":null,"topics":["elasticsearch","ruby","sequel"],"latest_commit_sha":null,"homepage":"https://jrgns.github.io/sequel-elasticsearch","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/jrgns.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE.txt","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2018-01-16T22:39:18.000Z","updated_at":"2023-09-21T08:21:22.000Z","dependencies_parsed_at":"2022-08-21T01:20:22.438Z","dependency_job_id":null,"html_url":"https://github.com/jrgns/sequel-elasticsearch","commit_stats":null,"previous_names":[],"tags_count":19,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jrgns%2Fsequel-elasticsearch","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jrgns%2Fsequel-elasticsearch/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jrgns%2Fsequel-elasticsearch/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jrgns%2Fsequel-elasticsearch/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/jrgns","download_url":"https://codeload.github.com/jrgns/sequel-elasticsearch/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":226143895,"owners_count":17580245,"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":["elasticsearch","ruby","sequel"],"created_at":"2024-08-06T08:02:21.980Z","updated_at":"2024-11-24T08:31:23.831Z","avatar_url":"https://github.com/jrgns.png","language":"Ruby","readme":"# Sequel::Elasticsearch\n\nSequel::Elasticsearch allows you to transparently mirror your database, or specific tables, to Elasticsearch. It's especially useful if you want the power of search through Elasticsearch, but keep the sanity and structure of a relational database.\n\n[![Build Status](https://travis-ci.org/jrgns/sequel-elasticsearch.svg?branch=master)](https://travis-ci.org/jrgns/sequel-elasticsearch)\n[![Maintainability](https://api.codeclimate.com/v1/badges/ff453fe81303a2fa7c02/maintainability)](https://codeclimate.com/github/jrgns/sequel-elasticsearch/maintainability)\n[![Test Coverage](https://api.codeclimate.com/v1/badges/ff453fe81303a2fa7c02/test_coverage)](https://codeclimate.com/github/jrgns/sequel-elasticsearch/test_coverage)\n\n## Installation\n\nAdd this line to your application's Gemfile:\n\n```ruby\ngem 'sequel-elasticsearch'\n```\n\nAnd then execute:\n\n    $ bundle\n\nOr install it yourself as:\n\n    $ gem install sequel-elasticsearch\n\n## Usage\n\nRequire the gem with:\n\n```ruby\nrequire 'sequel/plugins/elasticsearch'\n```\n\nYou'll need an Elasticsearch cluster to sync your data to. By default the gem will try to connect to `http://localhost:9200`. Set the `ELASTICSEARCH_URL` ENV variable to the URL of your cluster.\n\nThis is a Sequel plugin, so you can enable it DB wide:\n\n```ruby\nSequel::Model.plugin :elasticsearch\n\n```\n\nOr per model:\n\n```ruby\nDocument.plugin Sequel::Elasticsearch\n\n# or\n\nclass Document \u003c Sequel::Model\n  plugin :elasticsearch\nend\n```\n\nThere's a couple of options you can set:\n\n```ruby\nSequel::Model.plugin :elasticsearch,\n  elasticsearch: { log: true }, # Options to pass the the Elasticsearch ruby client\n  index: 'all-my-data', # The index in which the data should be stored. Defaults to the table name associated with the model\n  type: 'is-mine' # The type in which the data should be stored.\n```\n\nAnd that's it! Just transact as you normally would, and your records will be created and updated in the Elasticsearch cluster.\n\n### Indexing\n\nEnsure that you create the [index mappings](https://www.elastic.co/guide/en/elasticsearch/reference/current/mapping.html) for your data before using this plugin, otherwise you might get some weird results.\n\nThe records will by default be indexed using the `values` call of the model. Should you need to customize what's indexed, you can define a `indexed_values` method (or `as_indexed_json` method if you prefer the Rails way).\n\n### Searching\n\nYour model is now searchable through Elasticsearch. Just pass down a string that's parsable as a [query string query](https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-query-string-query.html).\n\n```ruby\nDocument.es('title:Sequel')\nDocument.es('title:Sequel AND body:Elasticsearch')\n```\n\nThe result from the `es` method is an enumerable containing `Sequel::Model` instances of your model:\n\n```ruby\nresults = Document.es('title:Sequel')\nresults.each { |e| p e }\n# Outputs\n# #\u003cDocument @values={:id=\u003e1, :title=\u003e\"Sequel\", :body=\u003e\"Document 1\"}\u003e\n# #\u003cDocument @values={:id=\u003e2, :title=\u003e\"Sequel\", :body=\u003e\"Document 2\"}\u003e\n```\n\nThe result also contains the meta info about the Elasticsearch query result:\n\n```ruby\nresults = Document.es('title:Sequel')\np results.count # The number of documents included in this result\np results.total # The total number of documents in the index that matches the search\np results.timed_out # If the search timed out or not\np results.took # How long, in miliseconds the search took\n```\n\nYou can also use the scroll API to search and fetch large datasets:\n\n```ruby\n# Get a dataset that will stay consistent for 5 minutes and extend that time with 1 minute on every iteration\nscroll = Document.es('test', scroll: '5m')\np scroll_id # Outputs the scroll_id for this specific scrolling snapshot\nputs \"Found #{scroll.count} of #{scroll.total} documents\"\nscroll.each { |e| p e }\nwhile (scroll = Document.es(scroll, scroll: '1m')) \u0026\u0026 scroll.empty? == false do\n  puts \"Found #{scroll.count} of #{scroll.total} documents\"\n  scroll.each { |e| p e }\nend\n```\n\n### Import\n\nYou can import the whole dataset, or specify a dataset to be imported. This will create a new, timestamped index for your dataset, and import all the records from that dataset into the index. An alias will be created (or updated) to point to the newly created index.\n\n```ruby\nDocument.import! # Import all the Document records. Use the default settings.\n\nDocument.import!(dataset: Document.where(active: true)) # Import all the active Document records\n\nDocument.import!(\n    index: 'active-documents', # Use the active-documents index\n    dataset: Document.where(active: true), # Only index active documents\n    batch_size: 20 # Send documents to Elasticsearch in batches of 20 records\n)\n```\n\n## Development\n\nAfter checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` 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/jrgns/sequel-elasticsearch.\n\nFeatures that needs to be built:\n\n- [x] An `es` method to search through the data on the cluster.\n- [x] Let `es` return an enumerator of `Sequel::Model` instances.\n- [ ] A rake task to create or suggest mappings for a table.\n\n## License\n\nThe gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).\n\n","funding_links":[],"categories":["Ruby"],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjrgns%2Fsequel-elasticsearch","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fjrgns%2Fsequel-elasticsearch","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjrgns%2Fsequel-elasticsearch/lists"}