Skip to content

Commit

Permalink
Refactor show_results (flexsearch) to be more descriptive
Browse files Browse the repository at this point in the history
  • Loading branch information
schnerring committed Oct 15, 2021
1 parent cd1f9f5 commit 7debd0a
Showing 1 changed file with 21 additions and 13 deletions.
34 changes: 21 additions & 13 deletions assets/js/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -121,31 +121,39 @@ Source:

function show_results(){
const maxResult = 5;
var searchQuery = this.value;
var results = index.search(searchQuery, {limit: maxResult, enrich: true});

var value = this.value;
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
// flatten results since index.search() returns results for each indexed field
const flatResults = {}; // keyed by href to dedupe results
for (const result of results.flatMap(r => r.result)) {
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) {
const doc = flatResults[href];

const entry = document.createElement('div');
entry.innerHTML = '<a href><span></span><span></span></a>';
suggestions.appendChild(entry);

const a = document.createElement('a');
a.href = href;
entry.appendChild(a);

entry.querySelector('a').href = href;
entry.querySelector('span:first-child').textContent = doc.title;
entry.querySelector('span:nth-child(2)').textContent = doc.description;
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);

if(suggestions.childElementCount == maxResult) break;
}
}
Expand Down

0 comments on commit 7debd0a

Please sign in to comment.