API Reference / API Parameters / sortFacetValuesBy
Type: string
Engine default: count
Parameter syntax
'sortFacetValuesBy' => 'count'|'alpha'

Can be used in these methods:

About this parameter

Controls how facet values are fetched.

When using facets, Algolia retrieves a list of matching facet values for each faceted attribute. This parameter controls in which order the facet values are retrieved within each faceted attribute. This parameter doesn’t control or impact how facet values are displayed on the UI. See facet value display to control how facet values should be displayed.

Usage notes:

  • You can either sort by count (the default, from high to low) or alphabetically

  • The set of returned facet values depends on the maximum number of facet values returned, which depends on the maxValuesPerFacet setting. The default is 100 and the max is 1000. Therefore, values with very low frequency could potentially not be returned.

  • For example:

    • With maxValuesPerFacet set to 2
    • With sortFacetValues set to alpha
    • And with the facet “brand” containing the values ["Amazon", "Apple", "Facebook", "Google"]
    • When using “facets” Algolia would retrieve the first 2 values in alphabetical order, so ["Amazon", "Apple"] if all values are present in the result set.

Options:

  • count: Facet values are sorted by decreasing count. The count is the number of records containing this facet value in the results of the query.

  • alpha: Facet values are sorted in alphabetical order, ascending from A to Z.

Examples

Set default sort order for facet values

1
2
3
$index->setSettings([
  'sortFacetValuesBy': 'alpha'
]);

Override default sort order for facet values at query time

1
2
3
$results = $index->search('query', [
  'sortFacetValuesBy' => "count"
]);

Did you find this page helpful?