A replica index is a copy of your primary index that sorts results in alternative ways.
For instance, on an ecommerce site, you might want to sort search results by default from cheapest to most expensive.
You also offer a drop-down menu to let users sort from most expensive to cheapest.
If you’re using InstantSearch, use the sort-by widget to let users choose their sorting strategy.
Create replica indices
Let users sort results in various ways by creating a replica for each sorting strategy.
That sorting strategy might order results by price, date, relevance, or any other criteria you provide.
Switch index as required
When users change the sorting strategy, ensure you switch to the appropriate index (primary or replica).
// 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 eventssort_by_price=false# 2. Get the index name based on sort_by_priceindex_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 eventsconstsortByPrice=false;// 2. Get the index name based on sortByPriceconstindexName=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"ifsort_by_priceelse"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 eventsletsortByprice=false// 2. Get the index name based on sortByPriceletindexName=sortByprice?"products_price_desc":"products";// 3. Search on dynamic index name (primary or replica)letindex=client.index(withName:indexName)index.search(Query(query:"phone")){(result,error)in// use result and error}
// 1. Change the sort dynamically based on the UI eventsboolsortByprice=false;// 2. Get the index name based on sortByPricestringindexName=sortByprice?"products_price_desc":"products";// 3. Search on dynamic index name (primary or replica)SearchIndexindex=client.InitIndex(indexName);index.Search(newQuery("phone"));
1
2
3
4
5
6
7
8
9
// 1. Change the sort dynamically based on the UI eventsbooleansortByprice=false;// 2. Get the index name based on sortByPriceStringindexName=sortByprice?"products_price_desc":"products";// 3. Search on dynamic index name (primary or replica)SearchIndexindex=client.initIndex(indexName);index.search(newQuery("phone"));
1
2
3
4
5
6
7
8
9
10
11
12
13
// 1. Change the sort dynamically based on UI eventssortByPrice:=false// 2. Get the index name based on sortByPricevarindexNamestringifsortByPrice{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 eventsvalsortByPrice=false// 2. Get the index name based on sortByPricevalindexName=if(sortByPrice)"products_price_desc"else"products"// 3. Search on dynamic index name (primary or replica)client.execute{searchintoindexNamequeryQuery(query=Some("phone"),)}