krisk / Fuse

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

Using boolean search parameters as config? #326

Closed mhp-borisbojic closed 4 years ago

mhp-borisbojic commented 5 years ago

Hi,

I'd like to set some default boolean parameters in the config, I wonder if this is already possible in some way?

My json looks like this:

[
  {
    name: 'A title',
    title: 'Sample entry 1',
    cool: true,
    closed: false,
    tags: ['one', 'two', 'three'],
    public: true,
  }
]

How can I search for entries that have cool: true or closed: false? Thanks!

krisk commented 5 years ago

You cannot search booleans. For something like this, you wouldn't need Fuse.js at all. However, if you are indeed interested in fuzzy matching the booleans (i.e, account for typos), you could pre-process the JSON and change all the booleans to their string representation (true"true", false"false"), and use this new JSON when searching.

mhp-borisbojic commented 5 years ago

Hey, thanks for the answer.

I think you didn't get me right, my intention is not to "search" for booleans.

Right now my search looks like this (Vue.js + Vuex Store):

 const filteroptions = {
        shouldSort: true,
        tokenize: true,
        findAllMatches: true,
        threshold: 0.3,
        location: 0,
        distance: 20,
        maxPatternLength: 64,
        minMatchCharLength: 3,
        keys: [
          'id',
          'title',
          'tags',
        ],
      };

      const data = state.loadedIconsData;
      let tempResult;
      let result;

      // Fuse doesn't support config based search parameters, so we have to use this workaround
      if (state.searchFiter.isBrand !== false || state.searchFiter.isVehicle !== false ) {
        tempResult = filter(data, { brand: state.searchFiter.isBrand, vehicle: state.searchFiter.isVehicle });
      } else {
        tempResult = data;
      }

      if (state.searchFiter.input !== '' && state.searchFiter.input.length > 2) {
        const fuse = new Fuse(tempResult, filteroptions);
        result = fuse.search(state.searchFiter.input);
      } else {
        result = tempResult;
      }

I'm using Fuse for the real fuzzy search, but need to filter out some elements based on their boolean values (e.g. don't include objects that have brand === false.

So I wonder if we could at this booleans somehow to Fuse's options, e.g. like this:

const filteroptions = {
  shouldSort: true,
  tokenize: true,
  findAllMatches: true,
  threshold: 0.3,
  location: 0,
  distance: 20,
  maxPatternLength: 64,
  minMatchCharLength: 3,
  keys: [
    'id',
    'title',
    'tags',
  ],
  customLeaveOutSomething = {
    brand: true,
    public, false
  } 
};
richcarrot commented 5 years ago

Consider doing the filtering before, or after, the search; instead of filtering as part of the search. I wouldn't expect Fuse to do both.

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