Search code examples
javascriptfuzzy-searchfuzzy-logicfuse.js

fuseJS fuzzy search is not working for me


Are there any fuseJS experts out there who can tell me why fuseJS fuzzy search is not working for me as I think it should. (Please see the demo code below.) When I do a search for "presets" I get one result returned, which is:

presets ... eDirectory ... 2018/5/30 17:22:50 ... 4 KB

But when I so a search for "sets" I get no results retuned. Shouldn't I be getting the same results for both of these searches? Do I need to add something to the options? Or is this type of search not possible with fuseJS?

Thanks in advance for any help that you can give me.

P.M.

    <!DOCTYPE html>
<html lang="en">
<head>
    <title>Fuse.js example</title>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/fuse.js/3.0.4/fuse.min.js"></script>
</head>
<body>
    <h2>data:</h2>
    <textarea id="json" cols="30" rows="10"></textarea>
    <h2>Search for: <span id="searchString"></span></h2>
    <h2>Results:</h2>
    <div id="results"></div>
    <script>
    files =[
                {
                    name: "zips",
                    type: "bDirectory",
                    dateModified: "2018/5/10 17:39:27",
                    size: "1 KB"
                }
                ,
                {
                    name: "presets",
                    type: "eDirectory",
                    dateModified: "2018/5/30 17:22:50",
                    size: "4 KB"
                }
                ,
                {
                    name: "workflow",
                    type: "dDirectory",
                    dateModified: "2018/6/11 7:23:11",
                    size: "5 KB"
                }
                ,
                {
                    name: "software",
                    type: "aDirectory",
                    dateModified: "2018/6/14 14:35:36",
                    size: "2 KB"
                }
                ,
                {
                    name: "nmm_data",
                    type: "fDirectory",
                    dateModified: "2018/6/14 15:37:08",
                    size: "6 KB"
                }
                ,
                {
                    name: "jobs",
                    type: "cDirectory",
                    dateModified: "2018/6/15 13:43:47",
                    size: "3 KB"
                }
            ];

    var searchString = "presets";
    document.getElementById("searchString").innerHTML = searchString;

    document.getElementById("json").value=JSON.stringify(files);

    var options = {
        keys: ['name','type', "dateModified", "size"],
        threshold:0.0,
        caseSensitive:false
    }

    var f = new Fuse(files, options);
    var output = f.search(searchString);

    console.log(output);
    for(i=0;i<output.length;i++){
        document.getElementById('results').innerHTML += output[i].name + ' ... ' + output[i].type + ' ... ' + output[i].dateModified + ' ... ' + output[i].size + '<br />'
    }



</script>
</body>
</html>

Solution

  • Just have a look to threshold (taken from http://fusejs.io/)

    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.

    and change the value to 0.2, for example. Then you get a result.

    You may try with a greater set of data some different values for threshold and check the results.

    var files = [{ name: "zips", type: "bDirectory", dateModified: "2018/5/10 17:39:27", size: "1 KB" }, { name: "presets", type: "eDirectory", dateModified: "2018/5/30 17:22:50", size: "4 KB" }, { name: "workflow", type: "dDirectory", dateModified: "2018/6/11 7:23:11", size: "5 KB" }, { name: "software", type: "aDirectory", dateModified: "2018/6/14 14:35:36", size: "2 KB" }, { name: "nmm_data", type: "fDirectory", dateModified: "2018/6/14 15:37:08", size: "6 KB" }, { name: "jobs", type: "cDirectory", dateModified: "2018/6/15 13:43:47", size: "3 KB" }],
        searchString = "sets",
        options = { keys: ['name', 'type', "dateModified", "size"], threshold: 0.2, caseSensitive: false },
        f = new Fuse(files, options),
        output = f.search(searchString),
        i;
    
    document.getElementById("searchString").innerHTML = searchString;
    document.getElementById("json").value = JSON.stringify(files);
    console.log(output);
    
    for (i = 0; i < output.length; i++) {
        document.getElementById('results').innerHTML += output[i].name + ' ... ' + output[i].type + ' ... ' + output[i].dateModified + ' ... ' + output[i].size + '<br />'
    }
    <script src="https://cdnjs.cloudflare.com/ajax/libs/fuse.js/3.0.4/fuse.min.js"></script>
    <h2>data:</h2>
    <textarea id="json" cols="30" rows="10"></textarea>
    <h2>Search for: <span id="searchString"></span></h2>
    <h2>Results:</h2>
    <div id="results"></div>