javascriptfuzzy-searchfuse.js

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?


Solution

  • 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"}]