2

有没有 fuseJS 专家可以告诉我为什么 fuseJS 模糊搜索不适合我,因为我认为它应该。(请参阅下面的演示代码。)当我搜索“预设”时,我得到一个返回结果,即:

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

但是当我搜索“集合”时,我没有得到任何结果。我不应该为这两个搜索获得相同的结果吗?我需要在选项中添加一些东西吗?或者 fuseJS 无法进行这种类型的搜索?

提前感谢您能给我的任何帮助。

下午

    <!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>
4

1 回答 1

2

看看threshold(取自http://fusejs.io/

临界点

匹配算法在什么时候放弃。阈值0.0需要完美匹配(字母和位置),阈值1.0可以匹配任何内容。

并将值更改为0.2,例如。然后你得到一个结果。

您可以尝试使用一组更大的数据来获取一些不同的值threshold并检查结果。

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>

于 2018-07-08T18:19:00.887 回答