Search in a Replica Index

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 e-commerce website, and by default, you want to sort search results 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 have one replica index per sorting strategy, you need to change what index Algolia must search into when the user changes the sorting.

Switching Index

Before you implement the search, you need to have replicas for each sorting strategy you want to provide. See Creating Replicas if you haven’t yet.

Once you have replicas set up, you need to initialize all indices you may want to search into (primary and replicas), and switch to the right index to search on depending on what the user selected.

Using InstantSearch for web? You might want to use our built-in sortBy widget instead.

// 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)

Did you find this page helpful?