Skip to content

Commit

Permalink
Boost title-matching scores if main title
Browse files Browse the repository at this point in the history
Related to sphinx-doc#12391. Intended as a proof of concept, not a full solution.
  • Loading branch information
wlach committed May 25, 2024
1 parent 48cbb43 commit fbb62cf
Showing 1 changed file with 6 additions and 2 deletions.
8 changes: 6 additions & 2 deletions sphinx/themes/basic/static/searchtools.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit fbb62cf

Please sign in to comment.