API Reference / API Parameters / ranking
Type: list of strings
Engine default: ["typo", "geo", "words", "filters", "proximity", "attribute", "exact", "custom"]
Parameter syntax
'ranking' => [
  // Place the `asc` and `desc` modifiers at the top
  // if you're configuring an index just for sorting
  'asc(attribute1)',
  'desc(attribute2)',
  'typo',
  'geo',
  'words',
  'filters',
  'proximity',
  'attribute',
  'exact',
  'custom'
]

Can be used in these methods:

About this parameter

Controls how Algolia sorts your results.

The tie-breaking algorithm sequentially applies ranking criteria in the order specified.

You can change the order of the default criteria but you shouldn’t. The out-of-the-box ranking order works for most uses.

However, if you do decide to change ranking, apply it with the setSettings method (attribute names are case-sensitive) or the Algolia dashboard:

  1. Go to the Algolia dashboard and select your Algolia application.
  2. On the left sidebar, select Algolia Search Search.
  3. Select your Algolia index:

    Select your Algolia application and index

  4. On the Configuration tab, select Ranking and Sorting.
  5. Click Add sort-by attribute and type or select your attributes.
  6. Expand the textual ranking criteria
  7. Drag the ranking criterion to the appropriate location
  8. Save your changes.

Before changing this parameter, consider running an A/B test to ensure your changes will positively affect results.

Ranking criteria

typo

Sort results by the fewest spelling mistakes.

geo

Sort results by increasing distance. This option only applies when carrying out a geographical search.

words

Sort results by the number of matching query words, starting with the most matches. This option only applies if you’re using optionalWords.

filters

Sort results by filter score. This option is essential if you want to use optionalFilters.

proximity

Sort results by how close the query words are to each other.

attribute

Sort results based on the order of attributes you set in searchableAttributes.

exact

  • For a single-word query, sorting depends on the exactOnSingleWordQuery parameter
  • For multiple words, sort results by the number of query words that match exactly.

custom

  • Sort according to the customRanking parameter.
  • If custom isn’t defined, customRanking is ignored.

Modifiers

asc

Sort in ascending order (lowest to highest).

desc

Sort in descending order (highest to lowest).

Examples

Set ranking

This example sets the ranking criteria for an index. It’s slightly different to Algolia’s default ranking since it swaps the order of attribute and proximity.

1
2
3
4
5
6
7
8
9
10
11
12
$index->setSettings([
  'ranking' => [
    'typo',
    'geo',
    'words',
    'filters',
    'attribute',
    'proximity',
    'exact',
    'custom'
  ]
]);

Sort index by an attribute in ascending price order

This example modifies the default ranking criteria to first sort records from low to high price. To do this, it:

1
2
3
4
5
6
7
8
9
10
11
12
13
$index->setSettings([
  'ranking' => [
    'asc(price)',
    'typo',
    'geo',
    'words',
    'filters',
    'proximity',
    'attribute',
    'exact',
    'custom'
  ]
]);

Sort index by an attribute in descending price order

Override the default ranking criteria to sort records from high to low price:

This is the same as the preceding example but using a desc sort on price.

1
2
3
4
5
6
7
8
9
10
11
12
13
$index->setSettings([
  'ranking' => [
    "desc(price)",
    "typo",
    "geo",
    "words",
    "filters",
    "proximity",
    "attribute",
    "exact",
    "custom"
  ]
});
Did you find this page helpful?