krisk / Fuse

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

exact string matches return score as a very small floating point number, not exactly 0 #545

Closed cypressf closed 3 years ago

cypressf commented 3 years ago

score: 2.220446049250313e-16 when I am comparing two identical strings, and searching by key I'm using typescript and fuse.js@6.4.6

    const options = {
        includeScore: true,
        threshold: 0.1,
        keys: ["name"],
      };

   const results = (new Fuse([{name: 'a'}], options)).search('a');
   console.log(results[0].score === 0) // false, because score is 2.220446049250313e-16
dodlhuat commented 3 years ago

I'd see this as a bug. I am facing the same issue and therefore findAllMatches isn't working as it never finds an exact match!

krisk commented 3 years ago

For better or worse, this is by design. Take a look at the scoring theory to understand why.

ljones-i-nexus commented 3 years ago

This looks like design https://github.com/krisk/Fuse/blob/6c76f5241a62bf50085e066f71bc68f1b4d1868f/src/core/index.js#L298.

My question is, are the docs now incorrect? It doesn't look like a 0 for an exact match can actually be returned? If that's the case, am I safe to check against EPSILON when wanting an exact match and the weighting is at 1?

ljones-i-nexus commented 3 years ago

For better or worse, this is by design. Take a look at the scoring theory to understand why.

According to the docs, by default if a perfect match is found then the search ceases to continue unless specifically setting findAllMatches to true. If you would like to do it manually, the docs also state that a score for an exact match is 0. Neither of these things is true regardless of the scoring theory.

I'm not saying this needs to change (although findAllMatches does seem redundant at this point and exact matching is inconsistent at best), but the docs certainly need to be updated, otherwise I can completely understand why people are treating this as a bug.

https://fusejs.io/api/options.html#findallmatches https://fusejs.io/api/options.html#includescore

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