From 5eaea64b866242123880ac0246ac22e6e7e5ba63 Mon Sep 17 00:00:00 2001 From: James Addison Date: Mon, 24 Jun 2024 11:22:28 +0100 Subject: [PATCH] [search] Refactor scoring logic adjustment: * Minimize diff relative to mainline codebase to ease review/historic viewing. * Move updated main-title score variable into a ``Scorer`` constant. --- sphinx/themes/basic/static/searchtools.js | 9 +++------ tests/js/searchtools.js | 2 +- 2 files changed, 4 insertions(+), 7 deletions(-) diff --git a/sphinx/themes/basic/static/searchtools.js b/sphinx/themes/basic/static/searchtools.js index a6701bf394b..b21446926e3 100644 --- a/sphinx/themes/basic/static/searchtools.js +++ b/sphinx/themes/basic/static/searchtools.js @@ -39,6 +39,7 @@ if (typeof Scorer === "undefined") { objPrioDefault: 0, // query found in title + mainTitle: 16, title: 15, partialTitle: 7, // query found in terms @@ -328,14 +329,10 @@ 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 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) + let score = Math.round(titles[file] !== title ? Scorer.title : Scorer.mainTitle * queryLower.length / title.length) normalResults.push([ docNames[file], - isMainTitle ? title : `${titles[file]} > ${title}`, + titles[file] !== title ? `${titles[file]} > ${title}` : title, id !== null ? "#" + id : "", null, score, diff --git a/tests/js/searchtools.js b/tests/js/searchtools.js index de61b20b3c4..c3b55c9c1fa 100644 --- a/tests/js/searchtools.js +++ b/tests/js/searchtools.js @@ -100,7 +100,7 @@ describe('Basic html theme search', function() { 'Main Page', '#main-page', null, - 17, + 16, 'index.rst' ] ];