By design, Algolia provides one ranking formula per index: 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.
If you want to set up sort by attribute it is important that you understand replica indices.
To sort by attribute, you will first need to create a replica index and then modify the ranking formula of the replica. This can be done through the Dashboard and the API.
This guide will help you set up the necessary back end for sorting by attribute, but you need to configure your front end (with widgets or custom logic) to make the option accessible to your users.
Attribute format
Attributes used for sorting must have boolean or numerical values. You cannot use a string attribute and dates must be represented as numbers.
Numerical values should be indexed as actual numbers, not strings.
Using the API
1. Creating a replica
To create replicas, you need to use the setSettings method on your primary index. You can add more than one replica at a time if you want to provide multiple alternative sorting strategies.
// No initIndex
client.execute{changeSettingsof"products_price_desc"`with`IndexSettings(ranking=Some(Seq(Ranking.desc("price"),Ranking.typo,Ranking.geo,Ranking.words,Ranking.filters,Ranking.proximity,Ranking.attribute,Ranking.exact,Ranking.custom)))}
Using the Dashboard
1. Creating 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.
2. Changing replica settings
Select your replica index.
Click the Ranking tab.
In the Ranking Formula & Custom Ranking section, use the Sort-By function to add the attribute you want to sort by and select either DESC or ASC.