Framework integration / Rails / Configuration & Relevance

Replicas and multiple indices

Replica indices

Algolia uses one ranking strategy per index. If you want to provide more ranking or sorting strategies, add replica indices with the add_replica method. To inherit the settings from the primary index, add inherit:true.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 
classBook<ActiveRecord::Baseattr_protectedincludeAlgoliaSearchalgoliasearchper_environment: truedosearchableAttributes[:name,:author,:editor]# Define a replica index to search by `author` onlyadd_replica'Book_by_author',per_environment: truedosearchableAttributes[:author]end# define a replica index with custom ordering but same settings than the main blockadd_replica'Book_custom_order',inherit: true,per_environment: truedocustomRanking['asc(rank)']endendend

Share a single index

If you want to share an index for multiple models, make sure that your objectIDs are unique. For example, you can prepend the model class name to the record’s id:

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 
classStudent<ActiveRecord::Baseattr_protectedincludeAlgoliaSearchalgoliasearchindex_name: 'people',id: :algolia_iddo# [...]endprivatedefalgolia_id"student_#{id}"# ensure the teacher & student IDs are not conflictingendendclassTeacher<ActiveRecord::Baseattr_protectedincludeAlgoliaSearchalgoliasearchindex_name: 'people',id: :algolia_iddo# [...]endprivatedefalgolia_id"teacher_#{id}"# ensure the teacher & student IDs are not conflictingendend

To reindex a model that’s part of a shared index, you must use Model.reindex! instead of Model.reindex. If you use reindex, the resulting index would only contain records for the current model. For more information, see Zero-downtime indexing

Target multiple indices

To index records in several indices, use the add_index method.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 
classBook<ActiveRecord::Baseattr_protectedincludeAlgoliaSearchPUBLIC_INDEX_NAME="Book_#{Rails.env}"SECURED_INDEX_NAME="SecuredBook_#{Rails.env}"# store all books in index 'SECURED_INDEX_NAME'algoliasearchindex_name: SECURED_INDEX_NAMEdosearchableAttributes[:name,:author]# convert security to tagstagsdo[released?'public':'private',premium?'premium':'standard']end# store all 'public' (released and not premium) books in index 'PUBLIC_INDEX_NAME'add_indexPUBLIC_INDEX_NAME,if: :public?dosearchableAttributes[:name,:author]endendprivatedefpublic?released&&!premiumendend
Did you find this page helpful?
close