diff --git a/sphinx/search/__init__.py b/sphinx/search/__init__.py index 6e21b2f5f..09430876b 100644 --- a/sphinx/search/__init__.py +++ b/sphinx/search/__init__.py @@ -397,7 +397,8 @@ class IndexBuilder(object): # again, stemmer must not remove words from search index if not _filter(stemmed_word) and _filter(word): stemmed_word = word - if stemmed_word not in self._title_mapping and _filter(stemmed_word): + already_indexed = docname in self._title_mapping.get(stemmed_word, []) + if _filter(stemmed_word) and not already_indexed: self._mapping.setdefault(stemmed_word, set()).add(docname) def context_for_searchtool(self): diff --git a/tests/roots/test-search/index.rst b/tests/roots/test-search/index.rst index 6383d9157..21fcdf53c 100644 --- a/tests/roots/test-search/index.rst +++ b/tests/roots/test-search/index.rst @@ -11,4 +11,10 @@ Stemmer ======= zfs -findthisstemmedkey \ No newline at end of file +findthisstemmedkey + +textinheading + +.. toctree:: + + tocitem \ No newline at end of file diff --git a/tests/roots/test-search/tocitem.rst b/tests/roots/test-search/tocitem.rst new file mode 100644 index 000000000..61c42a242 --- /dev/null +++ b/tests/roots/test-search/tocitem.rst @@ -0,0 +1,10 @@ +heading 1 +========= + +lorem ipsum + + +textinheading +============= + +lorem ipsum \ No newline at end of file diff --git a/tests/test_search.py b/tests/test_search.py index 63d78df46..a363b30be 100644 --- a/tests/test_search.py +++ b/tests/test_search.py @@ -105,3 +105,13 @@ def test_stemmer_does_not_remove_short_words(app, status, warning): def test_stemmer(app, status, warning): searchindex = (app.outdir / 'searchindex.js').text() assert 'findthisstemmedkei' in searchindex + + +@with_app(testroot='search') +def test_term_in_heading_and_section(app, status, warning): + searchindex = (app.outdir / 'searchindex.js').text() + # if search term is in the title of one doc and in the text of another + # both documents should be a hit in the search index as a title, + # respectively text hit + assert 'textinhead:1' in searchindex + assert 'textinhead:0' in searchindex \ No newline at end of file