Share a single index between multiple models
masterTo share one Algolia index across different models, specify the same index_name in their algoliasearch blocks.
Critical: You must ensure that the id (or the value returned by the id: option) is unique across all models to prevent objectID collisions. For example, prefixing the ID with the model name (e.g., "student_#{id}").
Warning: When sharing an index, never use MyModel.reindex (the atomic version), as it will replace the entire shared index with only the records from that specific model. Always use reindex!.
class Student < ActiveRecord::Base
include AlgoliaSearch
algoliasearch index_name: 'people', id: :algolia_id
private
def algolia_id
"student_#{id}"
end
end
class Teacher < ActiveRecord::Base
include AlgoliaSearch
algoliasearch index_name: 'people', id: :algolia_id
private
def algolia_id
"teacher_#{id}"
end
end