From ca9eba8d3e94ab24b546722aae6f1cf92f8d2057 Mon Sep 17 00:00:00 2001 From: Steve Fenton Date: Tue, 5 Nov 2024 14:38:57 +0000 Subject: [PATCH] Weighted scoring --- public/js/search.js | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/public/js/search.js b/public/js/search.js index 64573e0..feb71d9 100644 --- a/public/js/search.js +++ b/public/js/search.js @@ -72,6 +72,7 @@ function initializeSearch() { var currentQuery = ''; var dataUrl = siteSearchElement.dataset.sourcedata; + // Legacy scoring var scoring = { depth: 5, phraseTitle: 60, @@ -84,6 +85,14 @@ function initializeSearch() { termKeywords: 15, }; + // Found word scoring + const scores = { + titleExact: 20, + titleContains: 15, + headingContains: 10, + contentContains: 1, + }; + var ready = false; var scrolled = false; @@ -321,12 +330,12 @@ function initializeSearch() { // Title if (item.safeTitle === currentQuery) { - item.foundWords += 2; + item.foundWords += scores.titleExact; } if (contains(item.safeTitle, currentQuery)) { item.score = item.score + scoring.phraseTitle; - item.foundWords += 2; + item.foundWords += scores.titleContains; } // Headings @@ -334,14 +343,14 @@ function initializeSearch() { if (contains(c.safeText, currentQuery)) { item.score = item.score + scoring.phraseHeading; item.matchedHeadings.push(c); - item.foundWords++; + item.foundWords += scores.headingContains; } }); // Description if (contains(item.description, currentQuery)) { item.score = item.score + scoring.phraseDescription; - item.foundWords++; + item.foundWords += scores.contentContains; } // Part 2 - Term Matches, i.e. "Kitchen" or "Sink"