使用lawnchair db的phonegap应用程序的搜索功能

时间:2012-09-25 03:41:34

标签: search cordova lawnchair

我该怎么做(标题)?

我查看了http://brian.io/lawnchair/plugins/上的插件“docs” 并且我可以看到寻找平等,但我没有看到.indexOf()

的东西 用例: 我想搜索包含关键字“obama”的每篇文章的文章数据库 至少我想在文章的正文字段中看到每篇文章对象都带有“奥巴马”。

如果有关键字与每篇文章相关联,那么我也可以添加它,我的主要问题就是进行搜索。

1 个答案:

答案 0 :(得分:0)

我最终使用正则表达式并迭代整个数据库: 如果有人有更好的想法或更清晰的代码,请随时贡献。

var lawnchair = Lawnchair({name:'lawnchair'},function(e){
            console.log('storage open');
        });

 $('#search').click(function(e) {
            var search_term = $("#search_field").val();
            var search_type = $('#search_type').val(); 
            var re = new RegExp(search_term, "gi")
            console.log("startings search for "+search_term);
            console.log("startings search for "+re.toString());
            lawnchair.all(function(articles){
                $('#article_list').empty();
                console.log(articles.length);
                var counter = 0;
                for(var i = 0; i<articles.length;i++)
                {
                    cur_a = articles[i].value;
                    if(cur_a["title"] != null){
                        var thing_to_search = "string";
                        if(search_type == "both"){
                            thing_to_search = cur_a["title"]+" "+cur_a["body"];
                        }else if(search_type == "title"){
                            thing_to_search = cur_a["title"];
                        }else if (search_type == "body"){
                            thing_to_search = cur_a["body"];
                        }
                        var matches = thing_to_search.match(re);
                        if (matches != null) 
                        {
                            counter = counter + 1;
                            var lyo = make_article_layout();
                            lyo.find("#title").text(cur_a["title"]);
                            //xrank is the kw density
                            //let title be 10x more important than body
                            var xrank = (1.0 * matches.length) / thing_to_search.length;
                            if(cur_a["title"] != null){
                                var m2 = cur_a["title"].match(re);
                                if(m2 != null){
                                    xrank = (xrank * m2.length)
                                }
                            }
                            lyo.find("#match_count").text(matches.length.toString());
                            lyo.find("#xrank").text(xrank.toString());
                            console.log(cur_a["title"]);
                            //console.log(cur_a["body"]);
                            lyo.id = cur_a["_id"];
                            //lyo.find("#body").text(cur_a["body"]);
                            lyo.find("#published_at").append(" "+cur_a["published_at"]);
                            lyo.find("#author").append(" "+cur_a["author"]);
                            lyo.find("#source").append(" paper_id:"+cur_a["paper_id"]+" | "+cur_a["url"]);
                        }
                    }
                }
                $('#status').text("Results Found:"+counter.toString())
            });
        });