Using replicas with different sorting strategies allows you to provide a way for users to change the sorting on the front end. For example, imagine you have an ecommerce website for which you sort the products, by default, from cheapest to most expensive. You might want to provide a dropdown or toggle switch to let users sort from most expensive to cheapest.
Because you must have one replica index per sorting strategy, you need to change the index Algolia searches into when the user changes the sorting order.
Switching Index
Before you implement the search, you need to have replicas for each sorting strategy you want to provide. See our how-to guide on creating replicas if you haven’t set up replicas yet.
Once you have replicas set up, you need to initialize all indices you want to search into (primary and replicas), and switch to the right index based on which sorting strategy the user has selected.
This logic is the same for both virtual and standard replicas.
Using InstantSearch for web? You might want to use our built-in sortBy
widget instead.
1
2
3
4
5
6
7
8
| // 1. Change the sort dynamically based on the UI events
$sortByPrice = false;
// 2. Get the index name based on sortByPrice
$indexName = $sortByPrice ? 'products_price_desc' : 'products';
// 3. Search on dynamic index name (primary or replica)
$client->initIndex($indexName)->search('phone');
|
1
2
3
4
5
6
7
8
| # 1. Change the sort dynamically based on UI events
sort_by_price = false
# 2. Get the index name based on sort_by_price
index_name = sortByPrice ? 'products_price_desc' : 'products'
# 3. Search for 'phone' on dynamic index name (primary or replica)
client.init_index(indexName).search('phone')
|
1
2
3
4
5
6
7
8
9
10
| // 1. Change the sort dynamically based on UI events
const sortByPrice = false;
// 2. Get the index name based on sortByPrice
const indexName = sortByPrice ? 'products_price_desc' : 'products';
// 3. Search on dynamic index name (primary or replica)
client.initIndex(indexName).search('phone').then(({ hits }) => {
console.log(hits);
});
|
1
2
3
4
5
6
7
8
| # 1. Change the sort dynamically based on the UI events
sort_by_price = False
# 2. Get the index name based on sort_by_price
index_name = "products_price_desc" if sort_by_price else "products"
# 3. Search on dynamic index name (primary or replica)
client.init_index(index_name).search('phone');
|
1
2
3
4
5
6
7
8
9
10
11
| // 1. Change the sort dynamically based on the UI events
let sortByprice = false
// 2. Get the index name based on sortByPrice
let indexName = sortByprice ? "products_price_desc" : "products";
// 3. Search on dynamic index name (primary or replica)
let index = client.index(withName: indexName)
index.search(Query(query: "phone")) { (result, error) in
// use result and error
}
|
1
2
3
4
5
6
7
8
9
| // 1. Change the sort dynamically based on the UI events
bool sortByprice = false;
// 2. Get the index name based on sortByPrice
string indexName = sortByprice ? "products_price_desc" : "products";
// 3. Search on dynamic index name (primary or replica)
SearchIndex index = client.InitIndex(indexName);
index.Search(new Query("phone"));
|
1
2
3
4
5
6
7
8
9
| // 1. Change the sort dynamically based on the UI events
boolean sortByprice = false;
// 2. Get the index name based on sortByPrice
String indexName = sortByprice ? "products_price_desc" : "products";
// 3. Search on dynamic index name (primary or replica)
SearchIndex index = client.initIndex(indexName);
index.search(new Query("phone"));
|
1
2
3
4
5
6
7
8
9
10
11
12
13
| // 1. Change the sort dynamically based on UI events
sortByPrice := false
// 2. Get the index name based on sortByPrice
var indexName string
if sortByPrice {
indexName = "products_price_desc"
} else {
indexName = "products"
}
// 3. Search on dynamic index name (primary or replica)
client.InitIndex(indexName).Search("phone")
|
1
2
3
4
5
6
7
8
9
10
11
12
| // 1. Change the sort dynamically based on UI events
val sortByPrice = false
// 2. Get the index name based on sortByPrice
val indexName = if (sortByPrice) "products_price_desc" else "products"
// 3. Search on dynamic index name (primary or replica)
client.execute {
search into indexName query Query(
query = Some("phone"),
)
}
|
1
2
3
4
5
| val sortByPrice = false
val indexName = if (sortByPrice) "products_price_desc" else "products"
val index = client.initIndex(IndexName(indexName))
index.search(Query("phone"))
|