Elasticsearch Integrating Elasticsearch with Ruby on Rails for Advanced Search Functionality

By Opster Team

Updated: Jul 20, 2023

| 2 min read


Elasticsearch is a widely used open-source search and analytics engine that provides advanced search capabilities. Ruby on Rails, on the other hand, is a popular web application framework written in Ruby. The integration of Elasticsearch with Ruby on Rails can significantly enhance the search functionality of Rails applications. This article will guide you through the process of integrating Elasticsearch with a Rails application.

Setting Up Elasticsearch in Rails

Before integrating Elasticsearch with Rails, you need to install it on your system. You can download Elasticsearch from the official website and follow the installation instructions provided there. Once installed, you can verify the installation by running the following command in your terminal:

curl -X GET "localhost:9200"

If Elasticsearch is running correctly, you should see a response with the version number and other details.

Next, you need to add the Elasticsearch gem to your Rails application. Add the following line to your Gemfile:

gem 'elasticsearch-model', git: 'git://github.com/elastic/elasticsearch-rails.git'
gem 'elasticsearch-rails', git: 'git://github.com/elastic/elasticsearch-rails.git'

Then run `bundle install` to install the gem.

Integrating Elasticsearch with a Rails Model

To integrate Elasticsearch with a Rails model, you need to include the `Elasticsearch::Model` module in your model. For example, if you have a `Post` model and you want to add Elasticsearch functionality to it, you can do it as follows:

class Post < ApplicationRecord
  include Elasticsearch::Model
  include Elasticsearch::Model::Callbacks

The `Elasticsearch::Model::Callbacks` module automatically indexes your model instances when they are created, updated, or deleted.

Creating and Configuring the Elasticsearch Index

To create an Elasticsearch index for your model, you can use the `create_index!` method. For example:


You can also configure the index settings and mappings by defining a `settings` method in your model. For example:

class Post < ApplicationRecord
  include Elasticsearch::Model
  include Elasticsearch::Model::Callbacks

  settings index: { number_of_shards: 1 } do
    mappings dynamic: 'false' do
      indexes :title, analyzer: 'english'
      indexes :content, analyzer: 'english'

In this example, the `settings` method configures the index to have one shard and disables dynamic mapping. It also sets up two fields, `title` and `content`, to be indexed with the `english` analyzer.

Searching with Elasticsearch

Once you have integrated Elasticsearch with your Rails model and set up the index, you can use the `search` method to perform searches. For example:

@posts = Post.search('ruby on rails')

This will return all posts that contain the phrase “ruby on rails” in their title or content.


In conclusion, integrating Elasticsearch with Ruby on Rails can greatly enhance the search capabilities of your Rails applications. By following the steps outlined in this article, you can set up Elasticsearch in your Rails application and start performing advanced searches.

How helpful was this guide?

We are sorry that this post was not useful for you!

Let us improve this post!

Tell us how we can improve this post?