fuzzysort icon indicating copy to clipboard operation
fuzzysort copied to clipboard

Prioritising parts of the content to match

Open janglad opened this issue 7 months ago • 1 comments

say I have a data structure like

declare const tasks: {title: string, longDescription:string}[]

What is the recommended approach to fuzzy search over both title and string, but prioritise matches based on title?

janglad avatar May 23 '25 21:05 janglad

just use keys. shorter strings are prioritized. so your title would automatically be prioritized over your longDescription

const books = [
  {title:'spaghetti and meatballs', longDescription:"a book about food and stuff and recipes and idk this is a long description about it. i think it's a masterpiece"},
  {title:'easy recipes', longDescription:"yet another book about food. probably mentions spaghetti and meatballs. i don't know, i've never read it"}
]

var results = fuzzysort.go('meatballs', books, {keys: ['title', 'longDescription']})
// {title: 'spaghetti and meatballs', score: .82}, {title: 'easy recipes', score: .67}

var results = fuzzysort.go('recipes', books, {keys: ['title', 'longDescription']})
// {title: 'easy recipes', score: .89}, {title: 'spaghetti and meatballs, score: .66}

if you need more control, you can write a custom scoreFn, something like this scoreFn: r => r.score + r[0].score*10

farzher avatar Dec 01 '25 01:12 farzher