From fbb62cfda9600d2caa4d6f097745d59cea3a431b Mon Sep 17 00:00:00 2001 From: Will Lachance Date: Sat, 25 May 2024 11:59:22 -0400 Subject: [PATCH] Boost title-matching scores if main title Related to #12391. Intended as a proof of concept, not a full solution. --- sphinx/themes/basic/static/searchtools.js | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/sphinx/themes/basic/static/searchtools.js b/sphinx/themes/basic/static/searchtools.js index eaed90953f4..a6701bf394b 100644 --- a/sphinx/themes/basic/static/searchtools.js +++ b/sphinx/themes/basic/static/searchtools.js @@ -328,10 +328,14 @@ const Search = { for (const [title, foundTitles] of Object.entries(allTitles)) { if (title.toLowerCase().trim().includes(queryLower) && (queryLower.length >= title.length/2)) { for (const [file, id] of foundTitles) { - let score = Math.round(100 * queryLower.length / title.length) + let isMainTitle = titles[file] === title + // score these a little bit above document matches, with more of a boost + // for main document titles + let baseScore = Scorer.title + (isMainTitle ? 2 : 1) + let score = Math.round(baseScore * queryLower.length / title.length) normalResults.push([ docNames[file], - titles[file] !== title ? `${titles[file]} > ${title}` : title, + isMainTitle ? title : `${titles[file]} > ${title}`, id !== null ? "#" + id : "", null, score,