fuseJS模糊搜索对我不起作用

时间:2018-07-08 18:06:39

标签: javascript fuzzy-search fuse fuzzy-logic

是否有保险丝专家可以告诉我,为什么保险丝模糊搜索对我不起作用,我认为应该这样做。 (请参阅下面的演示代码。)当我搜索“预设”时,我得到一个返回的结果,即:

预设... 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>

1 个答案:

答案 0 :(得分:1)

只需看看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>