[Awesome Ruby Gem] Use elasticsearch gems to provide Elasticsearch integrations for ActiveModel/Record and Ruby on Rails
Elasticsearch
The elasticsearch gems provides a low-level client for communicating with an Elasticsearch cluster, fully compatible with other official clients.
This repository contains various Ruby and Rails integrations for Elasticsearch:
-
ActiveModel integration with adapters for ActiveRecord and Mongoid
-
Repository pattern based persistence layer for Ruby objects
-
Enumerable-based wrapper for search results
-
ActiveRecord::Relation-based wrapper for returning search results as records
-
Convenience model methods such as search, mapping, import, etc
-
Rake tasks for importing the data
-
Support for Kaminari and WillPaginate pagination
-
Integration with Rails’ instrumentation framework
-
Templates for generating example Rails application
-
Elasticsearch client and Ruby API is provided by the elasticsearch-ruby project.
This project is split into three separate gems:
-
elasticsearch-model
elasticsearch-rails/README.md at master · elastic/elasticsearch-rails - https://github.com/elastic/elasticsearch-rails/blob/master/elasticsearch-model/README.md, which contains search integration for Ruby/Rails models such as ActiveRecord::Base and Mongoid, -
elasticsearch-persistence
elasticsearch-rails/elasticsearch-persistence at master · elastic/elasticsearch-rails - https://github.com/elastic/elasticsearch-rails/tree/master/elasticsearch-persistence, which provides a standalone persistence layer for Ruby/Rails objects and models -
elasticsearch-rails
elasticsearch-rails/elasticsearch-model at master · elastic/elasticsearch-rails - https://github.com/elastic/elasticsearch-rails/tree/master/elasticsearch-model, which contains various features for Ruby on Rails applications
Installation
You can install it as a gem:
1 | gem install elasticsearch-rails |
or add it into a Gemfile (Bundler):
1 | # Gemfile |
Then, run bundle install
.
1 | bundle install |
Usages
Example of a basic integration into an ActiveRecord-based model:
1 | require 'elasticsearch/model' |
You can generate a simple Ruby on Rails application with a single command (see the other available templates). You’ll need to have an Elasticsearch cluster running on your system before generating the app. The easiest way of getting this set up is by running it with Docker with this command:
1 | docker run \ |
Once Elasticsearch is running, you can generate the simple app with this command:
1 | rails new searchapp --skip --skip-bundle --template https://raw.github.com/elasticsearch/elasticsearch-rails/master/elasticsearch-rails/lib/rails/templates/01-basic.rb |
Example of using Elasticsearch as a repository for a Ruby domain object:
1 | class Article |
Please refer to documentation Ruby and Rails Integrations [7.x] | Elastic - https://www.elastic.co/guide/en/elasticsearch/client/ruby-api/current/index.html for detailed information and examples.