Integrating Algolia Search into a Rails App

Greetings and Salutations Dear Reader!

Search is a cool thing. If you need to navigate information, you want your search to return relevant results, as quickly as possible. If you have a nice sized database and you want to have a performant, cool search experience, you have plenty of options. For example, you could use Elasticsearch or you can integrate with a service like Swiftype or Algolia.

Now, let us pretend you have a super cool rails app all set up.

The goal here is to set up a basic app, and populate your database with records you can then import into Algolia. We’re going to be scaffolding here, to make sure its easy to follow. The important parts are to have the migration and model.

The app I’ve built to showcase the Rails and Algolia integrations is a fun little toy called `plant_searcher_app`. This app will have all the fun things that you get with Rails, Models! Views! Controllers!

Lions! … ok, no lions, I got carried away… sorry.

Ok, lets create our rails app:

rails new plant_searcher_app —-database=postgresql

Next, we’ll scaffold out the Plant model and migration:

rails generate scaffold Plant name:string symbol:string

So now we need data, or um plants! I love data, and I love plants. It is great.

If you do too, there is a fun site to check out to get data, of course I can’t be held responsible if it is gone in a few days ( it is only … the USDA ). If you are reading this in the future — that is a dark joke, you know … a fact.

To get your own subset of data, take a quick look at the “Advanced Search and Download feature. The data we are using in our super cool app is a csv of native plants of Massachusetts. Then you go ahead and create a bunch of Plant records.

I personally prefer seeding my database with test data. You can essentially run rake db:seed to populate your database. The app is pretty basic right now, but it is all we need for this example.

More importantly, we have a database full of plant records, which have the scientific name and the symbol used for that plant.

And on to the fun!

You can set up a trial with Algolia, they don’t even ask for your credit card number — so sign up and check it out. They have some pretty cool documentation, and even a rails gem! You will need to get your new app id and api key, here.

Now, remember, do not commit your app id or api key to a public repo! So be sure to set up some environment variables. I like dotenv-rails, you could use whatever works for you.

Next we will add the cool rails gem I told you about, and some configuration.

At the moment, our app only has one model, in Plant.rb , but for this next step, I think we should use a concern. This means the logic will live in that concern and be easy to share across models.

So, now you are really close to being all set up!

We just need to get the data into the indices. To do this we’re going to write a few rake tasks.

Yay rake tasks! We can go ahead and try a few of these tasks out. Find a nice plant record, like the plant ‘Actaea racemosa var. racemosa’, and run the command rake 'search:add_one[Plant, 10]' .

At this point, we can look at our index in the cool Algolia Dashboard for indices. You should have a plants_development index, and when you browse that you should see one record. Your record will have an id of plant_10 .

And now you have your index all set up and integrated into your super cool rails app!

We can run rake 'search:index_all_plants' and Celebrate!

What now?

You can search your cool database in the Algolia dashboard and find all the native plant names you like … which is pretty great .

Some ideas for next steps.

  • We might need be sure when we update a Plant record, that it will be re_indexed — There are a few options for this, you can set up sidekiq for your rails app and spin up a job when you update the record.
  • We might want a super cool front end for user to take advantage of the fast and relevant results —We could create a really fun react app, and hit the Algolia api directly!

The opportunities are endless, and there is a lot of fun you can have with it.

if you liked this, please ❤ and/or share!

Also here is the code! (yay github!)