--- template: overrides/main.html search: boost: 1.05 --- # Setting up site search Material for MkDocs provides an excellent, client-side search implementation, omitting the need for the integration of third-party services, which might be tricky to integrate to be compliant with data privacy regulations. Moreover, with some effort, search can be made available [offline][1]. [1]: #offline-search ## Configuration ### Built-in search !!! danger "[Search: better, faster, smaller](../blog/2021/search-better-faster-smaller.md)" We rebuilt the search plugin and integration from the ground up, introducing [rich search previews](../blog/2021/search-better-faster-smaller.md#rich-search-previews), much better [tokenizer support](../blog/2021/search-better-faster-smaller.md#tokenizer-lookahead), [more accurate highlighting](../blog/2021/search-better-faster-smaller.md#accurate-highlighting) and much more. Read the [blog article](../blog/2021/search-better-faster-smaller.md) to learn more about our new search implementation. Start using it immediately by [becoming a sponsor][20]! [:octicons-file-code-24: Source][2] · [:octicons-cpu-24: Plugin][3] · [:octicons-heart-fill-24:{ .mdx-heart } Better in Insiders][20]{ .mdx-insiders } The [built-in search plugin][3] integrates seamlessly with Material for MkDocs, adding multilingual client-side search with [lunr][4] and [lunr-languages][5]. It's enabled by default, but must be re-added to `mkdocs.yml` when other plugins are used: ``` yaml plugins: - search ``` The following options are supported: `lang`{ #lang } : :octicons-milestone-24: Default: _automatically set_ – This option allows to include the language-specific stemmers provided by [lunr-languages][5]. Note that Material for MkDocs will set this automatically based on the [site language][6], but it may be overridden, e.g. to support multiple languages: === "A single language" ``` yaml plugins: - search: lang: ru ``` === "Multiple languages" ``` yaml plugins: - search: lang: - en - ru ``` The following languages are supported:
The content of this section is included
", "title":"Section 1" } ] } ``` [22]: ../reference/images.md#attribute-list #### Excluding blocks If even more fine-grained control is needed, content blocks can be excluded from search sections with a `{ data-search-exclude }` pragma, so they will not be included in the search index: === "`docs/page.md`" ``` markdown # Document title The content of this block is included The content of this block is excluded { data-search-exclude } ``` === "`search_index.json`" ``` json { ... "docs": [ { "location":"page/", "text":"The content of this block is included
", "title":"Document title" } ] } ``` ## Customization The search implementation of Material for MkDocs is probably its most sophisticated feature, as it tries to balance a _great typeahead experience_, _good performance_, _accessibility_, and a result list that is _easy to scan_. This is where Material for MkDocs deviates from other themes. The following section explains how search can be customized to tailor it to your needs. ### Query transformation [:octicons-file-code-24: Source][23] · :octicons-mortar-board-24: Difficulty: _easy_ When a user enters a query into the search box, the query is pre-processed before it is submitted to the search index. Material for MkDocs will apply the following transformations, which can be customized by [extending the theme][24]: ``` ts export function defaultTransform(query: string): string { return query .split(/"([^"]+)"/g) /* (1) */ .map((terms, index) => index & 1 ? terms.replace(/^\b|^(?![^\x00-\x7F]|$)|\s+/g, " +") : terms ) .join("") .replace(/"|(?:^|\s+)[*+\-:^~]+(?=\s+|$)/g, "") /* (2) */ .trim() /* (3) */ } ``` 1. Search for terms in quotation marks and prepend a `+` modifier to denote that the resulting document must contain all terms, converting the query to an `AND` query (as opposed to the default `OR` behavior). While users may expect terms enclosed in quotation marks to map to span queries, i.e. for which order is important, `lunr` doesn't support them, so the best we can do is to convert the terms to an `AND` query. 2. Replace control characters which are not located at the beginning of the query or preceded by white space, or are not followed by a non-whitespace character or are at the end of the query string. Furthermore, filter unmatched quotation marks. 3. Trim excess whitespace from left and right. If you want to switch to the default behavior of the `mkdocs` and `readthedocs` themes, both of which don't transform the query prior to submission, or customize the `transform` function, you can do this by [overriding the `config` block][25]: ``` html {% extends "base.html" %} {% block config %} {{ super() }} {% endblock %} ``` The `transform` function will receive the query string as entered by the user and must return the processed query string to be submitted to the search index. [23]: https://github.com/squidfunk/mkdocs-material/blob/master/src/assets/javascripts/integrations/search/transform/index.ts [24]: ../customization.md#extending-the-theme [25]: ../customization.md#overriding-blocks-recommended ### Custom search [:octicons-file-code-24: Source][26] · :octicons-mortar-board-24: Difficulty: _challenging_ Material for MkDocs implements search as part of a [web worker][27]. If you want to switch the web worker with your own implementation, e.g. to submit search to an external service, you can add a custom JavaScript file to the `docs` directory and [override the `config` block][24]: ``` html {% block config %} {{ super() }} {% endblock %} ``` Communication with the search worker is implemented using a designated message format using _discriminated unions_, i.e. through the `type` property of the message. See the following interface definitions to learn about the message formats: - [:octicons-file-code-24: `SearchMessage`][28] - [:octicons-file-code-24: `SearchIndex` and `SearchResult`][29] The sequence and direction of messages is rather intuitive: - :octicons-arrow-right-24: `SearchSetupMessage` - :octicons-arrow-left-24: `SearchReadyMessage` - :octicons-arrow-right-24: `SearchQueryMessage` - :octicons-arrow-left-24: `SearchResultMessage` [26]: https://github.com/squidfunk/mkdocs-material/blob/master/src/assets/javascripts/integrations/search/worker [27]: https://developer.mozilla.org/en-US/docs/Web/API/Web_Workers_API/Using_web_workers [28]: https://github.com/squidfunk/mkdocs-material/blob/master/src/assets/javascripts/integrations/search/worker/message/index.ts [29]: https://github.com/squidfunk/mkdocs-material/blob/master/src/assets/javascripts/integrations/search/_/index.ts