Configure facet index settings
First, create a new index using this books dataset. Documents in this dataset have the following fields:genres, language, and rating to the list of filterableAttributes:
Use facets in a search query
Make a search query setting thefacets search parameter:
facetDistribution and facetStats:
facetDistribution lists all facets present in your search results, along with the number of documents returned for each facet.
facetStats contains the highest and lowest values for all facets containing numeric values.
Sorting facet values
By default, all facet values are sorted in ascending alphanumeric order. You can change this using thesortFacetValuesBy property of the faceting index settings:
genres facet by descending value count.
Repeating the previous query using the new settings will result in a different order in facetsDistribution:
Searching facet values
You can also search for facet values with the facet search endpoint:genres facet for values starting with c:
The response contains a facetHits array listing all matching facets, together with the total number of documents that include that facet:
q, filter, and matchingStrategy parameters. Learn more about them in the API reference.
Toggle facet search globally
By default, the facet search endpoint is enabled for all indexes. If you do not need facet search and want to speed up indexing, you can disable it with thefacetSearch index setting:
facetSearch to false disables the /indexes/{index_uid}/facet-search endpoint for this index. Documents are still indexed for regular facet distribution, but Meilisearch skips the additional processing needed for facet search, resulting in faster indexing. To re-enable facet search, send the same request with true.
Get exact facet counts
By default, the facet counts returned by the facet search endpoint are estimates. This is faster but may not be perfectly accurate for large datasets. To get exact facet counts, set theexhaustiveFacetCount parameter to true in your facet search request: