Skip to content

Commit d223036

Browse files
authored
Merge pull request #3462 from meilisearch/introduce-attribute-rank-position
Introduce the `attributeRank` and `wordPosition` ranking rules
2 parents 0c3f79a + befd089 commit d223036

14 files changed

Lines changed: 101 additions & 54 deletions

.code-samples.meilisearch.yaml

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -160,8 +160,9 @@ update_settings_1: |-
160160
"words",
161161
"typo",
162162
"proximity",
163-
"attribute",
163+
"attributeRank",
164164
"sort",
165+
"wordPosition",
165166
"exactness",
166167
"release_date:desc",
167168
"rank:desc"
@@ -268,8 +269,9 @@ update_ranking_rules_1: |-
268269
"words",
269270
"typo",
270271
"proximity",
271-
"attribute",
272+
"attributeRank",
272273
"sort",
274+
"wordPosition",
273275
"exactness",
274276
"release_date:asc",
275277
"rank:desc"
@@ -1060,7 +1062,8 @@ sorting_guide_update_ranking_rules_1: |-
10601062
"sort",
10611063
"typo",
10621064
"proximity",
1063-
"attribute",
1065+
"attributeRank",
1066+
"wordPosition",
10641067
"exactness"
10651068
]'
10661069
sorting_guide_sort_nested_1: |-

