krisk / Fuse

Lightweight fuzzy-search, in JavaScript
https://fusejs.io/
Apache License 2.0
17.79k stars 756 forks source link

Add score to matches result #535

Closed ibedarev closed 3 years ago

ibedarev commented 3 years ago

Description

Add score to matches result. It allows filter nested arrays

Describe the solution you'd like

change transformMatches function to function transformMatches(includeScore) return function (result, data) inside add

if (includeScore && isDefined(match.score)) {
        obj.score = match.score
      }

Use case

Sorting nested arrays in search result

const sortMatches = (a: Fuse.FuseResultMatch, b: Fuse.FuseResultMatch) => {
  if (a.score !== b.score) return a.score! - b.score!
  return a.refIndex! - b.refIndex!
}

matches.filter((m) => m.key === 'specific.neste.path').sort(sortMatches)

ibedarev commented 3 years ago

https://github.com/krisk/Fuse/pull/536

github-actions[bot] commented 3 years ago

This issue is stale because it has been open 30 days with no activity. Remove stale label or comment or this will be closed in 5 days

lawrence-laz commented 10 months ago

I hit the same use case: searching through complex objects with array property, each object usually has multiple matches inside the array property which are scored individually (can be seen in FuseSortFunctionArg), but these scores are unavailable in FuseResult, where we only get overall scores for each object as a whole.

It's useful to know each matches score individually so as to be able to display only the selected best matches from the array to the user.