Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
1 of 2 tasks
asnaeb opened this issue Nov 13, 2023 · 2 comments
Open
1 of 2 tasks

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

asnaeb opened this issue Nov 13, 2023 · 2 comments
Labels

Comments

@asnaeb
Copy link

asnaeb commented Nov 13, 2023

Is there an existing issue for this?

  • I have searched the existing issues

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?

  • This is a regression bug

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 asnaeb added the bug label Nov 13, 2023
@asnaeb asnaeb changed the title Reverse match returns no result when matched fields are nullish Inverse match returns no result when matched fields are nullish Nov 14, 2023
@asnaeb
Copy link
Author

asnaeb commented Dec 11, 2023

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
Copy link

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!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants