Ranking
Results are ordered by a list of rules, applied one after another. Change the order or add your own to fit your content.
When a query matches many documents, ZeroIndex sorts them with a list of ranking rules. The first rule sorts everything. Each next rule only breaks ties left by the ones before it.
Default rules
Words
Documents that match more of the query words come first.
Typo
Documents with fewer typos come first. headphones beats hedphones.
Proximity
Documents where the query words sit closer together come first.
Attribute
Matches in more important fields come first, in the order of searchableAttributes.
Exactness
Documents that match the query exactly come before those that only match a prefix.
Custom rules
Add a rule that sorts by a number to push popular or recent documents up. The field must be listed in sortableAttributes.
await client.index("products").updateSettings({
rankingRules: ["words", "typo", "proximity", "attribute", "exactness", "sales_count:desc"],
});Typo tolerance
ZeroIndex allows one typo in words of five letters or more, and two typos in words of nine letters or more. You can change both thresholds, or turn typos off for fields like product codes.
await client.index("products").updateSettings({
typoTolerance: {
minWordSizeForTypos: { oneTypo: 4, twoTypos: 8 },
disableOnAttributes: ["sku"],
},
});Seeing why a result ranks where it does
Pass showRankingScore to get a score between 0 and 1 for every hit. It is useful while tuning, but do not show it to users.
const { hits } = await client.index("products").search("wireless", { showRankingScore: true });
// hits[0]._rankingScore === 0.97