DEV Community

Rahul Chowdhury πŸ•Ά
Rahul Chowdhury πŸ•Ά

Posted on

Dynamic index name on Algolia with Rails

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?

Top comments (3)

Collapse
 
sylvainutard profile image
Sylvain Utard

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.

Collapse
 
ben profile image
Ben Halpern

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?

Collapse
 
rahulchowdhury profile image
Rahul Chowdhury πŸ•Ά

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