bug/feat: exact match should be first result in search dropdown
dipamsen opened this issue · 2 comments
ah! looks like it is maybe doing it in alphabetical order? We could implement a Levenshtein distance (I actually had this on my list to do a quick coding challenge at one point heh) or similar algorithm maybe? Or perhaps there's a more standard way of sorting results that can be applied here.
Looking at this issue this morning. Let me know what you think and I can finish the PR.
Levenshtein distance doesn't perform great here because it will rank shorter strings higher. It bubbles up the best match properly, but the rest is not very coherent.
The following approach ranks full word matches highest, then ranks partial matches based on the closeness to the beginning of the string. Seems to work well and it's much cheaper to compute.
// values are normalized before being passed to this function
const rank = (value, input) => {
// exact match: highest priority
if (value === input) return 0;
// complete word match: higher priority based on word position
const words = value.split(' ');
for (let i = 0; i < words.length; i++) {
if (words[i] === input) return i + 1;
}
// partial match: lower priority based on character position
const index = value.indexOf(input);
return index === -1 ? Number.MAX_SAFE_INTEGER : 1000 + index;
};EDIT: made it so that partial matches can never rank higher than full word match by adding 1000 instead of using word count.


