Configure filterable and sortable attributes
Before using filters and sorting together, you must add the relevant attributes to bothfilterableAttributes and sortableAttributes. An attribute used only in filters does not need to be sortable, and an attribute used only for sorting does not need to be filterable.
Suppose you have a movies index with documents like this:
genres is filterable and rating is sortable:
Filter and sort in a single request
Once your settings are configured, pass bothfilter and sort in the same search request:
q parameter is set to an empty string, making this a placeholder search that returns all matching documents.
The response looks like this:
Combine multiple filters with sort
You can useAND, OR, and NOT operators to build complex filter expressions:
Combine geo filter with text search and sort
If your documents have_geo data, you can combine geo search filtering with text search and sorting. For example, find restaurants near a specific location and sort them by rating:
_geo is in filterableAttributes and rating is in sortableAttributes.
Sort by multiple attributes
You can sort by more than one attribute. Meilisearch uses the second sort criterion as a tiebreaker when documents have the same value for the first:Key points
- Fields used in
filtermust be infilterableAttributes - Fields used in
sortmust be insortableAttributes - A field can appear in both settings lists if you need to both filter and sort by it
- Filters narrow the result set before sorting is applied
- When combining with a text query, Meilisearch first applies the text relevancy ranking rules, then uses
sortas an additional ranking rule
Next steps
Filter search results
Learn the basics of configuring and using filters
Sort search results
Learn more about sorting configuration and options
Build faceted navigation
Add an interactive faceted sidebar to your search