Skip to content

Commit

Permalink
Update search to use Lunr 2.3.5
Browse files Browse the repository at this point in the history
Using advice from olivernn/lunr.js#287 to retain the
"autocomplete" behavior seen in earlier versions.
  • Loading branch information
johnfairh committed Feb 8, 2019
1 parent fdc2a4d commit 73abc98
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 15 deletions.
24 changes: 15 additions & 9 deletions lib/jazzy/themes/fullwidth/assets/js/jazzy.search.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,4 @@
$(function(){
var searchIndex = lunr(function() {
this.ref('url');
this.field('name');
this.field('abstract');
});

var $typeahead = $('[data-typeahead]');
var $form = $typeahead.parents('form');
var searchURL = $form.attr('action');
Expand All @@ -27,8 +21,13 @@ $(function(){
$form.addClass('loading');

$.getJSON(searchURL).then(function(searchData) {
$.each(searchData, function (url, doc) {
searchIndex.add({url: url, name: doc.name, abstract: doc.abstract});
const searchIndex = lunr(function() {
this.ref('url');
this.field('name');
this.field('abstract');
for (const [url, doc] of Object.entries(searchData)) {
this.add({url: url, name: doc.name, abstract: doc.abstract});
}
});

$typeahead.typeahead(
Expand All @@ -41,7 +40,14 @@ $(function(){
display: displayTemplate,
templates: { suggestion: suggestionTemplate },
source: function(query, sync) {
var results = searchIndex.search(query).map(function(result) {
const lcSearch = query.toLowerCase();
const results = searchIndex.query(function(q) {
q.term(lcSearch, { boost: 100 });
q.term(lcSearch, {
boost: 10,
wildcard: lunr.Query.wildcard.TRAILING
});
}).map(function(result) {
var doc = searchData[result.ref];
doc.url = result.ref;
return doc;
Expand Down

0 comments on commit 73abc98

Please sign in to comment.