Refactor show_results (flexsearch) to be more descriptive

This commit is contained in:
Michael Schnerring 2021-10-15 15:12:42 +02:00
parent cd1f9f5811
commit 7debd0ae62
1 changed files with 21 additions and 13 deletions

View File

@ -121,31 +121,39 @@ Source:
function show_results(){ function show_results(){
const maxResult = 5; const maxResult = 5;
var searchQuery = this.value;
var results = index.search(searchQuery, {limit: maxResult, enrich: true});
var value = this.value; // flatten results since index.search() returns results for each indexed field
var results = index.search(value, {limit: maxResult, enrich: true}); const flatResults = {}; // keyed by href to dedupe results
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
for (const result of results.flatMap(r => r.result)) { for (const result of results.flatMap(r => r.result)) {
flatResults[result.doc.href] = result.doc; flatResults[result.doc.href] = result.doc;
} }
//construct a list of suggestions list suggestions.innerHTML = "";
suggestions.classList.remove('d-none');
// construct a list of suggestions
for(const href in flatResults) { for(const href in flatResults) {
const doc = flatResults[href]; const doc = flatResults[href];
const entry = document.createElement('div'); const entry = document.createElement('div');
entry.innerHTML = '<a href><span></span><span></span></a>'; suggestions.appendChild(entry);
entry.querySelector('a').href = href; const a = document.createElement('a');
entry.querySelector('span:first-child').textContent = doc.title; a.href = href;
entry.querySelector('span:nth-child(2)').textContent = doc.description; entry.appendChild(a);
const title = document.createElement('span');
title.textContent = doc.title;
a.appendChild(title);
const description = document.createElement('span');
description.textContent = doc.description;
a.appendChild(description);
suggestions.appendChild(entry); suggestions.appendChild(entry);
if(suggestions.childElementCount == maxResult) break; if(suggestions.childElementCount == maxResult) break;
} }
} }