API Reference / API Parameters / facets
Feb. 26, 2019
Type: list of strings
Engine default: [] (no facets retrieved)
Parameter syntax
'facets' => [
  'attribute',
  ...
]

Can be used in these methods:

About this parameter

Facets to retrieve.

The effect of this setting: For each of the retrieved facets (eg. color; size), the response will contain a list of facet values (blue, red; small, large, …) in objects matching the current query. Each value will be returned with its associated count (number of matched objects containing that value).

The maximum number of facet values returned depends on the maxValuesPerFacet setting. The default is 100 and the max is 1000.

By default, the returned values are sorted by frequency, but this can be changed to alphabetical with sortFacetValuesBy.

Usage notes:

  • Facets must have been declared beforehand in the attributesForFaceting index setting.

  • Faceting does not filter your results. If you want to filter results, you should use filters.

  • Default/Empty list: If not specified or empty, no facets are retrieved. The special value * may be used to retrieve all facets.

  • Approximating facet counts: If the number of hits is high, facet counts may be approximate. The response field exhaustiveFacetsCount is true when the count is exact.

Examples

Only retrieve faceting data for some attributes

For this example, let’s consider that attributesForFaceting are set to: ["category", "author", "nb_views", "nb_downloads"] but, for the current search, you want to retrieve facet values only for category and author.

1
2
3
$results = $index->search('query', [
  'facets' => ['category', 'author']
]);

Did you find this page helpful?