API Reference / API Parameters / ranking
Jan. 07, 2019
Type: list of strings
Engine default: [“typo”, “geo”, “words”, “filters”, “proximity”, “attribute”, “exact”, “custom”]
Parameter syntax
'ranking' => [
  // the `asc` and `desc` modifiers must be placed at the top
  // if you are configuring an index for sorting purposes only
  'asc(attribute1)',
  'desc(attribute2)',
  'typo',
  'geo',
  'words',
  'filters',
  'proximity',
  'attribute',
  'exact',
  'custom'
]

Can be used in these methods:

About this parameter

Controls the way results are sorted.

You must specify a list of ranking criteria. Each criterion will be applied in sequence by the tie-breaking algorithm in the order they are specified.

Ranking Criteria:

  • typo: Sort by increasing number of typos.

  • geo: Sort by decreasing geo distance when performing a geo search. This criterion is ignored when not performing a geo search.

  • words: Sort by decreasing number of matched query words. This parameter is useful when you use the optionalWords query parameter to rank hits with the most matched words first.

  • filters: The filter criteria is the sum of scores for filters matched by one hit. In case of OR filters, only one score is taken in account even if the two filters match. This parameter must be present in the ranking if you want to use optionalFilters.

  • proximity: Sort by increasing proximity of query words in hits.

  • attribute: Sort according to the order of attributes defined by searchableAttributes.

  • exact:

    • If the query contains only one word: The behavior depends on the value of exactOnSingleWordQuery.
    • If the query contains multiple words: Sort by decreasing number of words that matched exactly.
  • custom:

    • Sort according to a user-defined formula specified via the customRanking setting.
    • Note: If custom is not mentioned then you customRanking will not be taken into account.

Modifiers:

  • asc: Sort by increasing value of the attribute.

  • desc: Sort by decreasing value of the attribute.

Examples

Set the default ranking

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

Sort index by an attribute in ascending order (Sort by price in this example)

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 order (Sort by price in this example)

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?