How To Setup Elasticsearch In Your Rails Application In Development

Step 1 – Install elasticsearch

If you haven’t installed elasticsearch on your local development machine, this article tells you how to do so in Linux

Step 2 – Add the right ruby gems for the Ruby elasticsearch client in your Gemfile and bundle install

1
2
gem 'elasticsearch-rails'
3
 
4
gem 'elasticsearch-model'
5
 
6
gem 'elasticsearch-extensions'
7

Step 3 – Configure indexes in your Rails model(s)

01
02
class User < ActiveRecord::Base
03
  include Elasticsearch::Model
04
  include Elasticsearch::Model::Callbacks
05
 
06
  settings index: { number_of_shards: 2 } do
07
    mappings dynamic: 'false' do
08
      indexes :name, analyzer: 'english', index_options: 'offsets'
09
      indexes :email, analyzer: 'english', index_options: 'offsets'
10
      indexes :hobbies, analyzer: 'english', index_options: 'offsets'
11
    end
12
  end
13
 
14
  def self.search_users(search_words)
15
    User.import
16
    response = User.search(
17
      size: 20, 
18
      query: {
19
            match: {
20
                "_all" => {
21
                  "query" => search_words,
22
                  "operator" => "or"
23
                }
24
            }
25
        }
26
    )
27
    response.records
28
  end
29
end
30

Step 4 – Create / refresh the index

Start up the rails console in development mode and issue the following
commands:

1
2
User.__elasticsearch__.create_index! force: true
3
User.__elasticsearch__.refresh_index!
4

These commands will setup a search index with your desired configuration in the User model above. If you ever change your settings, you’ll need to reissue these commands. I hope to come back and update these instructions with a more automated process soon.

Choosing the query type

In the User model, I called the search method on the User model to return 20 results (see the size: 20 setting). The _all keyword tells elasticsearch to match all documents using the query search_words and “or” logic. For example if search_words is “Joe Smith”, elasticsearch searches for documents with the words Joe OR Smith. The query I use above should provide you what you need for simple searches.

How To Install Elasticsearch On Linux for Rails Development

What is elasticsearch?

Elasticsearch is an open-source real-time search and analytics engine that runs on top of Lucene, a Java-based indexing and search library.

How to install it on Linux Mint 16 and Ubuntu 14.04

Step 1 – Download elasticsearch

At the command prompt, type:

1
2
wget https://download.elasticsearch.org/elasticsearch/elasticsearch/elasticsearch-1.3.1.deb
3

Elasticsearch was at version 1.3.1 at the time of this writing. You can
substitute the latest version number when you go to download it.

Step 2 – Install it by double clicking on the deb package or via the command prompt

If you want to install it via the command prompt, type:

1
2
sudo dpkg -i elasticsearch-1.3.1.deb
3

Otherwise, just double click on the .deb package and the Linux installer
will prompt you to install it. For example, in Linux Mint 16, a dialog
box comes up that asks me to install it. I just click the Install
button and it installs.

Starting up elasticsearch

To start elasticsearch, at the command prompt, type:

1
2
sudo /etc/init.d/elasticsearch start
3

How To Squash Your Last 2 Git Commits

Why Squash Commits? If you’ve been working with Git for a while you might start to wonder why you can’t combine that last trivial “hello world” type commit with your previously more meaningful commit. Fortunately, you don’t have to wonder as Git

Continue Reading …

Installing Phantomjs on Linux Mint 16

Just wrote a protip on Coderwall about how to install Phantomjs on Linux Mint 16...

Continue Reading …

Setting Up Spring + RSpec For Testing Your Rails 4 Application

What is spring? Spring is an application preloader. That means it boots up your rails application and keeps it running in the background. Why is this good? It means you don’t have to boot it up every time you need to run a test

Continue Reading …

Avoiding the ‘oCol is undefined’ Error with jQuery-datatables-rails

The ‘oCol is undefined’ Error Recently I installed jQuery-datatables-rails in a Rails 4 project. With the Firebug plugin in Firefox, I received an error ‘oCol is undefined’. With Google Chrome via the console, I received an error ‘Cannot

Continue Reading …

Integrating Fat Free CRM Into Your Rails Application

What is Fat FREE CRM? Fat Free CRM is an open-source customer relationship management piece of software that lets you perform the basic marketing activities integral to the sales cycle. Tracking leads, managing campaigns, and contact lists are

Continue Reading …

How to Pass Javascript Functions Without Them Firing

In Javascript, functions are first-class citizens. Implications? This means you can pass functions around in Javascript like they were variables. Here’s some example code to illustrate the concept. //setup prototype function

Continue Reading …

Why You As a Client Want a Rails Codebase With Tests

Would you ever stop changing the oil on your car? I wouldn’t. It’s part of the maintenance that leads to a longer life span for my car and ensures it runs smoothly. In fact, because the automobile industry has done such a good job of educating

Continue Reading …

How to Move One Directory Into Another Directory With Linux and Git

Recently, I was tired and used the Linux mv command and erased a whole bunch of websites when I was SSHing into my web host account. So now I am going to document how to use the mv command just for kicks (and because it’s a short, fun thing to blog

Continue Reading …