Managing your Elasticsearch indexes in Rails

Most Rails apps start life with a single relational database (generally PostgreSQL or MySQL), which are great for many purposes, but can get pretty slow when doing unstructured searches over huge datasets. When you hit this kind of problem, it’s time to reach for something like Elasticsearch or Solr.

At carwow, a few of the team already had experience using Elasticsearch, so we went with that.

One of the bigger challenges with using a specialised search tool is keeping the data in sync between your main database and the search index. There’s a great blog post called Syncing Postgres to Elasticsearch over on the GoCardless blog, that details how to make sure you always get the right data pushed into Elasticsearch.

At the end, it mentions zero-downtime reindexing, which is detailed in the Elasticsearch docs. We’ve put together a little gem which manages most of this for you. There’s two main classes:

  • : this organises Elasticsearch aliases for you, so you can re-index without downtime
  • : this proves helpful methods for indexing and removing individual documents or batches of documents from the index, and hides the complexity of the aliases

Here’s an example. First, create a configuration for your index:

class MyModel < ApplicationRecord

MyModelIndex =
  mapping: {
  data_source: MyModel.some_scope
) do |my_model|
  # this block transforms an instance of MyModel into the hash which goes into Elasticsearch
    attr_1: my_model.attr_1,
    attr_2: my_model.attr_2,
    attr_3: my_model.attr_3

Now you’ll need to work out where this model gets updated and deleted, and ensure that we send the update to the index afterwards:

indexer =
indexer.index_batch([my_model1, my_model2])
indexer.delete_by_ids([123, 456])

This is how you re-index everything:

First, we create a new index called , and add it to the write alias, :

Now, every time the makes any changes, it’ll apply them to the old index and the new index, since both are in the write alias.

Next, we populate the new index, which just re-indexes every record from the scope configured:


At this point, you’ll have two indexes:

  • will still be the index for the read alias (), and will still be an index for the write alias ()
  • will only be under the write alias

You’ll probably want to fire up your favourite Elasticsearch UI (e.g. Kibana, Kopf, Cerebro), and make sure that the new index looks about right. Then, it’s time to switch the aliases over, so your app starts sending queries to the shiny new index:


If things start breaking, you can always switch back to the old one using:


Once you’re confident everything is working fine with the new index, it’s time to cleanup. These two methods stop writing to the old index (i.e., remove it from the write alias), and then delete the old index altogether:


Hopefully this saves you a bit of time when getting your Elasticsearch index set up!

carwow Product, Design & Engineering

What happens under the hood at

Isaac Seymour

Written by

carwow Product, Design & Engineering

What happens under the hood at