By design, Algolia provides one ranking formula per index. It means that, when you want to provide different rankings for the same data, you need to use different indices for each ranking. These additional indices are called replicas.

For example, imagine you have an e-commerce website, and by default, you want to sort search results from most relevant to least relevant. Yet, it might also make sense to let users sort from cheapest to most expensive. Because this is a different sorting logic, you can’t use the same index. Instead, you need another index with the same data and a different ranking formula. This will be your first replica. And if you want to reverse the order, from most expensive to least, you’ll need a second replica. This gives you in total three indexes, one primary and two replicas.

Replicas let you perform write operations on a single, primary index, and automatically execute the same operations on all of its replicas. So the data is always the same. it’s different with the settings: Each replica index can have completely different settings so that you can use them for different purposes.

Example

Back to our e-commerce example. Let’s say we have an index called products that looks like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
[
  {
    "name": "Samsung Galaxy",
    "description": "Samsung Galaxy Express 3 4G LTE with 8GB Memory Prepaid Cell Phone",
    "price": 49.99
  },
  {
    "name": "AT&T GoPhone - Apple iPhone 5s 4G LTE 16GB Memory Prepaid Cell Phone w\/Airtime Card - Gray",
    "description": "Enjoy exceptional service on a popular phone with this AT&T GoPhone iPhone 5s. Enjoy all the features of the iPhone 5s, including its 4-inch Retina display and 8.0-megapixel camera, in tandem with AT&T's GoPhone mobile service, which provides a flexible plan with no annual contract. Stay connected with family and friends with this AT&T GoPhone iPhone 5s.",
    "price": 194.99
  },
  ...
]

We could update the ranking directly to this index this way:

1
2
3
4
5
6
7
8
9
10
11
12
13
$index->setSettings([
  'ranking' => [
    'asc(price)',
    'typo',
    'geo',
    'words',
    'filters',
    'proximity',
    'attribute',
    'exact',
    'custom'
  ]
]);

Such settings work well if we always want to sort by cheapest price. Yet, in our case, we want to allow our end users to change the index is sorted so that they can see the most expensive matches first.

For this, we need to create a replica. A replica is an index that has the same data as its primary index, but different settings. A replica is tied to its primary index, so any data change on the primary index automatically is forwarded to the replica.

Using the API

To create replicas, you need to use the setSettings method. Note that you can add more than one replica at a time, in case you want to provide more alternative sorting strategies.

1
2
3
4
5
$index->setSettings([
  'replicas' => [
    'products_price_desc'
  ]
]);

Then, you can initialize the replica index and use setSettings to change the sorting:

1
2
3
4
5
6
7
8
9
10
11
12
13
$replicaIndex->setSettings([
  'ranking' => [
    'desc(price)',
    'typo',
    'geo',
    'words',
    'filters',
    'proximity',
    'attribute',
    'exact',
    'custom'
  ]
]);

Using the Dashboard

You can also create replicas and change their settings in your Algolia dashboard.

Create a replica

  • Go to your dashboard and select your index.
  • Click the Replicas tab.
  • Click the Create Replica Index button, type a name for your replica and press Enter.
  • Repeat for all additional replicas you want to add.
  • Don’t forget to save your changes.

Change settings of the replica

  • Select your replica index.
  • Click the Ranking tab.
  • In the Ranking Formula & Custom Ranking section, use the Sort-By function to add the “price” attribute and select DESC.
  • Don’t forget to save your changes.

Did you find this page helpful?