> ## Documentation Index
> Fetch the complete documentation index at: https://algolia.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Relevant sorting

> Learn about the relevant sorting feature and how you can use it to provide more relevant results for users.

export const SearchQuery = () => <Tooltip tip="The text users enter into a search box. In the Search API, this corresponds to the query parameter. A search query is often used with filters, facets, and other parameters, but these aren't part of the query text itself.">
    search query
  </Tooltip>;

export const Records = () => <Tooltip tip="A record is a searchable object in an Algolia index. Each record consists of named attributes." cta="Algolia records" href="/doc/guides/sending-and-managing-data/prepare-your-data#algolia-records">
    records
  </Tooltip>;

export const Index = () => <Tooltip tip="An Algolia index is a searchable dataset that consists of records and configuration settings. These settings define how the records are searched and ranked.">
    index
  </Tooltip>;

export const Facet = () => <Tooltip tip="An attribute in your records that lets users filter or group results (for example, by color, brand, or price)." cta="Faceting" href="/doc/guides/managing-results/refine-results/faceting">
    facet
  </Tooltip>;

Use relevant sorting to only show the most relevant results for a user's search instead of displaying all <Records /> (exhaustive sorting).

<Callout icon="credit-card" color="#c084fc">
  This feature isn't available on every plan.
  Refer to your [pricing plan](https://www.algolia.com/pricing) to see if it's included.
</Callout>

## Examples

These examples illustrate the difference between [exhaustive](/doc/guides/managing-results/refine-results/sorting/in-depth/exhaustive-sort) and relevant sorting.

Exhaustive sorting returns and sorts all results that match the search term `iphone`:
here, a list of phone cases appear at the top of the results.

For the same search term, relevant sorting has applied a virtual replica's [custom ranking](/doc/guides/managing-results/must-do/custom-ranking) criteria to ensure that iPhone devices appear at the top of the results.

### Exhaustive sorting

<img src="https://mintcdn.com/algolia/uAYFrBCMSmYQz381/images/guides/sorting/hard-sort.jpg?fit=max&auto=format&n=uAYFrBCMSmYQz381&q=85&s=55dc34a6c30ac3e9cf65362f1ff0204c" alt="Screenshot of a search results page showing 'Exhaustive sort - Lowest price' with three Dynex™ iPhone cases listed, each with a price of $2.99." width="1280" height="720" data-path="images/guides/sorting/hard-sort.jpg" />

### Relevant sorting

<img src="https://mintcdn.com/algolia/uAYFrBCMSmYQz381/images/guides/sorting/relevant-sort.jpg?fit=max&auto=format&n=uAYFrBCMSmYQz381&q=85&s=72398ebf28939d7b8abbb3140c8c5145" alt="Screenshot of a search results page for 'iPhone' showing 63 relevant results sorted by 'Relevant sort - Lowest price' with phone listings." width="1280" height="720" data-path="images/guides/sorting/relevant-sort.jpg" />

## Effect of relevant sorting on the ranking formula

Algolia's [ranking formula](/doc/guides/managing-results/relevance-overview/in-depth/ranking-criteria) isn't overridden when you use relevant sorting. Instead, the relevant sorting algorithm:

1. Sorts results using the ranking formula of the primary <Index />.
2. Selects the most relevant results. You can determine how strict this should be by adjusting [`relevancyStrictness`](/doc/api-reference/api-parameters/relevancyStrictness) on a [virtual replica](/doc/guides/managing-results/refine-results/sorting/in-depth/replicas#standard-and-virtual-replicas).
3. Orders the selected results according to the primary index's [`sortBy`](/doc/guides/managing-results/refine-results/sorting/how-to/sort-by-attribute) attributes (if present) and the virtual replica's [`customRanking`](/doc/api-reference/api-parameters/customRanking).
4. Settle remaining ties according to the [filter score](/doc/guides/managing-results/refine-results/filtering/in-depth/filter-scoring).
5. Returns the relevant results.

The ranking formula determines which results are most relevant to the <SearchQuery />,
but custom ranking determines the order of results.
This means that parameters that affect the ranking formula don't affect result ordering.

If multiple results have the same value for a sorting attribute,
the other sorting attributes are used to decide which one comes first.
For example, if you sort according to price and then popularity,
and three results have the same cost,
the engine will prioritize them according to their popularity score.

For more information, see:

* [Sort by attribute](/doc/guides/managing-results/refine-results/sorting/how-to/sort-by-attribute)
* [Sort an index by date](/doc/guides/managing-results/refine-results/sorting/how-to/sort-an-index-by-date)

## Relevant sorting considerations

Relevant sorting interacts with other features in ways you might not expect.

### Other re-ranking features can affect results

Other re-ranking features, such as rules, apply after  relevant sorting and will modify sort order.
For example, if [Personalization](/doc/guides/personalization/classic-personalization/what-is-personalization) promotes expensive items for a particular user based on their past actions,
that user might see expensive items at the top of the results, even if they've chosen to *relevant sort* from lowest to highest price.

To ensure predictable behavior, don't use the following features or parameters with relevant sorting:

* [`distinct`](/doc/api-reference/api-parameters/distinct) > 1
* [Dynamic Re-ranking](/doc/guides/algolia-ai/re-ranking)
* [Personalization](/doc/guides/personalization/classic-personalization/personalizing-results)

### Facets count

When relevant sorting is enabled, Algolia still computes each <Facet /> on all results, not just those defined by the relevant sort.
A [UI banner](/doc/guides/managing-results/rules/merchandising-and-promoting/how-to/add-banners) detailing the behavior of the relevant sort may help manage user expectations.

### Enabling relevant sorting reduces the number of results

Essentially, that's the point: it gives users more relevant results, not all results ([exhaustive sorting](/doc/guides/managing-results/refine-results/sorting/in-depth/exhaustive-sort)).

For example, you search for `chromebook` on an ecommerce site and get Chromebook computers at the top of your results (so far, so good).

However, if you then sort by ascending price (because you want a cheaper computer),
the top (cheapest) listed items might be "Chromebook power adapters".
With a relevant sort, you get fewer results but only see the relevant items (computers).

Using a virtual replica even with `relevancyStrictness` set to 0 can't mimic the exhaustive sorting of a standard replica because it doesn't use the same algorithm. Be aware that a virtual replica:

* Doesn't duplicate the data. It sorts at query time.
* Can only retrieve a maximum of 20k results, so there's no guarantee that it will fetch the "deepest" hits.

<Tip>
  When using virtual replicas, use `nbSortedHits` rather than `nbHits` to get the number of results.
</Tip>

### Relevant sort doesn't apply when browsing an index

When you use the [Browse](/doc/libraries/sdk/methods/search/browse) method on a virtual replica index, records are returned in the same order as the primary index.
This means that relevant sort parameters, such as [relevancy strictness](/doc/api-reference/api-parameters/relevancyStrictness), have no effect.

## How to configure relevant sorting

### Create a virtual replica index

If you want to configure relevant sorting,
you first need to create a [virtual replica index](/doc/guides/managing-results/refine-results/sorting/in-depth/replicas#standard-and-virtual-replicas) for each attribute you want to sort by.

Virtual replicas provide an alternative "view" of the primary index.
Virtual replicas reuse the primary's ranking formula to return relevant results.
For that reason, you can't change the [`ranking`](/doc/api-reference/api-parameters/ranking)
on virtual replicas but can use [`customRanking`](/doc/api-reference/api-parameters/customRanking)
to define the sort criteria.

For more information, see:

* [Understanding replicas](/doc/guides/managing-results/refine-results/sorting/in-depth/replicas)
* [What is the effect of a replica on the number of records?](/doc/guides/managing-results/refine-results/sorting/in-depth/replicas-impact-on-pricing)

### Connect your UI to your index

Virtual replica indices manage the backend of sorting, but you still need to implement the frontend. You can do this with either custom logic or [InstantSearch](/doc/guides/building-search-ui/what-is-instantsearch/js).
If you're using InstantSearch, you should use the [`sortBy` UI widget](/doc/guides/managing-results/refine-results/sorting/how-to/use-sorting-ui-widget).

<img src="https://mintcdn.com/algolia/uAYFrBCMSmYQz381/images/guides/sorting/sortby-widget.png?fit=max&auto=format&n=uAYFrBCMSmYQz381&q=85&s=b85befa7f2afb5b15acd7d4972d61946" alt="Screenshot of a 'SortBy' drop-down menu showing options: 'Relevance', 'Price ascending', and 'Price desc.'." width="2478" height="756" data-path="images/guides/sorting/sortby-widget.png" />

To ensure users understand how relevant sort works and how they can control their experience:

* Indicate that sorting removed some results
* Offer them the option of displaying more.

You can do this with the `relevantSort` widget:

<img src="https://mintcdn.com/algolia/uAYFrBCMSmYQz381/images/guides/sorting/relevant-sort-widget.jpg?fit=max&auto=format&n=uAYFrBCMSmYQz381&q=85&s=14f2703649e1d90d86fea3f77edb7e16" alt="Screenshot of a search interface with 'iPhone' in the search box, showing 63 relevant results sorted by 'Relevant sort - Lowest price'." width="1280" height="319" data-path="images/guides/sorting/relevant-sort-widget.jpg" />

For more details, see:

* [`sortBy` widget](/doc/api-reference/widgets/sort-by/js)
* [`relevantSort` widget](/doc/api-reference/widgets/relevant-sort/js)
