krisk / Fuse

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

How to set prioritize exact matches when using dynamic weights for object property based search? #722

Closed gaurangblaze closed 7 months ago

gaurangblaze commented 1 year ago

I have a list of objects I need to search through. I have different weights for matches in different properties of the object. For example, this is my full search configuration:

const FUSE_OPTIONS = {
    isCaseSensitive: false,
    includeScore: true,    // return the score alongside the match (lower is better)
    includeMatches: true,  // return the match indices so that the matched portions can be highlighted
    findAllMatches: true,  // if multiple matches exist, then that object should have a better score
    minMatchCharLength: 1,
    ignoreLocation: true,  // it should not matter where in the search string the pattern appears
    ignoreFieldNorm: true,
    threshold: 0.5, // allow fuzzy matches
    keys: [
      { name: 'snippet.shortcut', weight: 4 },
      { name: 'snippet.name', weight: 3 },
      { name: 'group', weight: 2 },
      { name: 'text', weight: 1 },
    ]
  };

Right now, this configuration has an issue. Fuzzy matches from 'snippet.shortcut' rank higher than exact matches from 'text' property, because of the disproportionate weight.

In the search result list, I want the exact matches to show up first in the results. After that, I want to show the fuzzy matches. In other words, the search results list can be divided into two sections:

[list start][exact matches sorted by total weight][fuzzy matches sorted by total weight][list end]

Is this possible to do directly in fuse.js? I guess it's possible for me to do this manually by running fuse.js twice and manually extracting out the exact matches in the first call, but it's very cumbersome.

github-actions[bot] commented 8 months ago

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