How to Set Up Russian Search in MkDocs
Summary
The default MkDocs search is poor at Russian: a query in one grammatical form does not match pages that use another form of the word (the Russian examples in this guide are transliterated). This can be fixed by connecting lunr with Russian morphology. Here is a step-by-step setup guide.
Why the Standard Search Fails
MkDocs search is built on the lunr library. It works well with English because it can strip endings (running → run). However, Russian morphology is not included in the default build.
Result: the search works as an exact match, and Russian words have many endings. A user searches for 'vektor', but pages containing 'vektory', 'vektora' or 'vektorny' are not found.
What You Need for Russian Search
The setup is as follows:
flowchart LR
A[Search Query] --> B[lunr.multiLanguage]
B --> C[English Stemmer]
B --> D[Russian Stemmer]
C --> E[Document Index]
D --> E
lunr.multiLanguage('en', 'ru') builds an index where each word is processed by both an English and a Russian stemmer. Thus, the search works on both languages simultaneously.
Step 1. Download lunr Files
You need four files:
lunr.min.js— the core;lunr.stemmer.support.js— base for stemmers;lunr.ru.js— Russian stemmer;lunr.multi.js— multilingual mode.
You can get them from lunr-languages — the official repository with language packages.
Place the files in your project folder, for example:
docs/javascripts/
lunr.min.js
lunr.stemmer.support.js
lunr.ru.js
lunr.multi.js
Step 2. Link Scripts in mkdocs.yml
extra_javascript:
- javascripts/lunr.min.js
- javascripts/lunr.stemmer.support.js
- javascripts/lunr.ru.js
- javascripts/lunr.multi.js
Order matters: core first, then stemmer support, then Russian package, then multilingual module.
Step 3. Override Search Index
MkDocs standard search builds the index differently. To use multiLanguage, you need to override the JS function that builds the index.
Create a file docs/javascripts/ru-search.js:
document$.subscribe(function () {
if (typeof lunr === "undefined" || typeof lunr.multiLanguage !== "function") {
return;
}
if (window.__search) {
// Override the index-building function.
var original = window.__search.override;
window.__search.override = function () {
var idx = lunr.multiLanguage("en", "ru");
this.field("title", { boost: 10 });
this.field("text");
this.ref("location");
return idx;
};
if (typeof original === "function") {
original.call(window.__search);
}
}
});
Link it after the others:
extra_javascript:
- javascripts/lunr.min.js
- javascripts/lunr.stemmer.support.js
- javascripts/lunr.ru.js
- javascripts/lunr.multi.js
- javascripts/ru-search.js
Step 4. Check
mkdocs serve
Open the site and enter several forms of a word in search:
vektor— should find 'vektory';koshka— should find 'koshki';obuchenie— should find 'obucheniya'.
If word forms are found — Russian search works.
How It Works Internally
The stemmer reduces the word to its base:
- 'vektory' → 'vektor';
- 'obucheniya' → 'obuchen'.
The index stores the base, not the original form. Therefore, when searching for 'obuchenie', the query is also reduced to the base and matches 'obucheniya', 'obucheniyu', etc.
This is not a full morphological analysis but rather stemming: stripping typical endings. For a knowledge base, this is sufficient — the search becomes noticeably friendlier to the Russian language.
Common Issues
1. Search Stopped Working Altogether
Check the browser console: if lunr.multiLanguage is not a function, the files are linked in the wrong order or lunr.multi.js did not load.
2. Russian Works, but English Doesn't
Make sure you use lunr.multiLanguage('en', 'ru') and not lunr.ru alone. The multilingual mode keeps both stemmers.
3. Nothing Changed After Setup
The browser may have cached old scripts. Hard refresh the page: Ctrl+F5.