The elasticsearch-rails library is a companion for the the elasticsearch-model library, providing features suitable for Ruby on Rails applications.


This library is compatible with Ruby 3.1 and higher.

The version numbers follow the Elasticsearch major versions. Currently the main branch is compatible with version 8.x of the Elasticsearch stack.

Rubygem Elasticsearch
0.1 1.x
2.x 2.x
5.x 5.x
6.x 6.x
7.x 7.x
8.x 8.x
main 8.x


Install the package from Rubygems:

gem install elasticsearch-rails

To use an unreleased version, either add it to your Gemfile for Bundler:

gem 'elasticsearch-rails', git: 'git://github.com/elastic/elasticsearch-rails.git', branch: '5.x'

or install it from a source code checkout:

git clone https://github.com/elastic/elasticsearch-rails.git
cd elasticsearch-rails/elasticsearch-rails
bundle install
rake install


Rake Tasks

To facilitate importing data from your models into Elasticsearch, require the task definition in your application, eg. in the lib/tasks/elasticsearch.rake file:

require 'elasticsearch/rails/tasks/import'

To import the records from your Article model, run:

$ bundle exec rake environment elasticsearch:import:model CLASS='Article'

To limit the imported records to a certain ActiveRecord scope, pass it to the task:

$ bundle exec rake environment elasticsearch:import:model CLASS='Article' SCOPE='published'

Run this command to display usage instructions:

$ bundle exec rake -D elasticsearch

ActiveSupport Instrumentation

To display information about the search request (duration, search definition) during development, and to include the information in the Rails log file, require the component in your application.rb file:

require 'elasticsearch/rails/instrumentation'

You should see an output like this in your application log in development environment:

Article Search (321.3ms) { index: "articles", type: "article", body: { query: ... } }

Also, the total duration of the request to Elasticsearch is displayed in the Rails request breakdown:

Completed 200 OK in 615ms (Views: 230.9ms | ActiveRecord: 0.0ms | Elasticsearch: 321.3ms)

There's a special component for the Lograge logger. Require the component in your application.rb file (and set config.lograge.enabled):

require 'elasticsearch/rails/lograge'

You should see the duration of the request to Elasticsearch as part of each log event:

method=GET path=/search ... status=200 duration=380.89 view=99.64 db=0.00 es=279.37

Rails Application Templates

You can generate a fully working example Ruby on Rails application, with an Article model and a search form, to play with (it generates the application skeleton and leaves you with a Git repository to explore the steps and the code) with the 01-basic.rb template:

rails new searchapp --skip --skip-bundle --template https://raw.github.com/elastic/elasticsearch-rails/main/elasticsearch-rails/lib/rails/templates/01-basic.rb

Run the same command again, in the same folder, with the 02-pretty template to add features such as a custom Article.search method, result highlighting and Bootstrap integration:

rails new searchapp --skip --skip-bundle --template https://raw.github.com/elastic/elasticsearch-rails/main/elasticsearch-rails/lib/rails/templates/02-pretty.rb

Run the same command with the 03-expert.rb template to refactor the application into a more complex use case, with couple of hundreds of The New York Times articles as the example content. The template will extract the Elasticsearch integration into a Searchable "concern" module, define complex mapping, custom serialization, implement faceted navigation and suggestions as a part of a complex query, and add a Sidekiq-based worker for updating the index in the background.

rails new searchapp --skip --skip-bundle --template https://raw.github.com/elastic/elasticsearch-rails/main/elasticsearch-rails/lib/rails/templates/03-expert.rb


This software is licensed under the Apache 2 license, quoted below.

Licensed to Elasticsearch B.V. under one or more contributor
license agreements. See the NOTICE file distributed with
this work for additional information regarding copyright
ownership. Elasticsearch B.V. licenses this file to you under
the Apache License, Version 2.0 (the "License"); you may
not use this file except in compliance with the License.
You may obtain a copy of the License at


Unless required by applicable law or agreed to in writing,
software distributed under the License is distributed on an
KIND, either express or implied.  See the License for the
specific language governing permissions and limitations
under the License.