Glench / fuzzyset.js

fuzzyset.js - A fuzzy string set for javascript
http://glench.github.io/fuzzyset.js/
Other
1.37k stars 105 forks source link

Option to get matches even if has exact match #26

Closed btxtiger closed 6 years ago

btxtiger commented 6 years ago

Currently, the function returns just the exact match, if there ist one. An option to get also the lower-scored matches would be very helpful. Current workaround: Add "unused" character like space to the end of the search value, so there will never be exact matches

Glench commented 6 years ago

Hm, I get the sense that this is a relatively uncommon use case so I'd rather not add it to the library, but it's actually quite easy to add that functionality if you want to fork it. On line 103 of lib/fuzzyset.js in the _get() function it says:

if (result) {
    return [[1, result]];
}

If you just remove that then it should work. Cheers!

btxtiger commented 6 years ago

My use-case is, when a user searches for the city "Muehlheim" in ['Muehlheim', 'Muelheim'] the search result will only be ['Muehlheim']. But the thing is, that adding a "h" where none should be is a common misspelling in german, thats why I want to provide also the other results.

Great to know that! This will fix it, thanks!

Glench commented 6 years ago

Ah, thanks for letting me know! If other people ask for it I may add this feature in the future.

nicooprat commented 4 years ago

I'm getting the same use case: I have "Paris" and "Paris - La Défense". When searching for "Paris", the two should be returned. An option instead of forking the repo would be great :)

Thanks for your work anyway.

Glench commented 4 years ago

I'm getting the same use case: I have "Paris" and "Paris - La Défense". When searching for "Paris", the two should be returned. An option instead of forking the repo would be great :)

Is there any reason you'd need fuzzy matching in that case? You could just search through the entire array looking for the search string.

nicooprat commented 4 years ago

Of course, it's one case among many others. The results are confusing for our users. Here's a sample:

fuzzy

The fuzzy search is still useful to manage accents, cases, typos... But for "correct" searches it can get misleading :)

Glench commented 4 years ago

Alright, you've convinced me. I took out the exact match check so now users of the library have more flexibility in how results are handled.

To get the results in the dropdown you probably want, you'll need to do something like set.get(search_string, null, .01) (the .01 makes it so all results are shown, even ones that might have unexpected matches. For example, if the search string is 'par' and you have ['paris', 'paris - la defense', 'Aparecida de Goiânia'], all 3 matches would be returned. You'll have to put it in separate logic to take care of that case.)

It is now up to the user of this library to check the results for an exact match (score of 1).

nicooprat commented 4 years ago

Firsts tests look great, thanks!