Skip to content

fix(search): remove lunr stop-word filter so programming keywords are indexed - #4218

Open
okxint wants to merge 1 commit into
mkdocs:masterfrom
okxint:fix/search-stop-word-filter-drops-keywords
Open

okxint wants to merge 1 commit into
mkdocs:masterfrom
okxint:fix/search-stop-word-filter-drops-keywords

Conversation

@okxint

@okxint okxint commented Sep 16, 2026

Copy link
Copy Markdown

Fixes #4167.

Problem

lunr.js includes a built-in stopWordFilter that drops common English
words — "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 the
stop-word filter from both pipelines before adding fields:

if (lunr.stopWordFilter) {
  this.pipeline.remove(lunr.stopWordFilter);
  this.searchPipeline.remove(lunr.stopWordFilter);
}

Removing it from searchPipeline is equally important: even if the index is
built 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-English
language plugins replace the pipeline (some of them unregister stopWordFilter
themselves).

Files changed

File Change
mkdocs/contrib/search/templates/search/worker.js Remove stop-word filter in the runtime Lunr builder
mkdocs/contrib/search/prebuild-index.js Remove stop-word filter in the Node pre-build script

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
@okxint

okxint commented Sep 24, 2026

Copy link
Copy Markdown
Author

Friendly ping. Happy to add a test or adjust the approach if there's feedback.

@lovelydinosaur

Copy link
Copy Markdown
Collaborator

Thanks for the nudge. I've been working away on the next version of mkdocs.
Indexing will work a bit differently there. Might be open to inviting contributors if you're interested?

@determine123 determine123 left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Searching for words that match Python keywords won't return anything

3 participants