Skip to content

Commit

Permalink
Weighted scoring
Browse files Browse the repository at this point in the history
  • Loading branch information
Steve-Fenton committed Nov 5, 2024
1 parent 53bfbd5 commit ca9eba8
Showing 1 changed file with 13 additions and 4 deletions.
17 changes: 13 additions & 4 deletions public/js/search.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ function initializeSearch() {
var currentQuery = '';
var dataUrl = siteSearchElement.dataset.sourcedata;

// Legacy scoring
var scoring = {
depth: 5,
phraseTitle: 60,
Expand All @@ -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;

Expand Down Expand Up @@ -321,27 +330,27 @@ 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
item.headings.forEach((c) => {
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"
Expand Down

0 comments on commit ca9eba8

Please sign in to comment.