krisk / Fuse

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

How can an object get a score without any matches? #442

Closed washtenawIdtreble closed 4 years ago

washtenawIdtreble commented 4 years ago

Describe the bug

Fuse is returning search results that have scores but have no matches.

I'm using Fuse to search objects that look like this:

export class Business {
    public name: string,
    public number: string,
    public street: string,
    public city: string,
    public state: string,
    public zipCode: string,
    public website: string,
    public phone: string,
    public description: string,
    public logoImgSrc: string,
    public distance: number,
    public categories: Array<string>,
    public discount: string,
    public spanishDescription: string,
    public spanishDiscount: string
  }

My fuse options look like this:

includeScore: true,
includeMatches: true,
keys: [
    "name"
    "street"
    "city"
    "zipCode"
    "description"
    "categories"
    "discount"
    "spanishDescription"
    "spanishDiscount"
],
minMatchCharLength: Math.ceil(searchTerm.length / 2),
threshold: 0.5,
distance: 10000000,

I'm using a very high distance because I essentially want to ignore it. Some of the fields I'm searching are hundreds of characters long, and I don't care whether a match is found at the beginning or the end. I discovered that using a huge distance limits its impact on the score.

Note that I'm using half the search term length (rounded up) for the minimum match character length. I'm planning to experiment with changing that. I want users to be able to make typos where there are too many letters, so I didn't want to say that the minimum match character length was exactly the search term length. Maybe this is misguided.

When I run a search for the word "old" on an array of 38 of these businesses, the results include all 38 businesses.

Furthermore, if I adjust the minimum match character length from 2 to 3 for this search term:

How is it possible to get search results that have no matches? Am I using Fuse wrong in some way?

Version

5.2.3

Is this a regression?

I don't know

marcus-at-localhost commented 4 years ago

Searching for "remy" in the demo returns unrelated records as well. https://fusejs.io/demo.html

washtenawIdtreble commented 4 years ago

Searching for "remy" in the demo returns unrelated records as well. https://fusejs.io/demo.html

Very helpful, thanks for finding that!

krisk commented 4 years ago

Yes, this is a bug, thanks for filing this. minMatchCharLength is ignored when determining which records to exclude. I'll address it for the next release.

krisk commented 4 years ago

Now in latest fuse.js@6.2.0