Algolia uses one ranking strategy per .
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.
Ruby
Report incorrect code
Copy
class Book < ActiveRecord::Base attr_protected include AlgoliaSearch algoliasearch(per_environment: true) do searchableAttributes [:name, :author, :editor] # Define a replica index to search by `author` only add_replica("Book_by_author", per_environment: true) do searchableAttributes [:author] end # define a replica index with custom ordering but same settings than the main block add_replica("Book_custom_order", inherit: true, per_environment: true) do customRanking ["asc(rank)"] end endend
If you want to share an index for multiple models,
make sure that your object IDs are unique.
For example, you can prepend the model class name to the record’s ID:
Ruby
Report incorrect code
Copy
class Student < ActiveRecord::Base attr_protected include AlgoliaSearch algoliasearch(index_name: "people", id: :algolia_id) do # [...] end private def algolia_id # ensure the teacher & student IDs are not conflicting "student_#{id}" endendclass Teacher < ActiveRecord::Base attr_protected include AlgoliaSearch algoliasearch(index_name: "people", id: :algolia_id) do # [...] end private def algolia_id # ensure the teacher & student IDs are not conflicting "teacher_#{id}" endend
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 for the current model.
For more information, see Zero-downtime indexing
To index records in several indices, use the add_index method.
Ruby
Report incorrect code
Copy
class Book < ActiveRecord::Base attr_protected include AlgoliaSearch PUBLIC_INDEX_NAME = "Book_#{Rails.env}" SECURED_INDEX_NAME = "SecuredBook_#{Rails.env}" # store all books in index 'SECURED_INDEX_NAME' algoliasearch(index_name: SECURED_INDEX_NAME) do searchableAttributes [:name, :author] # convert security to tags tags do [released ? "public" : "private", premium ? "premium" : "standard"] end # store all 'public' (released and not premium) books in index 'PUBLIC_INDEX_NAME' add_index(PUBLIC_INDEX_NAME, if: :public?) do searchableAttributes [:name, :author] end end private def public? released && !premium endend