From b4307e7c094c3a556e267b136e591c146a41f12f Mon Sep 17 00:00:00 2001 From: Alex Shpak Date: Sun, 20 Oct 2019 19:02:32 +0200 Subject: #80, Migrate to flexsearch --- assets/search.js | 39 +++++++++++++-------------------------- 1 file changed, 13 insertions(+), 26 deletions(-) (limited to 'assets/search.js') diff --git a/assets/search.js b/assets/search.js index 6799c8d..b9cbb11 100644 --- a/assets/search.js +++ b/assets/search.js @@ -1,18 +1,18 @@ {{- $searchData := resources.Get "search-data.js" | resources.ExecuteAsTemplate "search-data.js" . | resources.Minify | resources.Fingerprint }} (function() { - const input = document.querySelector("#book-search-input"); - const results = document.querySelector("#book-search-results"); + const input = document.querySelector('#book-search-input'); + const results = document.querySelector('#book-search-results'); - input.addEventListener("focus", init); - input.addEventListener("keyup", search); + input.addEventListener('focus', init); + input.addEventListener('keyup', search); function init() { - input.removeEventListener("focus", init); //init once + input.removeEventListener('focus', init); //init once input.required = true; - loadScript("{{ "lunr.min.js" | relURL }}"); - loadScript("{{ $searchData.RelPermalink }}", function() { + loadScript('{{ "flexsearch.light.js" | relURL }}'); + loadScript('{{ $searchData.RelPermalink }}', function() { input.required = false; search(); }); @@ -27,24 +27,11 @@ return; } - const terms = lunr.tokenizer(input.value); - const searchHits = window.bookSearch.idx.query(function(query) { - query.term(terms, { - boost: 100 - }); - query.term(terms, { - boost: 10, - wildcard: lunr.Query.wildcard.LEADING | lunr.Query.wildcard.TRAILING - }); - query.term(terms, { - editDistance: 2 - }); - }); - - searchHits.slice(0, 10).forEach(function(hit) { - const page = window.bookSearch.pages[hit.ref]; - const li = document.createElement("li"), - a = li.appendChild(document.createElement("a")); + let searchHits = window.bookSearch.index.search(input.value, 10); + searchHits.forEach(function(hit) { + const page = window.bookSearch.pages[hit]; + const li = document.createElement('li'), + a = li.appendChild(document.createElement('a')); a.href = page.href; a.textContent = page.title; @@ -54,7 +41,7 @@ } function loadScript(src, callback) { - const script = document.createElement("script"); + const script = document.createElement('script'); script.defer = true; script.async = false; script.src = src; -- cgit v1.2.3