Elastic Search and Ruby on Rails

Keenan Jones
4 min readJan 1, 2021

--

Elastic Search is a search engineer based on the Lucene library and written in Java. The Apache Lucene library is a high performance — full featured text search engine library written in Java. Full text search refers to techniques for searching a single computer stored document or a collection in a full-text search. Full text search quickly finds all instances of a term without having to scan rows and without having to know which column a term is stored in. Often the search engine is searching through large data sets so the search algorithms for retrieval is rather complex.

Elastic Search also provides a distributed, multitenant-capable full text search engine with an HTTP web interface and schema free JSON document. Now in my case I wanted to implement search functionality to a ruby on rails image repo. We want to search images by name, description and keywords. Now to implement Elastic search with ruby on rails. Elasticsearch connects to ActiveRecord with its own ActiveModel via Adapters. Elasticsearch-rails use three separate gems:

  • The elasticsearch-model, which contains the search algorithm and overall search integration for ruby on rails ActiveRecord models. which extends the ActiveRecord model with functionality related to Elasticsearch.
  • Elasticsearch-persistence, which provides a standalone persistence layer for Ruby/Rails objects and models, this allows you to: to save, delete, find and search objects stored in Elasticsearch and configure mappings and settings for the index
  • Elasticsearch-rails, which contains various features for Ruby on rails applications.

To implement Elasticsearch for rails. I bumped into a problem before I explained the problem. I’m going to walk through my steps to how I got to this issue. Before implementing elastic to my project i was working on, I followed a quick and easy tutorial. The tutorial was to implement a rails app with elasticsearch.

first step

rails new elastic_rails
cd elastic_rails
rails s

Easy Rail start up

now quickly generate our Post model

rails generate model Post author:string title:string body:text tags:string published:boolean published_on:timestamp

create the database

rake db:create db:migrate

Easy no problems yet right?

Okay, now we need to open the console to create a Post this could be done in the seed file. But let’s keep following along just to be safe.

post = Post.new
post.title = 'My first post'
post.body = '<h1>Hello world! This is my first post!</h1>'
post.author = 'Lazarus Lazaridis'
post.tags = 'first, salute, hello'
post.published = true
post.published_on = DateTime.now
post.save

Success!

Now the controller and routes

rails generate scaffold_controller post

and in the config/routes.rb file

Rails.application.routes.draw do
# For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html
root 'posts#index'

resources :posts
end

Now this is a very generic setup but let’s keep pushing!

Our first look at the elasticsearch gem. add the following gems to the GemFIle and bundle.

gem 'elasticsearch-model'
gem 'elasticsearch-rails'

Now here we go add the following lines to the Post model in our app/models/post.rb file

include Elasticsearch::Model
include Elasticsearch::Model::Callbacks

Now these lines add the elasticsearch functionality to our Post model common gem usage. Now here’s the thing almost every tutorial I found asked me to install Kibana I thought it was similar to Postman so I spent a good three hours trying to get Elasticsearch to work with Postman when I finally gave in to try and download kibana. This didn’t work my mac didn’t to download it. I tried to install it with homebrew didn’t work and download it via my browser this also didn’t work.

Okay let’s just try to use Postman… Now back to the tutorial, we need to import our posts to elastic search so it can create a index so we can search for this post. We can do this in the model but let’s do it in the console so we can get results quick. So enter the following line in the console.

Post.import

Now this is the proper error so far because we can add

Post.import(force: true)

And this is the sticky situation. When I enter this line of code I get this code

Faraday::ConnectionFailed (Failed to open TCP connection to localhost:9200 (Connection refused - connect(2) for "localhost" port 9200))

A quick search tells me that faraday is a ruby gem its a middleware. Okay fine install this and nothing. Now after some time spent on stackoverflow. I find that it might be my Ruby -v, now at this point I’m done so I look up how to add search functionality to my rails project and here’s what I find. In the desired model add the following method.

def self.search(search)
if search
find(:all, :conditions => ['name LIKE ?', "%#{search}%"])
else
find(:all)
end
end

and add this line in your controller

def index
@post = Post.search(params[:search])
end

I’m writing this article to tell you sometime you don’t need a complex gem or extension package. Thanks for taking the time to read this article. I hope nobody sees this as a troll. I wanted to detail my research and how I decided to change my course of action.

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

--

--

Keenan Jones
Keenan Jones

No responses yet

Write a response