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 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 sort-by
widget.
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#
Found an issue?
Create a replica index .
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#
Found an issue?
Create a replica index .
Initialize the replica index .
Update the ranking
parameter of the replica index with the setSettings
method.
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)'
]
]);
1
2
3
4
5
6
7
replica_index = client . init_index ( 'products_virtual_price_desc' )
replica_index . set_settings ({
customRanking: [
'desc(price)'
]
})
1
2
3
4
5
6
7
8
9
const replicaIndex = client . initIndex ( ' products_virtual_price_desc ' );
replicaIndex . setSettings ({
customRanking : [
" desc(price) "
]
}). then (() => {
// done
});
1
2
3
4
5
6
7
replica_index = client . init_index ( 'products_virtual_price_desc' )
replica_index . set_settings ({
'customRanking' : [
'desc(price)'
]
})
1
2
3
4
5
6
7
let replica_index = client . index ( withName : "products_virtual_price_desc" )
replica_index . setSettings ([
"customRanking" : [
"desc(price)"
]
])
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
SearchIndex replica_index = client . InitIndex ( "products_virtual_price_desc" );
IndexSettings settings = new IndexSettings
{
CustomRanking = new List < string >
{
"desc(price)"
}
};
// Synchronous
replica_index . SetSettings ( settings );
// Asynchronous
await replica_index . SetSettingsAsync ( settings );
1
2
3
4
5
6
7
8
9
10
11
12
// Sychronous
Index < Contact > replica_index = client . initIndex ( "products_virtual_price_desc" , Contact . class );
// Asynchronous
AsyncIndex < Contact > replica_index = client . initIndex ( "products_virtual_price_desc" , Contact . class );
// Both
replica_index . setSettings (
new IndexSettings (). setCustomRankingRanking ( Arrays . asList (
"desc(price)"
))
);
1
2
3
4
5
6
7
replica_index := client . InitIndex ( "products_virtual_price_desc" )
res , err := replica_index . SetSettings ( algoliasearch . Map {
"customRanking" : [] string {
"desc(price)" ,
},
})
1
2
3
4
5
6
7
8
9
// No initIndex
client . execute {
setSettings of "products_virtual_price_desc" `with` IndexSettings (
customRanking = Some ( Seq (
Ranking . 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'
]
]);
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
replica_index = client . init_index ( 'products_standard_price_desc' )
replica_index . set_settings ({
ranking: [
'desc(price)' ,
'typo' ,
'geo' ,
'words' ,
'filters' ,
'proximity' ,
'attribute' ,
'exact' ,
'custom'
]
})
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
const replicaIndex = client . initIndex ( ' products_standard_price_desc ' );
replicaIndex . setSettings ({
ranking : [
" desc(price) " ,
" typo " ,
" geo " ,
" words " ,
" filters " ,
" proximity " ,
" attribute " ,
" exact " ,
" custom "
]
}). then (() => {
// done
});
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
replica_index = client . init_index ( 'products_standard_price_desc' )
replica_index . set_settings ({
'ranking' : [
'desc(price)' ,
'typo' ,
'geo' ,
'words' ,
'filters' ,
'proximity' ,
'attribute' ,
'exact' ,
'custom'
]
})
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
let replica_index = client . index ( withName : "products_standard_price_desc" )
replica_index . setSettings ([
"ranking" : [
"desc(price)" ,
"typo" ,
"geo" ,
"words" ,
"filters" ,
"proximity" ,
"attribute" ,
"exact" ,
"custom"
]
])
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
SearchIndex replica_index = client . InitIndex ( "products_standard_price_desc" );
IndexSettings settings = new IndexSettings
{
Ranking = new List < string >
{
"desc(price)" ,
"typo" ,
"geo" ,
"words" ,
"filters" ,
"proximity" ,
"attribute" ,
"exact" ,
"custom"
}
};
// Synchronous
replica_index . SetSettings ( settings );
// Asynchronous
await replica_index . SetSettingsAsync ( settings );
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
// Sychronous
Index < Contact > replica_index = client . initIndex ( "products_standard_price_desc" , Contact . class );
// Asynchronous
AsyncIndex < Contact > replica_index = client . initIndex ( "products_standard_price_desc" , Contact . class );
// Both
replica_index . setSettings (
new IndexSettings (). setRanking ( Arrays . asList (
"desc(price)" ,
"typo" ,
"geo" ,
"words" ,
"filters" ,
"proximity" ,
"attribute" ,
"exact" ,
"custom" ,
))
);
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
replica_index := client . InitIndex ( "products_standard_price_desc" )
res , err := replica_index . SetSettings ( algoliasearch . Map {
"ranking" : [] string {
"desc(price)" ,
"typo" ,
"geo" ,
"words" ,
"filters" ,
"proximity" ,
"attribute" ,
"exact" ,
"custom" ,
},
})
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
// No initIndex
client . execute {
setSettings of "products_standard_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
))
)
}
© Algolia · Privacy Policy · Cookie settings