How to Implement Elasticsearch in a Rails Web App: Part 1
Tags: rails, ruby, tutorial, showdev
Published: 2019-06-27 14:00:36

---
title: How to Implement Elasticsearch in a Rails Web App: Part 1
published: true

description: When developing a web app, pay special attention to data analyzing and searching algorithms. For this purpose, Elasticsearch may become your best solution. Check a detailed tutorial on how to implement this powerful full-text search engine in a Rails Web app.
tags: rails, ruby, tutorial, showdev

cover_image: https://thepracticaldev.s3.amazonaws.com/i/5kmfk85b4tg4mk0iea36.jpg

canonical_url: https://www.codica.com/blog/developing-rails-web-app-with-elasticsearch/
---
*This article was originally published on [Codica blog](https://www.codica.com/blog/developing-rails-web-app-with-elasticsearch/).*

Building an app, you should take into account two of the most crucial aspects, i.e. information analyzing and searching. They define the overall success of your product. The same is true when you aim to develop a perfect Rails web app.

At Codica, we believe that when it goes about powerful and convenient search algorithms, Elasticsearch saves the day. We want to show you the process of creating a test Rails application with the technology implementation.

Today, we will set the tools and services needed and initiate a new Rails application.

You can read the full article version here: [How to Implement Elasticsearch When Developing a Rails Web App](https://www.codica.com/blog/developing-rails-web-app-with-elasticsearch/).

## Step #1: Setting up the tools

Before jumping into the actual code writing, let’s install the services and tools we are going to use.

**Install Ruby 2.6.1**
[RVM](https://rvm.io/) will help us to manage multiple Ruby versions set up on our system.
To check the Ruby version, use <code>rvm list</code> and install the following one <code>rvm install 2.6.1</code>.

**Install Rails 5.2.3**
The next step is to set up [Rails gem](https://github.com/rails/rails).

<code>gem install rails -v 5.2.3</code>

To make sure you have installed the needed Rails gem version, type in the request <code>Rails -v</code>.

**Install Elasticsearch 6.4.0**
We have [saved Elasticsearch](https://www.elastic.co/downloads/past-releases/elasticsearch-6-4-0) to the Downloads folder. Start the service by typing in the request:
<code>~/Downloads/elasticsearch-6.4.0/bin/elasticsearch</code>

Open the tool with [http://localhost:9200/](http://localhost:9200/) to make sure it is launched.

Here, we get the following:
![](https://thepracticaldev.s3.amazonaws.com/i/bqb09vi2afxpokc3ig7d.png)

**Install Kibana 6.4.2**
We have saved Kibana to the Downloads folder. To launch the service, type in the following:
<code>~/Downloads/kibana-6.4.2-linux-x86_64/bin/kibana</code>

To be sure that Kibana is operating, go to [http://localhost:5601/](http://localhost:5601/).

You see the following window:
![](https://thepracticaldev.s3.amazonaws.com/i/k4f3ymi26idsvppowqns.png)


We have installed all the needed tools and services. Let’s proceed to the following step.

## Step #2: Initiating a new Rails app

At this stage, we use both the PostgreSQL database and the Rails in API mode:
```ruby
rvm use 2.6.1
rails new elasticsearch_rails --api -T -d postgresql
cd elasticsearch_rails
bundle install
```

First of all, configure **config/database.yml** structure similar to this:
```ruby
default: &default
 adapter: postgresql
 encoding: unicode
 pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
 username: postgres

development:
 <<: *default
 database: elasticsearch_rails_development

test:
 <<: *default
 database: elasticsearch_rails_test

production:
 <<: *default
 database: elasticsearch_rails_production
 username: elasticsearch_rails
 password: <%= ENV['DB_PASSWORD'] %>
```

So, we have created the <code>rails db:create</code> database.

Now we are going to build a model which will be indexed and searchable. Create a **Location** table with two fields, such as **name** and **level**:

```
rails generate model location name level
```

After the table was built, launch the migration with the <code>rails db:migrate</code> command.

We have arranged all the test data needed. Copy the contents [of the following file](https://github.com/codica2/sample-article-code-elasticsearch-rails/blob/master/db/seeds.rb), insert it into **db/seeds.rb**, and run <code>rails db:seed</code>.

## Conclusion

In this post, we have discussed the tools and services needed and also created a Rails app.
In the next part, we will walk you through the process of Elasticsearch integration with the app created, adding functionality to it, and making the research request available.

Stay tuned and read the full article version here: [How to Implement Elasticsearch When Developing a Rails Web App](https://www.codica.com/blog/developing-rails-web-app-with-elasticsearch/).