diff --git a/CHANGES.rst b/CHANGES.rst index 64ce57723..d39895a9f 100644 --- a/CHANGES.rst +++ b/CHANGES.rst @@ -33,6 +33,8 @@ Features added Bugs fixed ---------- +* #11959: Fix multiple term matching when word appears in both title and document. + Patch by Will Lachance. * #11958: HTML Search: Fix partial matches overwriting full matches. Patch by William Lachance. * #11944: Use anchor in search preview. diff --git a/sphinx/themes/basic/static/searchtools.js b/sphinx/themes/basic/static/searchtools.js index c5826b137..0e134912c 100644 --- a/sphinx/themes/basic/static/searchtools.js +++ b/sphinx/themes/basic/static/searchtools.js @@ -511,9 +511,8 @@ const Search = { // create the mapping files.forEach((file) => { - if (fileMap.has(file) && fileMap.get(file).indexOf(word) === -1) - fileMap.get(file).push(word); - else fileMap.set(file, [word]); + if (!fileMap.has(file)) fileMap.set(file, [word]); + else if (fileMap.get(file).indexOf(word) === -1) fileMap.get(file).push(word); }); }); diff --git a/tests/js/searchtools.js b/tests/js/searchtools.js index 82282e3e8..91c35a6ba 100644 --- a/tests/js/searchtools.js +++ b/tests/js/searchtools.js @@ -27,6 +27,33 @@ describe('Basic html theme search', function() { expect(Search.performTermsSearch(searchterms, excluded, terms, titleterms)).toEqual(hits); }); + it('should be able to search for multiple terms', function() { + index = { + alltitles: { + 'Main Page': [[0, 'main-page']], + }, + docnames:["index"], + filenames:["index.rst"], + terms:{main:0, page:0}, + titles:["Main Page"], + titleterms:{ main:0, page:0 } + } + Search.setIndex(index); + + searchterms = ['main', 'page']; + excluded = []; + terms = index.terms; + titleterms = index.titleterms; + hits = [[ + 'index', + 'Main Page', + '', + null, + 15, + 'index.rst']]; + expect(Search.performTermsSearch(searchterms, excluded, terms, titleterms)).toEqual(hits); + }); + }); });