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).
namespaceAlgolia;usingSystem;usingSystem.Collections.Generic;usingSystem.Net.Http;usingSystem.Text.Json;usingAlgolia.Search.Clients;usingAlgolia.Search.Http;usingAlgolia.Search.Models.Search;classSearchInReplicaIndex{asyncTaskMain(string[]args){varclient=newSearchClient(newSearchConfig("ALGOLIA_APPLICATION_ID","ALGOLIA_API_KEY"));varquery="query";// 1. Change the sort dynamically based on the UI eventsvarsortByPrice=false;// 2. Get the index name based on sortByPricevarindexName=sortByPrice?"products_price_desc":"products";// 3. Search on dynamic index name (primary or replica)awaitclient.SearchSingleIndexAsync<Hit>(indexName,newSearchParams(newSearchParamsObject{Query="query"}));}}
import'package:algolia_client_search/algolia_client_search.dart';voidsearchInReplicaIndex()async{finalclient=SearchClient(appId:'ALGOLIA_APPLICATION_ID',apiKey:'ALGOLIA_API_KEY');// 1. Change the sort dynamically based on the UI eventsfinalsortByPrice=false;// 2. Get the index name based on sortByPrice// ignore: dead_codefinalindexName=sortByPrice?"products_price_desc":"products";// 3. Search on dynamic index name (primary or replica)awaitclient.searchSingleIndex(indexName:indexName,searchParams:SearchParamsObject(query:"query",),);}
packagemainimport"github.com/algolia/algoliasearch-client-go/v4/algolia/search"funcsearchInReplicaIndex(){client,err:=search.NewClient("ALGOLIA_APPLICATION_ID","ALGOLIA_API_KEY")iferr!=nil{// The client can fail to initialize if you pass an invalid parameter.panic(err)}// 1. Change the sort dynamically based on the UI eventssortByPrice:=false// 2. Get the index name based on sortByPriceindexName:="products"ifsortByPrice{indexName="products_price_desc"}// 3. Search on dynamic index name (primary or replica)_,err=client.SearchSingleIndex(client.NewApiSearchSingleIndexRequest(indexName).WithSearchParams(search.SearchParamsObjectAsSearchParams(search.NewEmptySearchParamsObject().SetQuery("query"))))iferr!=nil{panic(err)}}
packagecom.algolia;importcom.algolia.api.SearchClient;importcom.algolia.config.*;importcom.algolia.model.search.*;publicclasssearchInReplicaIndex{publicstaticvoidmain(String[]args)throwsException{try(SearchClientclient=newSearchClient("ALGOLIA_APPLICATION_ID","ALGOLIA_API_KEY");){Stringquery="query";// 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)client.searchSingleIndex(indexName,newSearchParamsObject().setQuery("query"),Hit.class);}catch(Exceptione){System.out.println("An error occurred: "+e.getMessage());}}}
1
2
3
4
5
6
7
8
9
10
11
12
import{algoliasearch}from'algoliasearch';constclient=algoliasearch('ALGOLIA_APPLICATION_ID','ALGOLIA_API_KEY');// 1. Change the sort dynamically based on the 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)awaitclient.searchSingleIndex({indexName:indexName,searchParams:{query:'query'}});
importcom.algolia.client.api.SearchClientimportcom.algolia.client.configuration.*importcom.algolia.client.transport.*importcom.algolia.client.extensions.*importcom.algolia.client.model.search.*suspendfunsearchInReplicaIndex(){valclient=SearchClient(appId="ALGOLIA_APPLICATION_ID",apiKey="ALGOLIA_API_KEY")// 1. Change the sort dynamically based on the 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.searchSingleIndex(indexName=indexName,searchParams=SearchParamsObject(query="query",),)}
<?phprequire__DIR__.'/../vendor/autoload.php';useAlgolia\AlgoliaSearch\Api\SearchClient;$client=SearchClient::create('ALGOLIA_APPLICATION_ID','ALGOLIA_API_KEY');$query='query';// 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->searchSingleIndex($indexName,['query'=>'query',],);
fromalgoliasearch.search.clientimportSearchClientSync_client=SearchClientSync("ALGOLIA_APPLICATION_ID","ALGOLIA_API_KEY")query="query"# 1. Change the sort dynamically based on the UI events
sort_by_price=False# 2. Get the index name based on sortByPrice
index_name="products_price_desc"ifsort_by_priceelse"products"# 3. Search on dynamic index name (primary or replica)
_client.search_single_index(index_name=index_name,search_params={"query":"query",},)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
require"algolia"client=Algolia::SearchClient.create("ALGOLIA_APPLICATION_ID","ALGOLIA_API_KEY")query="query"# 1. Change the sort dynamically based on the UI eventssort_by_price=false# 2. Get the index name based on sortByPriceindex_name=sort_by_price?"products_price_desc":"products"# 3. Search on dynamic index name (primary or replica)client.search_single_index(index_name,Algolia::Search::SearchParamsObject.new(query: "query"))
importscala.concurrent.ExecutionContext.Implicits.globalimportscala.concurrent.{Await,Future}importscala.concurrent.duration.Durationimportalgoliasearch.api.SearchClientimportalgoliasearch.config.*importalgoliasearch.extension.SearchClientExtensionsimportalgoliasearch.search.SearchParamsObjectdefsearchInReplicaIndex():Future[Unit]={valclient=SearchClient(appId="ALGOLIA_APPLICATION_ID",apiKey="ALGOLIA_API_KEY")// 1. Change the sort dynamically based on the UI eventsvalsortByPrice=false// 2. Get the index name based on sortByPricevalindexName=ifsortByPricethen"products_price_desc"else"products"// 3. Search on dynamic index name (primary or replica)client.searchSingleIndex(indexName=indexName,searchParams=Some(SearchParamsObject(query=Some("query")))).map{response=>println(response)}.recover{caseex:Exception=>println(s"An error occurred: ${ex.getMessage}")}}
importFoundation#if os(Linux) // For linux interopimportFoundationNetworking#endifimportCoreimportSearchfuncsearchInReplicaIndex()asyncthrows{letclient=trySearchClient(appID:"ALGOLIA_APPLICATION_ID",apiKey:"ALGOLIA_API_KEY")// 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)letresponse:SearchResponse<Hit>=tryawaitclient.searchSingleIndex(indexName:indexName,searchParams:SearchSearchParams.searchSearchParamsObject(SearchSearchParamsObject(query:"query")))print(response)}