Skip to content

Commit 1e4f80d

Browse files
authored
[search] fix partial matches overwriting full matches (#11958)
1 parent 7f582a5 commit 1e4f80d

File tree

3 files changed

+15
-9
lines changed

3 files changed

+15
-9
lines changed

CHANGES.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ Features added
3333
Bugs fixed
3434
----------
3535

36+
* #11958: HTML Search: Fix partial matches overwriting full matches.
37+
Patch by William Lachance.
3638
* #11944: Use anchor in search preview.
3739
Patch by Will Lachance.
3840
* #11668: Raise a useful error when ``theme.conf`` is missing.

sphinx/themes/basic/static/searchtools.js

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -477,14 +477,18 @@ const Search = {
477477
// add support for partial matches
478478
if (word.length > 2) {
479479
const escapedWord = _escapeRegExp(word);
480-
Object.keys(terms).forEach((term) => {
481-
if (term.match(escapedWord) && !terms[word])
482-
arr.push({ files: terms[term], score: Scorer.partialTerm });
483-
});
484-
Object.keys(titleTerms).forEach((term) => {
485-
if (term.match(escapedWord) && !titleTerms[word])
486-
arr.push({ files: titleTerms[word], score: Scorer.partialTitle });
487-
});
480+
if (!terms.hasOwnProperty(word)) {
481+
Object.keys(terms).forEach((term) => {
482+
if (term.match(escapedWord))
483+
arr.push({ files: terms[term], score: Scorer.partialTerm });
484+
});
485+
}
486+
if (!titleTerms.hasOwnProperty(word)) {
487+
Object.keys(titleTerms).forEach((term) => {
488+
if (term.match(escapedWord))
489+
arr.push({ files: titleTerms[word], score: Scorer.partialTitle });
490+
});
491+
}
488492
}
489493

490494
// no match but word was a required one

tests/js/searchtools.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ describe('Basic html theme search', function() {
2121
"<no title>",
2222
"",
2323
null,
24-
2,
24+
5,
2525
"index.rst"
2626
]];
2727
expect(Search.performTermsSearch(searchterms, excluded, terms, titleterms)).toEqual(hits);

0 commit comments

Comments
 (0)