texas697
texas697

Reputation: 6397

How to set fuse.js options

const JSON = [
  {
    "name": "01,02"
  },
  {
    "name": "01"
  },
  {
    "name": "05"
  },
  {
    "name": "06,09"
  },
  {
    "name": "04,05"
  },
  {
    "name": "02,03"
  },
  {
    "name": "02,04,05"
  },
  {
    "name": "01,02"
  },
  {
    "name": "01,03"
  }
]


function foo (str) {
  const options = {
    keys: ['name'],
    threshold: 0,
    location: 0,
    distance: 100,
    minMatchCharLength: str.length
  }
  const _fuse = new Fuse(JSON, options)
  console.log(_fuse.search(str))
}

foo('03')
<script src="https://cdnjs.cloudflare.com/ajax/libs/fuse.js/3.2.1/fuse.min.js"></script>

I am trying to use fuse.js for filtering. I am unsure of how to use the options. If I search with 03 I am not returning any results. I have tried changing the location and threshold but not completely understanding it. I set the minMatchCharLength to the length of the query string thinking that would prevent a query of 01 from returning items that only contain a 0. Is that correct?

Upvotes: 3

Views: 10569

Answers (3)

Jan Zikmund
Jan Zikmund

Reputation: 686

With threshold: 0, your name must exactly match both location and content, so that means search for "03" will only return items starting with "03".

If you want to keep the exact matching, but want to allow your search string to be anywhere within the name, since Fuse 6.2.0 you can now use the ignoreLocation parameter. As per the docs:

When true, search will ignore location and distance, so it won't matter where in the string the pattern appears.

So your example params would look like:

// ...  
const options = {
    keys: ['name'],
    threshold: 0,
    ignoreLocation: true,
    location: 0,
    distance: 100,
    minMatchCharLength: str.length
}
// ...

This is a better alternative to previous approaches, where you had to use high distance and low but non-zero threshold

Upvotes: 0

Andrey
Andrey

Reputation: 4050

Your code is fine already. You set threshold to 0 which means

fuse requires a perfct match (of both letters and location)

And there is no name property which is exactly 03.

So if you fiddle just with threshold and set it to 0.2 for example - you will get some results from search:

[{name: "02,03"}, {name: "01,03"}]

Upvotes: 1

Leo
Leo

Reputation: 680

try to change "name" to name:

const JSON =  [
  {
    name: "01,02"
  },
  {
    name: "01"
  },
  {
    name: "05"
  },
  {
    name: "06,09"
  },
  {
    name: "04,05"
  },
  {
    name: "02,03"
  },
  {
    name: "02,04,05"
  },
  {
    name: "01,02"
  },
  {
    name: "01,03"
  }
];

Here you can find a demo that allows you to 'play' with the configurations. You can paste your json and try to get different results: Link

For more information about the configuration, you can refer to step 2 of the previous link, which explains all the options. Eg: threshold : At what point does the match algorithm give up. A threshold of 0.0 requires a perfect match (of both letters and location), a threshold of 1.0 would match anything

Here a demo with your example : jsfiddle

Upvotes: -1

Related Questions