Dynamic index name on Algolia with Rails

twitter logo github logo ・1 min read

Anyone used/using Algolia with Rails?

I need to set the name of my index dynamically based on an association in my model.

Example:

-> Indexing an article
-> Article belongs to Publication named dev
-> Index name should be set to dev
-> The new article gets added to the dev index

Any help, guys?

twitter logo DISCUSS (3)
markdown guide
 

Unfortunately, dynamic index names are not supported. The gem actually needs them to be statically defined in your model in order to handle all the configuration (get/set settings) sync.

What you should do however, is relying a single index but tagging your records with their type. Then at query time, you can restrict the type of content you're looking for. You could also use facets but since having the associated counts is not required, tags are more efficient.

 

I'd guess maybe you want to dip into Algolia Ruby instead of using the Rails gem functionality, which does everything at class level and doesn't take arguments per se.

You might want to look at whether dynamic index names is an anti-pattern with Algolia. I'm not sure but I could see maybe it being ill-advised for some reason. Do you need everything to be a separate index altogether or could you do this a different way?

 

Yeah, I ended up using a single index with facets for filtering articles from a specific publication.

Classic DEV Post from May 10

Why I ignore the hype (and you should too)

It’s been 20 years since I made my first website. I've been burned by the hype ti...

Rahul Chowdhury 🕶 profile image
Android Engineer with over 3.5 years of experience. Writes code in Kotlin, Ruby and JavaScript.