krisk / Fuse

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

How to search a single word? #565

Closed hieuvts closed 2 years ago

hieuvts commented 2 years ago

Fuse search not match if field value is only a single word.

image

Code:

  const fuse = new Fuse(characters, {
    // keys: ["name", "company", "species"],
    keys: [{ name: "title"}],
    threshold: 1.0,
    ignoreLocation: true,
    includeScore: true,
  });

I have to inseart a space (after a stand alone word) to make Fuse recognize it. Is it a bug or I'm using wrong Fuse search options?

JordanMalan commented 2 years ago

@hieuvts I've opened your sandbox and tested a few scenarios and could not reproduce the issue. I have also tested locally with no issue.

Is it possible your build did not update when you first added that entry? That could explain why it wasn't available when searching... If you continue to have this issue, try refreshing the sandbox window (to the left of the address bar) and try again.

hieuvts commented 2 years ago

@JordanMalan I'm sorry for didn't provide a sandbox link. Please take a look in: https://codesandbox.io/s/confident-easley-k9l83?file=/src/App.js

"John" and "Michael" are not listed in the search results. But "Oliver " (with a white space) does. image

JordanMalan commented 2 years ago

@hieuvts Hi, I found your problem, see this Sandbox

The issue is that when you have no search query, you instead query with " " a space. So all of the names that have a space are returned.

In addition, your threshold was 1.0 which is far too high as a specific query like "John" will return all results. I recommend using 0.5 so that the search works better.

I've also mapped the fuse results to match the data array's format, this is so the map at the bottom works.

fusebug

hieuvts commented 2 years ago

@JordanMalan
Thank you for helping me solve the problem with a nice solution. It's easy to understand.