krisk / Fuse

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

Returned scores are equal when differences only exist beyond the length of the search query #441

Closed RoderickJDunn closed 4 years ago

RoderickJDunn commented 4 years ago

Describe the bug

When matching a list of strings that are longer than the query string, there is no difference between the scores of the results if differences only exist beyond the last index of the query string.

Version

6.0.0

Is this a regression?

No, I believe it existed in previous versions.

🔬Minimal Reproduction

Consider this very basic example to illustrate what I mean.

List of Items to search through:

let list = [
"mr. h",
"mr. p",
"mr. m"
];

Search Term: let searchTerm = "mr.h"

Notice that the search term is almost equivalent to the first result- the only difference is the missing space after the ".".

Running the search produces the following results:

const options = {
  includeScore: true,
  threshold: 0.7,
};

const fuse = new Fuse(list, options)
const result = fuse.search(searchTerm)

[ { "item": "mr. h", "refIndex": 0, "score": 0.37526977437859277 }, { "item": "mr. p", "refIndex": 1, "score": 0.37526977437859277 }, { "item": "mr. m", "refIndex": 2, "score": 0.37526977437859277 } ]

I would expect the first item "mr. h" to have a higher score than the other 2, since it has only 1 character difference compared to the search query, while the others have two differences. However, all 3 scores are equal. ie. All three results have an extra space at index 2 (compared to the query), but the 2nd/3rd result also have the wrong last character (p/m instead of h).

This behaviour only seems to be present when the list items are longer than the search query, and are identical up to the length of the search query, but have differences beyond that.

Is this expected behaviour? If so, could you explain why that's the case?

github-actions[bot] commented 4 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