DEV Community

loading...
Cover image for How to Implement Elasticsearch in a Rails Web App: Part 1

How to Implement Elasticsearch in a Rails Web App: Part 1

Codica
A team of web developers building custom marketplaces, SaaS and PWAs with Ruby on Rails, React, Vue.js. Our Github: https://github.com/codica2
Originally published at codica.com Updated on ・3 min read

This article was originally published on Codica blog.

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.

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 will help us to manage multiple Ruby versions set up on our system.
To check the Ruby version, use rvm list and install the following one rvm install 2.6.1.

Install Rails 5.2.3
The next step is to set up Rails gem.

gem install rails -v 5.2.3

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

Install Elasticsearch 6.4.0
We have saved Elasticsearch to the Downloads folder. Start the service by typing in the request:
~/Downloads/elasticsearch-6.4.0/bin/elasticsearch

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

Here, we get the following:

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

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

You see the following window:

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:

rvm use 2.6.1
rails new elasticsearch_rails --api -T -d postgresql
cd elasticsearch_rails
bundle install
Enter fullscreen mode Exit fullscreen mode

First of all, configure config/database.yml structure similar to this:

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'] %>
Enter fullscreen mode Exit fullscreen mode

So, we have created the rails db:create 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
Enter fullscreen mode Exit fullscreen mode

After the table was built, launch the migration with the rails db:migrate command.

We have arranged all the test data needed. Copy the contents of the following file, insert it into db/seeds.rb, and run rails db:seed.

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.

Discussion (0)