[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:

Installation

You can install it as a gem:

1
$ gem install elasticsearch-rails

or add it into a Gemfile (Bundler):

1
2
3
4
5
# Gemfile

# elastic/elasticsearch-rails: Elasticsearch integrations for ActiveModel/Record and Ruby on Rails
# https://github.com/elastic/elasticsearch-rails
gem 'elasticsearch-rails', '7.2.0'

Then, run bundle install.

1
$ bundle install

Usages

Example of a basic integration into an ActiveRecord-based model:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
require 'elasticsearch/model'

class Article < ActiveRecord::Base
include Elasticsearch::Model
include Elasticsearch::Model::Callbacks
end

# Index creation right at import time is not encouraged.
# Typically, you would call create_index! asynchronously (e.g. in a cron job)
# However, we are adding it here so that this usage example can run correctly.
Article.__elasticsearch__.create_index!
Article.import

@articles = Article.search('foobar').records

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
2
3
4
5
6
7
8
$ docker run \
--name elasticsearch-rails-searchapp \
--publish 9200:9200 \
--env "discovery.type=single-node" \
--env "cluster.name=elasticsearch-rails" \
--env "cluster.routing.allocation.disk.threshold_enabled=false" \
--rm \
docker.elastic.co/elasticsearch/elasticsearch-oss:7.6.0

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
2
3
4
5
6
7
8
9
10
class Article
attr_accessor :title
end

require 'elasticsearch/persistence'
repository = Elasticsearch::Persistence::Repository.new

repository.save Article.new(title: 'Test')
# POST http://localhost:9200/repository/article
# => {"_index"=>"repository", "_type"=>"article", "_id"=>"Ak75E0U9Q96T5Y999_39NA", ...}

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.

References

[1] elastic/elasticsearch-rails: Elasticsearch integrations for ActiveModel/Record and Ruby on Rails - https://github.com/elastic/elasticsearch-rails

[2] elasticsearch-rails | RubyGems.org | your community gem host - https://rubygems.org/gems/elasticsearch-rails

[3] elasticsearch-rails/elasticsearch-model at master · elastic/elasticsearch-rails - https://github.com/elastic/elasticsearch-rails/tree/master/elasticsearch-model

[4] elasticsearch-rails/elasticsearch-persistence at master · elastic/elasticsearch-rails - https://github.com/elastic/elasticsearch-rails/tree/master/elasticsearch-persistence

[5] Ruby and Rails Integrations [7.x] | Elastic - https://www.elastic.co/guide/en/elasticsearch/client/ruby-api/current/index.html

[6] Free and Open Search: The Creators of Elasticsearch, ELK & Kibana | Elastic - https://www.elastic.co/