krisk / Fuse

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

Inverse match returns no result when matched fields are nullish #749

Open asnaeb opened 7 months ago

asnaeb commented 7 months ago

Is there an existing issue for this?

Description of the bug

When using reverse matching operator ! along with nested search, items with matched fields that have nullish values (undefined, null or "") are completely discarded from the results. I think that a reverse match operator should match nullish values too. "", null and undefined should match !somestring! Is there a workaround for this that I am missing?

The Fuse.js version where this bug is happening.

6.6.2

Is this a regression?

Which version did this behavior use to work in?

None

Steps To Reproduce

We try to match every item whose lastName field doesn't include the string doe

const items = [
  {
    firstName: "John",
    lastName: null
  },
  {
    firstName: "Jack",
    lastName: undefined
  },
  {
    firstName: "Jim",
    lastName: ""
  }
];

const fuse = new Fuse(items, {
  keys: [ "firstName", "lastName"],
  useExtendedSearch: true
});

const result = fuse.search({lastName: "!doe"});

result.length === 0; // true;

Result is empty, but none of the three items actually have a lastName field which includes doe.

Expected behavior

const items = [
  {
    firstName: "John",
    lastName: null
  },
  {
    firstName: "Jack",
    lastName: undefined
  },
  {
    firstName: "Jim",
    lastName: ""
  }
];

const fuse = new Fuse(items, {
  keys: [ "firstName", "lastName"],
  useExtendedSearch: true
});

const result = fuse.search({lastName: "!doe"});

result.length === 3;
asnaeb commented 6 months ago

For anyone landing here with the same problem, I am currently using the following workaround

const fuse = new Fuse(items, {
  ...yourOptions,
  useExtendedSearch: true,
  getFn(...args) {
    return Fuse.config.getFn(...args) || "\uFFFF";
  }
});

This will return the unicode non-character FFFF as a fallback for nullish values, preventing them to be excluded from the result set. This character is very unlikely to be typed inside a search string so it is working fine for me at the moment but still it would be nice to see this fixed. I was even thinking of making a PR but based on what I've seen it looks like external PR's and issues get very little consideration these times so I'm not really motivated in doing one which could potentially remain ignored. (This doesn't mean to be a critique to the author)

fhollste commented 4 months ago

I am facing this issue as well but the suggested workaround does not work in my case. I believe that this is due to searching in a nested property where the parent is undefined. I am using a fork so I could fix it locally, can you @asnaeb give any hints on what causes the issue? Thanks!

github-actions[bot] commented 2 weeks ago

This issue is stale because it has been open 120 days with no activity. Remove stale label or comment or this will be closed in 30 days