From b575ca0eea8728f156f2180d2829a6f297e55976 Mon Sep 17 00:00:00 2001 From: Soichi Hayashi Date: Tue, 10 Aug 2021 09:53:17 -0400 Subject: [PATCH] Update index.js doks was displaying only the very last search result. --- assets/js/index.js | 45 +++++++++++++++++++++------------------------ 1 file changed, 21 insertions(+), 24 deletions(-) diff --git a/assets/js/index.js b/assets/js/index.js index 0df2fd296..248b22b3f 100644 --- a/assets/js/index.js +++ b/assets/js/index.js @@ -117,39 +117,36 @@ Source: suggestions.addEventListener('click', accept_suggestion, true); function show_results(){ + const maxResult = 5; var value = this.value; - var results = index.search(value, { limit: 5, index: ["content"], enrich: true }); - var entry, childs = suggestions.childNodes; - var i = 0, len = results.length; + var results = index.search(value, {limit: maxResult, enrich: true}); suggestions.classList.remove('d-none'); + suggestions.innerHTML = ""; + + //flatSearch now returns results for each index field. create a single list + const flatResults = {}; //keyed by href to dedupe results + results.forEach(result=>{ + result.result.forEach(r=>{ + flatResults[r.doc.href] = r.doc; + }); + }); - results.forEach(function(results) { - - entry = document.createElement('div'); - - entry.innerHTML = ''; - - a = entry.querySelector('a'), - t = entry.querySelector('span:first-child'), - d = entry.querySelector('span:nth-child(2)'); - - // console.log(results); - - a.href = results.result[i].doc.href; - t.textContent = results.result[i].doc.title; - d.textContent = results.result[i].doc.description; - - suggestions.appendChild(entry); + //construct a list of suggestions list + for(const href in flatResults) { + const doc = flatResults[href]; - }); + const entry = document.createElement('div'); + entry.innerHTML = ''; - while(childs.length > len){ + entry.querySelector('a').href = href; + entry.querySelector('span:first-child').textContent = doc.title; + entry.querySelector('span:nth-child(2)').textContent = doc.description; - suggestions.removeChild(childs[i]) + suggestions.appendChild(entry); + if(suggestions.childElementCount == maxResult) break; } - } function accept_suggestion(){