Skip to content

Commit ae51974

Browse files
authored
[search] fix multiple term matching edge case (#11960)
1 parent 1e4f80d commit ae51974

File tree

3 files changed

+31
-3
lines changed

3 files changed

+31
-3
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+
* #11959: Fix multiple term matching when word appears in both title and document.
37+
Patch by Will Lachance.
3638
* #11958: HTML Search: Fix partial matches overwriting full matches.
3739
Patch by William Lachance.
3840
* #11944: Use anchor in search preview.

sphinx/themes/basic/static/searchtools.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -511,9 +511,8 @@ const Search = {
511511

512512
// create the mapping
513513
files.forEach((file) => {
514-
if (fileMap.has(file) && fileMap.get(file).indexOf(word) === -1)
515-
fileMap.get(file).push(word);
516-
else fileMap.set(file, [word]);
514+
if (!fileMap.has(file)) fileMap.set(file, [word]);
515+
else if (fileMap.get(file).indexOf(word) === -1) fileMap.get(file).push(word);
517516
});
518517
});
519518

tests/js/searchtools.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,33 @@ describe('Basic html theme search', function() {
2727
expect(Search.performTermsSearch(searchterms, excluded, terms, titleterms)).toEqual(hits);
2828
});
2929

30+
it('should be able to search for multiple terms', function() {
31+
index = {
32+
alltitles: {
33+
'Main Page': [[0, 'main-page']],
34+
},
35+
docnames:["index"],
36+
filenames:["index.rst"],
37+
terms:{main:0, page:0},
38+
titles:["Main Page"],
39+
titleterms:{ main:0, page:0 }
40+
}
41+
Search.setIndex(index);
42+
43+
searchterms = ['main', 'page'];
44+
excluded = [];
45+
terms = index.terms;
46+
titleterms = index.titleterms;
47+
hits = [[
48+
'index',
49+
'Main Page',
50+
'',
51+
null,
52+
15,
53+
'index.rst']];
54+
expect(Search.performTermsSearch(searchterms, excluded, terms, titleterms)).toEqual(hits);
55+
});
56+
3057
});
3158

3259
});

0 commit comments

Comments
 (0)