krisk / Fuse

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

Search returns wrong results #515

Closed bRRRITSCOLD closed 3 years ago

bRRRITSCOLD commented 3 years ago

Describe the bug

Search does not return expected results.

Expected Result

Only one object returned

Actual Result

All items returned

Version

^6.4.3"

Is this a regression?

No - Never used before

🔬Minimal Reproduction

1) npm install --save fuse.js 2) copy and paste code below 3) run

Specs:

macOS: 10.15.7 (19H15)

Additional context

import Fuse from 'fuse.js';

const list = [ { auditLog: "logs-1/.asjdfnjasbfa.json", author: 'John Scalzi', tags: ['fiction'] }, { auditLog: "logs-2/.ahjdfnjasbfa.json", author: 'Steve', tags: ['thriller'] } ]

const options = { // Search in author and in tags array keys: ['auditLog'] }

const fuse = new Fuse(list, options)

const result = fuse.search("logs-1")

console.log(result)

krisk commented 3 years ago

I'm seeing all items returned:

[
  {
    item: {
      auditLog: 'logs-1/.asjdfnjasbfa.json',
      author: 'John Scalzi',
      tags: [ 'fiction' ]
    },
    refIndex: 0
  },
  {
    item: {
      auditLog: 'logs-2/.ahjdfnjasbfa.json',
      author: 'Steve',
      tags: [ 'thriller' ]
    },
    refIndex: 1
  }
]

Fuse version: 6.4.3

bRRRITSCOLD commented 3 years ago

@krisk I would think that only one item would be returned for a match on "logs-1"?

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

bRRRITSCOLD commented 3 years ago

bump

krisk commented 3 years ago

This is working as expected. If the search criteria is "logs-1", it will match both "logs-1" and "logs-2". Remember that Fuse is doing an approximate string matching (aka, fuzzy matching), and while "logs-1" is a perfect match, "logs-2" is a very close match nonetheless.

kennethbrent commented 3 years ago

What would be the best way to get fuse to return only an exact match if there is one, likewise only return the partial matches if it can't find an exact?

lmiller1990 commented 3 years ago

Anyone find a good config? I want to make something like the fuzzy finder in vscode - if I am looking for a file called log-1 with the query log-1, I do not want log-2 in my search results.

chmac commented 2 years ago

@lmiller1990 I think you're looking for something different to what fuse is doing. If I've understood correctly fuse is essentially about finding close misspellings, so you can search foo1 and find foo4 because it's similar. If you want something which is like the VSCode file matcher, it matches on exact content, but that content doesn't need to be consecutive. I don't think there will be a way to get Fuse to do that. Maybe there's a different package which does what you're looking for.