> ## Documentation Index
> Fetch the complete documentation index at: https://algolia.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Replicas and multiple indices

> Learn how to use replicas and multiple indices with the Algolia gem.

export const Records = () => <Tooltip tip="A record is a searchable object in an Algolia index. Each record consists of named attributes." cta="Algolia records" href="/doc/guides/sending-and-managing-data/prepare-your-data#algolia-records">
    records
  </Tooltip>;

export const Index = () => <Tooltip tip="An Algolia index is a searchable dataset that consists of records and configuration settings. These settings define how the records are searched and ranked.">
    index
  </Tooltip>;

## Replica indices

Algolia uses one ranking strategy per <Index />.
If you want to provide more ranking or sorting strategies,
[add replica indices](/doc/guides/managing-results/refine-results/sorting/in-depth/exhaustive-sort#backend-implementation)
with the `add_replica` method.
To inherit the settings from the primary index, add `inherit:true`.

```ruby Ruby icon=code theme={"system"}
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
  end

end
```

## Share a single index

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 Ruby icon=code theme={"system"}
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}"
  end
end

class 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}"
  end
end
```

<Warning>
  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](/doc/framework-integration/rails/indexing/indexing#regular-reindexing)
</Warning>

## Target multiple indices

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

```ruby Ruby icon=code theme={"system"}
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
  end

end
```
