krisk / Fuse

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

Weighted queries #643

Closed polx closed 1 year ago

polx commented 2 years ago

Description

We would like to query in different manners with different weights.

Describe the solution you'd like

Our goal is, today, for auto-completion: we wish to take a query for "math" and that it queries, in any of the index-defined fields:

In Lucene parlance, this called a query-weight and is written with a hat. We think there might be other use cases.

Describe alternatives you've considered

We think that it might be possible to try to merge several search results where each sub-query is done independently (but will it be comparable?).

springuper commented 2 years ago

There is an option sortFn which might be useful for your case.

You could re-compute the score according to your own standard and then sort the results, here is how we use it:

const sortFn = (a, b) => {
  const getScore = (matches) => {
    return matches.reduce((acc, match) => {
      const score = (match.key.weight || 1) * match.norm * (1 - Math.max(match.score, Number.EPSILON));
      acc += score;
      return acc;
    }, 1);
  };
  return getScore(b.matches) - getScore(a.matches);
};
github-actions[bot] commented 1 year 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