Sort by attribute
On this page
To sort an index by an attribute, you must first create a replica index, then update your index configuration.
You can choose between setting up:
- Relevant sorting with virtual replicas (Build and Premium plans only).
- Exhaustive sorting with standard replicas.
Relevant sorting is available on the Build and Premium pricing plans.
To let users select between different rankings in your user interface,
you also need to update your user interface—for example, by including the
Algolia applies one ranking strategy to each index. If you want to rank results differently, like sorting by a specific attribute, you must create separate replica indices for each type of ranking.
You can only sort by attributes with boolean or numerical values. Check, that you index numerical attributes as numbers, not strings. If you want to sort by dates, make sure to represent them as numbers as well.
Configure an attribute for sorting in the Algolia dashboard
Refresh the dashboard page in your browser.
Select the replica index.
On the Configuration tab, go to Relevant sort or Ranking & Sorting. You’ll see either option, depending on the type of replica you want to configure (virtual or standard).
To add an attribute for sorting, click +Add a sort attribute or +Add sort-by attribute and determine the sort direction (ascending or descending).
Review and save your changes.
Configure an attribute for sorting with the API
- Create a replica index.
- Initialize the replica index.
- Update the
rankingparameter of the replica index with the
Example: Relevant sort with a virtual replica
This example configures a Relevant sort with a virtual replica.
The virtual replica reuses the ranking attribute from its primary index,
so you only need to include the
customRanking attribute that is used for sorting.
1 2 3 4 5 6 7 $replica_index = $client->initIndex('products_virtual_price_desc'); $replica_index->setSettings([ 'customRanking' => [ 'desc(price)' ] ]);
Example: Exhaustive sort with a standard replica
This example configures the ranking of a standard replica index to be sorted by price in descending order. You need to provide all ranking attributes.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 $replica_index = $client->initIndex('products_standard_price_desc'); $replica_index->setSettings([ 'ranking' => [ 'desc(price)', 'typo', 'geo', 'words', 'filters', 'proximity', 'attribute', 'exact', 'custom' ] ]);