lunr: improve multiterm search #407

This commit is contained in:
Sören Weber 2024-10-27 12:37:35 +01:00
parent 57b73a5f47
commit 6a20332b51
No known key found for this signature in database
GPG key ID: BEC6D55545451B6D
2 changed files with 4 additions and 4 deletions

View file

@ -1 +1 @@
7.1.1+8debc83a770553885b78318e2651a127a82d2b0c
7.1.1+57b73a5f47d69d695fba57ec966ae7dd25400a66

View file

@ -57,7 +57,7 @@ Each array item needs the following layout:
let lunrIndex, pagesIndex;
function init() {
function initLunrIndex( index ){
function initIndex( index ){
pagesIndex = index;
// Set up Lunr by declaring the fields we use
// Also provide their boost level for the ranking
@ -93,7 +93,7 @@ function init() {
js.src = index_js_url;
js.setAttribute("async", "");
js.onload = function(){
initLunrIndex(relearn_searchindex);
initIndex(relearn_searchindex);
};
js.onerror = function(e){
console.error('Error getting Hugo index file');
@ -129,7 +129,7 @@ function search(term) {
term = term.replace( /[*:^~+-]/g, ' ' );
var searchTerm = lunr.tokenizer( term ).reduce( function(a,token){return a.concat(searchPatterns(token.str))}, []).join(' ');
return !searchTerm || !lunrIndex ? [] : lunrIndex.search(searchTerm).map(function(result) {
return { index: result.ref, matches: Object.keys(result.matchData.metadata), page: pagesIndex[ result.ref ] };
return { index: result.ref, matches: [ term, ...Object.keys(result.matchData.metadata) ], page: pagesIndex[ result.ref ] };
});
}