From bfce4f54b0fc010b04f0d17ad8b9d47d8c279e1f Mon Sep 17 00:00:00 2001 From: James Addison <55152140+jayaddison@users.noreply.github.com> Date: Tue, 26 Mar 2024 11:18:26 +0000 Subject: [PATCH] [HTML search] include partially-matched document titles in search results (#12041) Co-authored-by: William Lachance --- CHANGES.rst | 3 +++ sphinx/themes/basic/static/searchtools.js | 2 +- tests/js/searchtools.js | 25 +++++++++++++++++++++++ 3 files changed, 29 insertions(+), 1 deletion(-) diff --git a/CHANGES.rst b/CHANGES.rst index f5dc2d3c412..31e0c8acbd4 100644 --- a/CHANGES.rst +++ b/CHANGES.rst @@ -139,6 +139,9 @@ Bugs fixed * #10786: improve the error message when a file to be copied (e.g., an asset) is removed during Sphinx execution. Patch by Bénédikt Tran. +* #12040: HTML Search: Ensure that document titles that are partially-matched by + the user search query are included in search results. + Patch by James Addison. Testing ------- diff --git a/sphinx/themes/basic/static/searchtools.js b/sphinx/themes/basic/static/searchtools.js index 8e4650f2236..388058dab50 100644 --- a/sphinx/themes/basic/static/searchtools.js +++ b/sphinx/themes/basic/static/searchtools.js @@ -508,7 +508,7 @@ const Search = { if (!titleTerms.hasOwnProperty(word)) { Object.keys(titleTerms).forEach((term) => { if (term.match(escapedWord)) - arr.push({ files: titleTerms[word], score: Scorer.partialTitle }); + arr.push({ files: titleTerms[term], score: Scorer.partialTitle }); }); } } diff --git a/tests/js/searchtools.js b/tests/js/searchtools.js index 8cbd796b860..4f9984dd4e9 100644 --- a/tests/js/searchtools.js +++ b/tests/js/searchtools.js @@ -54,6 +54,31 @@ describe('Basic html theme search', function() { expect(Search.performTermsSearch(searchterms, excluded, terms, titleterms)).toEqual(hits); }); + it('should partially-match "sphinx" when in title index', function() { + index = { + docnames:["index"], + filenames:["index.rst"], + terms:{'useful': 0, 'utilities': 0}, + titles:["sphinx_utils module"], + titleterms:{'sphinx_utils': 0} + } + Search.setIndex(index); + searchterms = ['sphinx']; + excluded = []; + terms = index.terms; + titleterms = index.titleterms; + + hits = [[ + "index", + "sphinx_utils module", + "", + null, + 7, + "index.rst" + ]]; + expect(Search.performTermsSearch(searchterms, excluded, terms, titleterms)).toEqual(hits); + }); + }); });