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.
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
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.
Algolia offers two flavors of replicas, standard and virtual replicas . You can choose either one depending on your needs .
The example below shows how to create the products_price_desc
standard replica.
1
2
3
4
5
$index -> setSettings ([
'replicas' => [
'products_price_desc'
]
]);
1
2
3
4
5
index . set_settings ({
replicas: [
'products_price_desc'
]
})
1
2
3
4
5
6
7
index . setSettings ({
replicas : [
' products_price_desc '
]
}). then (() => {
// done
});
1
2
3
4
5
index . set_settings ({
'replicas' : [
'products_price_desc'
]
})
1
2
3
4
5
index . setSettings ([
"replicas" : [
"products_price_desc"
]
])
1
2
3
4
5
6
7
index . setSettings (
new JSONObject (). put (
"replicas" ,
new JSONArray ()
. put ( "products_price_desc" )
)
);
1
2
3
4
5
6
7
8
9
10
11
12
IndexSettings settings = new IndexSettings
{
Replicas = new List < string >
{
"products_price_desc"
}
};
index . SetSettings ( settings );
// Asynchronous
await index . SetSettingsAsync ( settings );
1
2
3
4
5
index . setSettings (
new IndexSettings (). setReplicas ( Arrays . asList (
"products_price_desc"
))
);
1
2
3
4
5
res , err := index . SetSettings ( algoliasearch . Map {
"replicas" : [] string {
"products_price_desc" ,
},
})
1
2
3
4
5
6
7
client . execute {
changeSettings of "products" `with` IndexSettings (
replicas = Some ( Seq (
"products_price_desc"
))
)
}
The example below shows how to create the products_price_desc
virtual replica.
1
2
3
4
5
$index -> setSettings ([
'replicas' => [
'virtual(products_price_desc)'
]
]);
1
2
3
4
5
index . set_settings ({
replicas: [
'virtual(products_price_desc)'
]
})
1
2
3
4
5
6
7
index . setSettings ({
replicas : [
' virtual(products_price_desc) '
]
}). then (() => {
// done
});
1
2
3
4
5
index . set_settings ({
'replicas' : [
'virtual(products_price_desc)'
]
})
1
2
3
4
5
index . setSettings ([
"replicas" : [
"virtual(products_price_desc)"
]
])
1
2
3
4
5
6
7
index . setSettings (
new JSONObject (). put (
"replicas" ,
new JSONArray ()
. put ( "virtual(products_price_desc)" )
)
);
1
2
3
4
5
6
7
8
9
10
11
12
IndexSettings settings = new IndexSettings
{
Replicas = new List < string >
{
"virtual(products_price_desc)"
}
};
index . SetSettings ( settings );
// Asynchronous
await index . SetSettingsAsync ( settings );
1
2
3
4
5
index . setSettings (
new IndexSettings (). setReplicas ( Arrays . asList (
"virtual(products_price_desc)"
))
);
1
2
3
4
5
res , err := index . SetSettings ( algoliasearch . Map {
"replicas" : [] string {
"virtual(products_price_desc)" ,
},
})
1
2
3
4
5
6
7
client . execute {
changeSettings of "products" `with` IndexSettings (
replicas = Some ( Seq (
"virtual(products_price_desc)"
))
)
}
Changing replica settings
To change a replica’s settings:
Initialize the replica.
Use setSettings
to change the replica’s setting.
In the example below, the products_price_desc
standard replica is sorted by price, descending.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
$replica_index = $client -> initIndex ( 'products_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_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_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_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_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
Index replica_index = client . getIndex ( "products_price_desc" );
replica_index . setSettings ( new JSONObject (). put ( "ranking" , new JSONArray ()
. put ( "desc(price)" )
. put ( "typo" )
. put ( "geo" )
. put ( "words" )
. put ( "filters" )
. put ( "proximity" )
. put ( "attribute" )
. put ( "exact" )
. put ( "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_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_price_desc" , Contact . class );
// Asynchronous
AsyncIndex < Contact > replica_index = client . initIndex ( "products_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_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 {
changeSettings of "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
))
)
}
In the example below, the products_price_desc
virtual replica is sorted by price, descending. Remember, a virtual replica reuses its primary index’s ranking and settings. That’s why only the attribute or attributes used for sorting are specified here.
1
2
3
4
5
6
7
$replica_index = $client -> initIndex ( 'products_price_desc' );
$replica_index -> setSettings ([
'customRanking' => [
'desc(price)'
]
]);
1
2
3
4
5
6
7
replica_index = client . init_index ( 'products_price_desc' )
replica_index . set_settings ({
customRanking: [
'desc(price)'
]
})
1
2
3
4
5
6
7
8
9
const replicaIndex = client . initIndex ( ' products_price_desc ' );
replicaIndex . setSettings ({
customRanking : [
" desc(price) "
]
}). then (() => {
// done
});
1
2
3
4
5
6
7
replica_index = client . init_index ( 'products_price_desc' )
replica_index . set_settings ({
'customRanking' : [
'desc(price)'
]
})
1
2
3
4
5
6
7
let replica_index = client . index ( withName : "products_price_desc" )
replica_index . setSettings ([
"customRanking" : [
"desc(price)"
]
])
1
2
3
4
5
Index replica_index = client . getIndex ( "products_price_desc" );
replica_index . setSettings ( new JSONObject (). put ( "customRanking" , new JSONArray ()
. put ( "desc(price)" )
);
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
SearchIndex replica_index = client . InitIndex ( "products_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_price_desc" , Contact . class );
// Asynchronous
AsyncIndex < Contact > replica_index = client . initIndex ( "products_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_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 {
changeSettings of "products_price_desc" `with` IndexSettings (
customRanking = Some ( Seq (
Ranking . desc ( "price" )
))
)
}
Using the Dashboard
Creating a replica
Go to the Indices section of the dashboard and select your index.
Click the Replicas tab.
Click the Create Replica Index button.
In the modal that appears, enter a name for your replica, choose Standard Replicas or Virtual Replicas , and select Create replica .
Repeat for each replica you want to add.
Don’t forget to save your changes.
Changing replica settings
Changing standard replica settings
Go to the Indices section of the dashboard and select your replica index.
Click the Configuration tab.
In the Ranking and Sorting section, use the + Add sort-by attribute button to add the attribute you want to sort by. Then select either Ascending or Descending to the right of the attribute name.
Don’t forget to save your changes.
Changing virtual replica settings
Go to the Indices section of the dashboard and select your replica index.
Click the Configuration tab.
In the Relevant sort section, use the + Add a sort attribute button to add the attribute you want to sort by. Then select either Ascending or Descending to the right of the attribute name.
Don’t forget to save your changes.