javascript - 如何在 Javascript 中使用 JSON 进行多维搜索?

标签 javascript arrays json performance search

我正在尝试使用 JavaScript 和 JSON 编写一个基本的实验性搜索系统,可搜索数据包含在 JSON 文件中。文件中列出了多个“帖子”,每个帖子都有一个“标签”数组。我的目的是搜索每个帖子标签,并仅检索具有与查询匹配的标签的帖子,例如“有趣的猫视频”(帖子必须具有所有三个标签:“有趣”、“猫”和“视频”,待返回)。

我特别关心的是性能。我确信这种技术效率很低,因为大约有 2000 个帖子,每个帖子都有 5 到 50 个标签,但它必须使用 JavaScript 来完成。我已经从该网站引用了如何最大限度地提高性能的信息,尽管我需要一些额外的帮助。

这是迄今为止我用于存储数据的代码:

{
    "index": {
        "count": "2",
        "posts": [
            {
                "id": "1",
                "date": "2014-11-21 17:16:39 GMT",
                "url": "http://url/",
                "image": "http://big_image/",
                "thumbnail": "http://little_image/",
                "tags": ["funny", "cat", "picture", "falling", "chair", "window sill", "funny"]
            },
            {
                "id": "2",
                "date": "2014-11-20 17:57:32 GMT",
                "url": "http://url1/",
                "image": "http://big_image1/",
                "thumbnail": "http://little_image1/",
                "tags": ["funny", "cat", "picture", "jumping", "water", "bath", "funny"]
            }
        ]
    }
}

这是我的 JavaScript:

var query = "funny cat bath".split(" ");
var data = JSON.parse("THE JSON GOES HERE");
var count = data.index.count;
var index = data.index.posts;
for (var i = 0, indexLength = index.length; i < indexLength; i++) {
    tags = index[i].tags;
    for (var q = 0, queryLength = query.length; q < queryLength; q++) {
        if(tags.indexOf(query[q]) !== false) {
            console.log(index[i]);
        }
    }
}

不幸的是,我不知道如何让它只返回具有所有三个标签的帖子,并且它返回带有所提供的任何标签的所有帖子。不仅如此,它还返回重复项。

有人有更好的解决方案吗?我被困住了。

最佳答案

您需要使用一个标志,并且仅在找到所有匹配项时“写出”匹配项,即在找到一个匹配项时将其写出。再加上indexOf返回-1,而不是false。基本思想如下:

var data = {
    "index": {
        "count": "2",
        "posts": [
            {
                "id": "1",
                "date": "2014-11-21 17:16:39 GMT",
                "url": "http://url/",
                "image": "http://big_image/",
                "thumbnail": "http://little_image/",
                "tags": ["funny", "cat", "picture", "falling", "chair", "window sill", "funny"]
            },
            {
                "id": "2",
                "date": "2014-11-20 17:57:32 GMT",
                "url": "http://url1/",
                "image": "http://big_image1/",
                "thumbnail": "http://little_image1/",
                "tags": ["funny", "cat", "picture", "jumping", "water", "bath", "funny"]
            }
        ]
    }
};


var query = "funny cat bath".split(" ");
var filteredSet = [];  //where the matched objects will reside
var posts = data.index.posts;  //get the posts
for (var i=0; i<posts.length;i++) {  //loop through the posts
    var post = posts[i];  
    var tags = post.tags;  //reference the tags
    var hasMatch = true;  //flag to hold the state if we have a good match - set to true by default
    for (var j=0; j<query.length; j++) {  //loop through the tags the user is looking for
        var index = tags.indexOf(query[j]);  //look for it in the set [Note older IEs needs polyfill see MDN for code]
        if (index===-1) { //indexOf returns -1 if not found
            hasMatch=false;  //set Boolean flag so we do not record item
            break;  //exit loop - no reason to keep checking
        }
    }
    if (hasMatch) { //if we found all the tags
        filteredSet.push(post); // add to the filtered set
    }
}
console.log(filteredSet);  //show the filtered set

关于javascript - 如何在 Javascript 中使用 JSON 进行多维搜索?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27110699/

相关文章:

c++ - 结构和数组(C++)如何使用一个数组将卷号分配给名称

javascript - 在移动设备上更改 highcharts 点的大小

javascript - 拖放排序元素后创建重复项

javascript - ie9 ajax跨域withCredentials不发送cookie

javascript - 在让 JSLint 开心的同时创建一个 n 大小的数组?

go - 在 Go 中将数组中的多个字节转换为另一种类型

json - 如何在嵌套对象 SWIFT 中获取值

mysql - 如何一次执行多个查询,如果一个查询失败,另一个查询就无法执行?

javascript - json 传递给 D3

javascript - 将高度设置为 100% 时,React Apexchart 未采用其父高度