javascript - Limting Search Results in Jekyll Search -


i've been trying without success limit results in search script included in site.

here original script:

jquery(function() {    // initialize lunr fields searched, plus boost.    window.idx = lunr(function () {      this.field('id');      this.field('title');      this.field('content', { boost: 10 });      this.field('categories');    });      // generated search_data.json file lunr.js can search locally.    window.data = $.getjson('/search.json');      // wait data load , add lunr    window.data.then(function(loaded_data){      $.each(loaded_data, function(index, value){        window.idx.add(          $.extend({ "id": index }, value)        );      });    });      // event when form submitted    $("#site_search").submit(function(event){        event.preventdefault();        var query = $("#search_box").val(); // value text field        var results = window.idx.search(query); // lunr perform search        display_search_results(results); // hand results off displayed    });      function display_search_results(results) {      var $search_results = $("#search_results");        // wait data load      window.data.then(function(loaded_data) {          // there results?        if (results.length) {          $search_results.empty(); // clear old results            // iterate on results          results.foreach(function(result) {            var item = loaded_data[result.ref];              // build snippet of html result            var appendstring = '<li><a href="' + item.url + '">' + item.title + '</a></li>';              // add snippet collection of results.            $search_results.append(appendstring);          });        } else {          // if there no results, let user know.          $search_results.html('<li><b><u>no results found</u></b></li>');        }      });    }  });

and i've tried without success include limiting statement when iterating on results:

        // iterate on results     (var = 0; < results.length && < 5; i++)  {          var item = loaded_date[results[i]]; 

i've fiddled around for quite time, , can't seem find amiss.

any assistance appreciated.

-d

what error getting, loop looks fine.

an alternative approach use array#slice limit number of results iterate over.

results.slice(0, 5).foreach(function (result) {   // snip }) 

replace existing iteration on results, have results.foreach.

this work if there less 5 results, , take first 5 if there more 5 results.


Comments

Popular posts from this blog

sequelize.js - Sequelize group by with association includes id -

android - Robolectric "INTERNET permission is required" -

java - Android raising EPERM (Operation not permitted) when attempting to send UDP packet after network connection -