krisk / Fuse

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

How to search for item with null/undefined as value? #637

Closed pawlarius closed 1 year ago

pawlarius commented 2 years ago

Hi Fuse team and community, first of all thank you for this amazing library! It really help all the heavy weight lifting for searching and matching items.. but I seem to struck into a case that I'm not sure if Fuse will/had already support.

So consider this is my items to be searched from:

const items = [
  { title: 'Apple', price: 10 },
  { title: 'Berry', price: undefined },
  { title: 'Cherry', price: null },
]

And I want to match items that doesn't have price, which in this case is "Berry" and "Cherry". But when I tried to use this query, Fuse will return invalid value.

const fuseOptions = new Fuse(items, {
keys: ['title', 'price'],
});

fuseOptions.search({
  $and: [
    {
      $or: [
        { price: null }
      ]
    }
  ]
})

I tried searching through this repo but all I found is Fuse do support filter zero values from this PR https://github.com/krisk/Fuse/pull/73. Is there any other reason why we don't allow null/undefined value for filtering?

krisk commented 2 years ago

Fuse doesn't actually handle this, as it was designed for searching, and not filtering, though I recognize the allure of the latter use case. For example, should Fuse at some point add filtering for numbers or dates with comparison operators (i.e, less than, greater then)? I don't know about this (yet).

For your case, one crude way would be to format undefined and null before they're passed into Fuse, to be "none", and you search for that. Admittedly, I don't like this approach, but it works 😅

ChadBurggraf commented 1 year ago

I'll chime in here and say that I would LOVE the ability to do comparative filtering and I'd be willing to help work on it.

There are two paths for handling this I assume:

  1. Add new extended search operators
  2. Add the ability to pass in custom BaseMatch implementations

For option 2, you could either assume all custom implementations take precedence over the built-in ones, or export the built-in searchers and only use those provided if any are provided (i.e., require callers to splice the searchers array into the precedence order they need).

github-actions[bot] commented 1 year 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