Repository navigation
Conversation
lunr.js ships with a `stopWordFilter` that drops common English words such as "for", "if", "in", "is", "from", "while", and "not" from both the index pipeline and the search pipeline. These words overlap heavily with Python (and other language) keywords, so searching for them returned no results. Both the runtime worker and the Node pre-build script now call this.pipeline.remove(lunr.stopWordFilter); this.searchPipeline.remove(lunr.stopWordFilter); inside the lunr builder callback. Removing the filter from `searchPipeline` is equally important: without it, the query term itself is stripped before the index is consulted, so even a correctly built index would produce zero hits. Fixes mkdocs#4167
|
Friendly ping. Happy to add a test or adjust the approach if there's feedback. |
|
Thanks for the nudge. I've been working away on the next version of mkdocs. |
determine123
left a comment
There was a problem hiding this comment.
I checked commit 74065f5 with a small automated Node harness covering the runtime builder, the actual prebuild-index.js subprocess, and loading the serialized index.
The English case works as intended: a document containing for if in is from while not gives no hits for those queries before this change, and one hit per query after it. An ordinary documentation query still returns its expected document.
One scope detail: with config.lang: ["de"] or ["en", "de"], in still returns zero hits in all three patched paths. The German plugin installs its own stop-word filter, so removing lunr.stopWordFilter does not remove that filter. This is an existing limitation rather than a regression introduced here.
Could we add a regression test for English keyword search and clarify that this patch removes the English stop-word filter only? If the intended guarantee includes keyword searches on multilingual sites, the active language-specific filters would also need consideration. A useful fixture is {location: "keywords", title: "Python keywords", text: "for if in is from while not"}, checked with lang: ["en"], ["de"], and ["en", "de"], including serialization/load.
Validation was limited to these Node fixtures; I did not run the full MkDocs test suite or a browser UI test.
Fixes #4167.
Problem
lunr.jsincludes a built-instopWordFilterthat drops common Englishwords — "for", "if", "in", "is", "from", "while", "not", "will", etc. — from
both the index pipeline (at build time) and the search pipeline (at
query time).
Those words overlap heavily with Python keywords (and keywords in many other
languages), so searching for them on any MkDocs site returns no results. This
was first noticed in the Ruff documentation (astral-sh/ruff#21869) but affects
every MkDocs site built with the default search plugin.
Fix
Inside the
lunr()builder callback in both JS files, explicitly remove thestop-word filter from both pipelines before adding fields:
Removing it from
searchPipelineis equally important: even if the index isbuilt without stop-word filtering, a query term that is itself a stop word
would be stripped before the index is consulted, still producing zero hits.
The
if (lunr.stopWordFilter)guard makes the change safe when non-Englishlanguage plugins replace the pipeline (some of them unregister
stopWordFilterthemselves).
Files changed
mkdocs/contrib/search/templates/search/worker.jsmkdocs/contrib/search/prebuild-index.js