Elasticsearch Integrating Elasticsearch with Ruby on Rails for Advanced Search Functionality

By Opster Team

Updated: Jul 20, 2023

| 2 min read

Introduction 

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:

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

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

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

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:

ruby
Post.__elasticsearch__.create_index!

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

ruby
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'
    end
  end
end

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:

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

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

Conclusion 

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?