Guides / Managing results / Refine results / Sorting results

Search in a replica index

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
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
namespace Algolia;

using System;
using System.Collections.Generic;
using System.Net.Http;
using System.Text.Json;
using Algolia.Search.Clients;
using Algolia.Search.Http;
using Algolia.Search.Models.Search;

class SearchInReplicaIndex
{
  async Task Main(string[] args)
  {
    var client = new SearchClient(new SearchConfig("ALGOLIA_APPLICATION_ID", "ALGOLIA_API_KEY"));

    var query = "query";

    // 1. Change the sort dynamically based on the UI events
    var sortByPrice = false;

    // 2. Get the index name based on sortByPrice
    var indexName = sortByPrice ? "products_price_desc" : "products";

    // 3. Search on dynamic index name (primary or replica)
    await client.SearchSingleIndexAsync<Hit>(
      indexName,
      new SearchParams(new SearchParamsObject { Query = "query" })
    );
  }
}
Did you find this page helpful?