How bucket sort works in Meilisearch
When you search, Meilisearch doesn’t score documents with a single number. Instead, it applies ranking rules sequentially, sorting documents into buckets at each step.Example: Searching for “Badman dark knight returns”
Step 1: Apply thewords rule
The first ranking rule (words) sorts documents by how many query words they contain:
| Bucket | Matches | Documents |
|---|---|---|
| 1 | All 4 words | ”Batman: The Dark Knight Returns” |
| 2 | 3 words | ”Batman: The Dark Knight” |
| 3 | 2 words | … |
| 4 | 1 word | ”Angel and the Badman” |
typo rule within buckets
If a bucket contains multiple documents, the next rule (typo) breaks ties. For example, in the 1-word bucket where “Badman” appears:
| Bucket | Typos | Documents |
|---|---|---|
| 4.1 | 0 typos | Documents containing “Badman” exactly |
| 4.2 | 1 typo | Documents corrected “Badman” → “Batman” |
Why bucket sort?
Bucket sort offers several advantages for search ranking:- Flexibility: Different sorting algorithms can be applied within individual buckets
- Configurable priority: You control which criteria matter most by reordering rules
- Efficient tie-breaking: Only documents that tie on one rule need evaluation by the next
Best and worst cases
| Case | Condition | Complexity |
|---|---|---|
| Best | Documents spread evenly across buckets | O(n+k) where n=documents, k=buckets |
| Worst | All documents in one bucket | Depends on the sorting algorithm used |
Default ranking rules
Meilisearch applies these rules in order:- words: Documents containing more query words rank higher
- typo: Documents with fewer typos rank higher
- proximity: Documents where query words appear closer together rank higher
- attributeRank: Documents matching in more important attributes rank higher
- sort: User-defined sort order (if specified)
- wordPosition: Documents with matches closer to the beginning of an attribute rank higher
- exactness: Documents with exact matches rank higher
Customizing ranking rules
You can reorder, add, or remove ranking rules:release_date:desc as a custom rule means newer movies rank higher when all other factors are equal.
Related concepts
- Ranking rules: Configure ranking behavior
- Ranking score: Understanding search relevance scores
- Custom ranking rules: Add business logic to ranking