Search code examples
searchcordovalawnchair

search functionality for phonegap app with lawnchair db


How do i do (title)?

I've looked at the plugin "docs" on http://brian.io/lawnchair/plugins/ and with that i could see doing a search for equality, but i dont see something for .indexOf()

use case: I want to search my db of articles for every article containing the keyword "obama" at the minimum i'd like to see every article object with "obama" in the body field of the article.

If having keywords associated with each article would help i could add that in too, my main problem atm is just doing the search.


Solution

  • I ended up using regular expressions and iterating over the whole database: If anyone has a better idea, or cleaner code, feel free to contribute.

    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())
                });
            });