assets/open-api/meilisearch-openapi-mintlify.json

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17534,10 +17534,18 @@
1753417534
},
1753517535
{
1753617536
"type": "string",
17537-
"description": "Documents with quey words contained in more important\nattributes are considered better.",
17538-
"enum": [
17539-
"Attribute"
17540-
]
17537+
"description": "Documents with query words contained in more important\nattributes are considered better.",
17538+
"enum": ["AttributeRank"]
17539+
},
17540+
{
17541+
"type": "string",
17542+
"description": "Documents with query words at the beginning of an attribute\nare considered better.",
17543+
"enum": ["WordPosition"]
17544+
},
17545+
{
17546+
"type": "string",
17547+
"description": "Legacy rule combining AttributeRank and WordPosition.\nDocuments with query words contained in more important\nattributes are considered better.",
17548+
"enum": ["Attribute"]
1754117549
},
1754217550
{
1754317551
"type": "string",

guides/relevancy/interpreting_ranking_scores.mdx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ Let's see how moving Sort **one position** in your ranking rules changes which r
9696
We’ve set up our ranking rules to have sort after our Group 1 wide net rules.
9797

9898
```json
99-
["words", "typo", "proximity", "sort", "attribute", "exactness"]
99+
["words", "typo", "proximity", "sort", "attributeRank", "wordPosition", "exactness"]
100100
```
101101

102102
With this set up Meilisearch evaluates the text relevance rules first, *then* uses Sort.
@@ -196,7 +196,7 @@ Also notice: Sort shows a `value` instead of a `score`. That's because Sort does
196196
Now let's move `sort` to the top of our ranking rules:
197197

198198
```json
199-
["sort", "words", "typo", "proximity", "attribute", "exactness"]
199+
["sort", "words", "typo", "proximity", "attributeRank", "wordPosition", "exactness"]
200200
```
201201

202202
### 🥇 Result #1 — Chicken Stew with Curry Spices and Vegetables

guides/relevancy/ordering_ranking_rules.mdx

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -37,12 +37,13 @@ This covers things like:
3737

3838
**These three rules cast a wide net and return lots of results.** That's good—you want to start broad and then narrow down, not the other way around. If you start too narrow you can lose relevancy easily.
3939

40-
**Group 2 - Fine-tuning : Exactness, Attribute (included in ranking score)**
40+
**Group 2 - Fine-tuning : Exactness, Attribute Rank, Word Position (included in ranking score)**
4141

4242
This covers things like:
4343

4444
- **Exactness**: Did the document match your whole search term or just pieces of it? Whole matches rank higher, especially when an entire field matches exactly or starts with your query. Documents containing extra content beyond the search term are ranked lower.
45-
- **Attribute**: Matches in your most important fields rank higher, and matches near the beginning of a field rank higher. You set field priority in `searchableAttributes`, with fields at the top of the list treated as the most important.
45+
- **Attribute Rank**: Matches in your most important fields rank higher. You set field priority in `searchableAttributes`, with fields at the top of the list treated as the most important.
46+
- **Word Position**: Matches near the beginning of a field rank higher.
4647

4748
**These are your fine-tuning filters.** They return fewer, more precise results. Use these after Group 1 rules to refine your large result set into something more precise.
4849

@@ -75,8 +76,9 @@ Ranking rules:
7576
"words",
7677
"typo",
7778
"proximity",
78-
"attribute",
79+
"attributeRank",
7980
"sort", // "price:asc" "author:desc" gets swapped in here
81+
"wordPosition",
8082
"exactness",
8183
"release_date:asc",
8284
"movie_ranking:desc"
@@ -99,7 +101,7 @@ These three rules cast a wide net and pass a large pool of relevant results thro
99101

100102
### Place Sort strategically
101103

102-
We recommend putting Sort after your Group 1 rules and before your Group 2 rules (Attribute, Exactness). This way, Meilisearch finds relevant results first and then uses your sort field to order documents that have similar text relevance, giving you a balance of match quality and sorting.
104+
We recommend putting Sort after your Group 1 rules and before your Group 2 rules (Attribute Rank, Word Position, Exactness). This way, Meilisearch finds relevant results first and then uses your sort field to order documents that have similar text relevance, giving you a balance of match quality and sorting.
103105

104106
If sorting matters more than text relevance for your use case - like an e-commerce price filter where users expect strict price ordering - move Sort higher. Just remember that Sort only activates when you include the `sort` parameter in your search query. Without it, the Sort rule has no effect.
105107

@@ -114,9 +116,9 @@ Place custom ranking rules at the end of your sequence. They work best for addin
114116
Each ranking rule has its own settings you can fine-tune beyond just ordering. For example, you can adjust which fields take priority in attribute ranking, or configure how aggressively typo tolerance matches similar words. If you want to dig into the specifics:
115117

116118
- [Built-in ranking rules](https://www.meilisearch.com/docs/learn/relevancy/ranking_rules#list-of-built-in-ranking-rules) — how each rule works and what it evaluates
117-
- [Attribute ranking order](https://www.meilisearch.com/docs/learn/relevancy/attribute_ranking_order) — controlling which fields matter most
119+
- [Attribute ranking order](https://www.meilisearch.com/docs/learn/relevancy/attribute_ranking_order) — controlling which fields matter most with `attributeRank` and `wordPosition`
118120
- [Typo tolerance settings](https://www.meilisearch.com/docs/learn/relevancy/typo_tolerance_settings) — adjusting how flexible matching behaves
119121

120122
**Want to see these rules in action?** In our next guide, [How Do I Interpret Ranking Score Details?](/guides/relevancy/interpreting_ranking_scores), we walk through a real example showing exactly how Meilisearch evaluates each rule — and how moving Sort one position can flip your results.
121123

122-
---
124+
---

learn/filtering_and_sorting/sort_search_results.mdx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,8 +83,9 @@ This is the default configuration of Meilisearch's ranking rules:
8383
"words",
8484
"typo",
8585
"proximity",
86-
"attribute",
86+
"attributeRank",
8787
"sort",
88+
"wordPosition",
8889
"exactness"
8990
]
9091
```

learn/relevancy/attribute_ranking_order.mdx

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,5 +38,7 @@ By default, nested fields share the same weight as their parent attribute. Use d
3838
With the above ranking order, `review.critic` becomes more important than its sibling `review.user` when calculating a document's ranking score.
3939

4040
<Note>
41-
The `attribute` rule's position in [`rankingRules`](/learn/relevancy/ranking_rules) determines how the results are sorted. Meaning, **if `attribute` is at the bottom of the ranking rules list, it will have almost no impact on your search results.**
41+
The `attributeRank` and `wordPosition` rules' positions in [`rankingRules`](/learn/relevancy/ranking_rules) determine how the results are sorted. Meaning, **if `attributeRank` is at the bottom of the ranking rules list, it will have almost no impact on your search results.**
42+
43+
The legacy `attribute` rule combines both `attributeRank` and `wordPosition`. If you use `attribute`, its position determines the impact of both attribute ranking order and position within attributes.
4244
</Note>

learn/relevancy/custom_ranking_rules.mdx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,9 @@ The following array includes all built-in ranking rules and places the custom ru
5151
"words",
5252
"typo",
5353
"proximity",
54-
"attribute",
54+
"attributeRank",
5555
"sort",
56+
"wordPosition",
5657
"exactness",
5758
"release_date:asc",
5859
"movie_ranking:desc"

learn/relevancy/ranking_rules.mdx

Lines changed: 27 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,16 @@ Built-in ranking rules are the core of Meilisearch's relevancy calculations.
1010

1111
## List of built-in ranking rules
1212

13-
Meilisearch contains six built-in ranking rules in the following order:
13+
Meilisearch contains seven built-in ranking rules in the following order:
1414

1515
```json
1616
[
1717
"words",
1818
"typo",
1919
"proximity",
20-
"attribute",
20+
"attributeRank",
2121
"sort",
22+
"wordPosition",
2223
"exactness"
2324
]
2425
```
@@ -47,21 +48,37 @@ Results are sorted by **increasing distance between matched query terms**. Retur
4748

4849
[It is possible to lower the precision of this ranking rule.](/reference/api/settings#proximity-precision) This may significantly improve indexing performance. In a minority of use cases, lowering precision may also lead to lower search relevancy for queries using multiple search terms.
4950

50-
## 4. Attribute
51+
## 4. Attribute rank
5152

5253
Results are sorted according to the **[attribute ranking order](/learn/relevancy/attribute_ranking_order)**. Returns documents that contain query terms in more important attributes first.
5354

54-
Also, note the documents with attributes containing the query words at the beginning of the attribute will be considered more relevant than documents containing the query words at the end of the attributes.
55+
This rule evaluates only the attribute ranking order and does not consider the position of matched words within attributes.
5556

56-
## 5. Sort
57+
## 5. Attribute position
58+
59+
Results are sorted by the **position of query terms within the attributes**. Returns documents that contain query terms closer to the beginning of an attribute first.
60+
61+
This rule evaluates only the position of matched words within attributes and does not consider the attribute ranking order.
62+
63+
## Attribute
64+
65+
`attribute` is an older built-in ranking rule equivalent to using both `attributeRank` and `wordPosition` together. When you use `attribute`, Meilisearch first sorts results by the attribute ranking order, then uses the position within attributes as a tiebreaker.
66+
67+
<Warning>
68+
You cannot use `attribute` together with `attributeRank` or `wordPosition`. If you try to configure ranking rules with both, Meilisearch will return an error. We recommend using a combination of the `attributeRank` and `wordPosition` rules.
69+
</Warning>
70+
71+
For most use-cases, we recommend using `attributeRank` and `wordPosition` separately. This gives you more control over result ordering by allowing you to place other ranking rules (like `sort` or custom ranking rules) between them.
72+
73+
## 6. Sort
5774

5875
Results are sorted **according to parameters decided at query time**. When the `sort` ranking rule is in a higher position, sorting is exhaustive: results will be less relevant but follow the user-defined sorting order more closely. When `sort` is in a lower position, sorting is relevant: results will be very relevant but might not always follow the order defined by the user.
5976

6077
<Note>
6178
Differently from other ranking rules, sort is only active for queries containing the [`sort` search parameter](/reference/api/search#sort). If a search request does not contain `sort`, or if its value is invalid, this rule will be ignored.
6279
</Note>
6380

64-
## 6. Exactness
81+
## 7. Exactness
6582

6683
Results are sorted by **the similarity of the matched words with the query words**. Returns documents that contain exactly the same terms as the ones queried first.
6784

@@ -95,16 +112,16 @@ The reason why `Creature` is listed before `Mississippi Grind` is because of the
95112
The `proximity` rule sorts the results by increasing distance between matched query terms.
96113
</Tab>
97114

98-
<Tab title="Attribute">
115+
<Tab title="Attribute rank">
99116
<Frame>
100-
<img src="/assets/images/ranking-rules/belgium.png" alt="Demonstrating the attribute ranking rule by searching for 'belgium'" />
117+
<img src="/assets/images/ranking-rules/belgium.png" alt="Demonstrating the attributeRank ranking rule by searching for 'belgium'" />
101118
</Frame>
102119

103-
### Attribute
120+
### Attribute rank
104121

105122
`If It's Tuesday, This must be Belgium` is the first document because the matched word `Belgium` is found in the `title` attribute and not the `overview`.
106123

107-
The `attribute` rule sorts the results by [attribute importance](/learn/relevancy/attribute_ranking_order).
124+
The `attributeRank` rule sorts the results by [attribute importance](/learn/relevancy/attribute_ranking_order).
108125

109126
</Tab>
110127

learn/relevancy/ranking_score.mdx

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ description: This article explains how the order of attributes in the `searchabl
66

77
When using the [`showRankingScore` search parameter](/reference/api/search#ranking-score), Meilisearch adds a global ranking score field, `_rankingScore`, to each document. The `_rankingScore` is between `0.0` and `1.0`. The higher the ranking score, the more relevant the document.
88

9-
Ranking rules sort documents either by relevancy (`words`, `typo`, `proximity`, `exactness`, `attribute`) or by the value of a field (`sort`). Since `sort` doesn't rank documents by relevancy, it does not influence the `_rankingScore`.
9+
Ranking rules sort documents either by relevancy (`words`, `typo`, `proximity`, `exactness`, `attributeRank`, `wordPosition`) or by the value of a field (`sort`). Since `sort` doesn't rank documents by relevancy, it does not influence the `_rankingScore`.
1010

1111
<Note>
1212
A document's ranking score does not change based on the scores of other documents in the same index.
@@ -16,10 +16,11 @@ For example, if a document A has a score of `0.5` for a query term, this value r
1616

1717
The table below details all the index settings that can influence the `_rankingScore`. **Unlisted settings do not influence the ranking score.**
1818

19-
| Index setting | Influences if | Rationale |
20-
| :--------------------- | :----------------------------------- | :------------------------------------------------------------------------------------------------------------------------------------------------------------- |
21-
| `searchableAttributes` | The `attribute` ranking rule is used | The `attribute` ranking rule rates the document depending on the attribute in which the query terms show up. The order is determined by `searchableAttributes` |
22-
| `rankingRules` | Always | The score is computed by computing the subscore of each ranking rule with a weight that depends on their order |
23-
| `stopWords` | Always | Stop words influence the `words` ranking rule, which is almost always used |
24-
| `synonyms` | Always | Synonyms influence the `words` ranking rule, which is almost always used |
25-
| `typoTolerance` | The `typo` ranking rule is used | Used to compute the maximum number of typos for a query |
19+
| Index setting | Influences if | Rationale |
20+
| :--------------------- | :--------------------------------------------------- | :--------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
21+
| `searchableAttributes` | The `attributeRank` ranking rule is used | The `attributeRank` ranking rule rates the document depending on the attribute in which the query terms show up. The order is determined by `searchableAttributes` |
22+
| `searchableAttributes` | The `wordPosition` ranking rule is used | The `wordPosition` ranking rule rates the document based on the position of query terms within attributes |
23+
| `rankingRules` | Always | The score is computed by computing the subscore of each ranking rule with a weight that depends on their order |
24+
| `stopWords` | Always | Stop words influence the `words` ranking rule, which is almost always used |
25+
| `synonyms` | Always | Synonyms influence the `words` ranking rule, which is almost always used |
26+
| `typoTolerance` | The `typo` ranking rule is used | Used to compute the maximum number of typos for a query |

learn/resources/telemetry.mdx

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -167,9 +167,11 @@ This list is liable to change with every new version of Meilisearch. It's not be
167167
| `ranking_rules.typo_position` | Position of the `typo` ranking rule if any, otherwise `null` | 2
168168
| `ranking_rules.proximity_position` | Position of the `proximity` ranking rule if any, otherwise `null` | 3
169169
| `ranking_rules.attribute_position` | Position of the `attribute` ranking rule if any, otherwise `null` | 4
170-
| `ranking_rules.sort_position` | Position of the `sort` ranking rule | 5
171-
| `ranking_rules.exactness_position` | Position of the `exactness` ranking rule if any, otherwise `null` | 6
172-
| `ranking_rules.values` | A string representing the ranking rules without the custom asc-desc rules | "words, typo, attribute, sort, exactness"
170+
| `ranking_rules.attribute_rank_position` | Position of the `attributeRank` ranking rule if any, otherwise `null` | 5
171+
| `ranking_rules.attribute_position_position` | Position of the `wordPosition` ranking rule if any, otherwise `null` | 6
172+
| `ranking_rules.sort_position` | Position of the `sort` ranking rule | 7
173+
| `ranking_rules.exactness_position` | Position of the `exactness` ranking rule if any, otherwise `null` | 8
174+
| `ranking_rules.values` | A string representing the ranking rules without the custom asc-desc rules | "words, typo, attributeRank, sort, wordPosition, exactness"
173175
| `sortable_attributes.total` | Number of sortable attributes | 3
174176
| `sortable_attributes.has_geo` | `true` if `_geo` is set as a sortable attribute | true
175177
| `filterable_attributes.total` | Number of filterable attributes | 3

0 commit comments

Comments
 (0)