Guides / Managing results / Refine results / Sorting results

Search in a replica index

Using replicas with different sorting strategies lets you provide a way for users to change the sorting on the frontend. For example, if you have an ecommerce website, and by default, want to sort search results from cheapest to most expensive. You might want to provide a drop-down menu or toggle switch to let users sort from most expensive to cheapest.

Because you must have one replica index per sorting strategy, you must change the index Algolia searches into when users change the sorting order.

Switching index

Before you implement the search, you must create replicas for each sorting strategy you want to provide.

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 users have selected.

This logic is the same for both virtual and standard replicas.

Using InstantSearch for web? You might want to use the 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?