Skip to content

Commit

Permalink
[search] fix partial matches overwriting full matches (#11958)
Browse files Browse the repository at this point in the history
  • Loading branch information
wlach committed Mar 3, 2024
1 parent 7f582a5 commit 1e4f80d
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 9 deletions.
2 changes: 2 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ Features added
Bugs fixed
----------

* #11958: HTML Search: Fix partial matches overwriting full matches.
Patch by William Lachance.
* #11944: Use anchor in search preview.
Patch by Will Lachance.
* #11668: Raise a useful error when ``theme.conf`` is missing.
Expand Down
20 changes: 12 additions & 8 deletions sphinx/themes/basic/static/searchtools.js
Original file line number Diff line number Diff line change
Expand Up @@ -477,14 +477,18 @@ const Search = {
// add support for partial matches
if (word.length > 2) {
const escapedWord = _escapeRegExp(word);
Object.keys(terms).forEach((term) => {
if (term.match(escapedWord) && !terms[word])
arr.push({ files: terms[term], score: Scorer.partialTerm });
});
Object.keys(titleTerms).forEach((term) => {
if (term.match(escapedWord) && !titleTerms[word])
arr.push({ files: titleTerms[word], score: Scorer.partialTitle });
});
if (!terms.hasOwnProperty(word)) {
Object.keys(terms).forEach((term) => {
if (term.match(escapedWord))
arr.push({ files: terms[term], score: Scorer.partialTerm });
});
}
if (!titleTerms.hasOwnProperty(word)) {
Object.keys(titleTerms).forEach((term) => {
if (term.match(escapedWord))
arr.push({ files: titleTerms[word], score: Scorer.partialTitle });
});
}
}

// no match but word was a required one
Expand Down
2 changes: 1 addition & 1 deletion tests/js/searchtools.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ describe('Basic html theme search', function() {
"<no title>",
"",
null,
2,
5,
"index.rst"
]];
expect(Search.performTermsSearch(searchterms, excluded, terms, titleterms)).toEqual(hits);
Expand Down

0 comments on commit 1e4f80d

Please sign in to comment.