By default, Algolia puts the most relevant items first in results.
However, you might want to sort results by a specific attribute, like price or date.
To sort your search results by a specific attribute:
Create a replica index
Configure an attribute for sorting
Update your user interface
Replica indices
Think of a replica index as a duplicate of your primary index but with a different sorting of results.
Choose from two types of replicas: standard or virtual :
Use a standard replica for exhaustive sorting. This method sorts results by the chosen attribute.
Use a virtual replica for relevant sorting. This method prioritizes relevant results while still incorporating the sorting preference for the chosen attribute.
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 and Sorting .
You’ll see either option, depending on the type of replica you want to configure (virtual or standard).
Click +Add a sort attribute or +Add sort-by attribute
Type in the name of the attribute you want to sort. Be careful here: Algolia doesn’t check to see if what you type matches the name of an attribute in the index.
Determine the sort direction (Ascending or Descending ).
Review and save your changes.
Create a replica index .
Update the ranking
parameter of the replica index with the setSettings
method.
Update user interface
To let users select between different rankings in your user interface,
you also need to update it—for example, by providing a sort-by
widget.
Example: relevant sort by price
This example applies relevant sorting to a virtual replica.
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" )
))
)
}
For virtual replicas, you only need to include the customRanking
attribute used for sorting: price
.
Example: exhaustive sort by price
This example applies exhaustive sorting to a standard replica.
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
))
)
}
For standard replicas, you need to provide all ranking attributes.
© Algolia · Privacy Policy · Cookie settings