mkdocs-material/docs/setup/setting-up-site-search.md

449 lines
13 KiB
Markdown
Raw Normal View History

2020-07-20 16:18:09 +03:00
---
search:
boost: 1.05
2020-07-20 16:18:09 +03:00
---
# Setting up site search
2022-01-25 22:06:53 +03:00
Material for MkDocs provides an excellent client-side search implementation,
2020-07-22 20:11:22 +03:00
omitting the need for the integration of third-party services, which might
2022-01-25 22:06:53 +03:00
not be compliant with privacy regulations. Moreover, search even works
[offline], allowing users to download your documentation.
2020-07-20 16:18:09 +03:00
2022-02-27 19:07:10 +03:00
[offline]: building-for-offline-usage.md
2020-07-20 16:18:09 +03:00
## Configuration
2022-02-27 15:19:44 +03:00
### Built-in search plugin
2020-07-20 16:18:09 +03:00
2022-09-11 20:25:40 +03:00
[:octicons-tag-24: 0.1.0][Search support] ·
2021-10-11 14:38:03 +03:00
:octicons-cpu-24: Plugin
2021-09-13 19:14:28 +03:00
2021-10-11 14:38:03 +03:00
The built-in search plugin integrates seamlessly with Material for MkDocs,
adding multilingual client-side search with [lunr] and [lunr-languages]. It's
enabled by default, but must be re-added to `mkdocs.yml` when other plugins
2020-07-20 16:18:09 +03:00
are used:
``` yaml
plugins:
- search
```
2021-10-11 14:38:03 +03:00
The following configuration options are supported:
2020-07-20 16:18:09 +03:00
2022-09-11 20:25:40 +03:00
[`lang`](#+search.lang){ #+search.lang }
2020-07-20 16:18:09 +03:00
2021-10-10 20:22:13 +03:00
: :octicons-milestone-24: Default: _automatically set_ This option allows
2021-10-11 14:38:03 +03:00
to include the language-specific stemmers provided by [lunr-languages].
2020-07-20 16:18:09 +03:00
Note that Material for MkDocs will set this automatically based on the
2021-10-11 14:38:03 +03:00
[site language], but it may be overridden, e.g. to support multiple
2020-07-20 16:18:09 +03:00
languages:
=== "A single language"
``` yaml
plugins:
- search:
2022-12-31 16:57:05 +03:00
lang: en
2020-07-20 16:18:09 +03:00
```
=== "Multiple languages"
``` yaml
plugins:
- search:
2021-12-11 16:30:07 +03:00
lang: # (1)!
2020-07-20 16:18:09 +03:00
- en
2022-12-31 16:57:05 +03:00
- de
2020-07-20 16:18:09 +03:00
```
2021-10-11 14:38:03 +03:00
1. Be aware that including support for other languages increases the
general JavaScript payload by around 20kb (before `gzip`) and by
another 15-30kb per language.
2023-01-30 12:51:45 +03:00
The following languages are supported by [lunr-languages]:
2020-07-20 16:18:09 +03:00
2021-10-05 00:36:31 +03:00
<div class="mdx-columns" markdown>
2020-12-21 19:38:58 +03:00
- `ar` Arabic
- `da` Danish
2022-01-29 18:11:11 +03:00
- `de` German
2020-12-21 19:38:58 +03:00
- `du` Dutch
- `en` English
2022-01-29 18:11:11 +03:00
- `es` Spanish
2020-12-21 19:38:58 +03:00
- `fi` Finnish
- `fr` French
2023-04-28 23:58:12 +03:00
- `hi` Hindi
2020-12-21 19:38:58 +03:00
- `hu` Hungarian
2023-04-28 23:58:12 +03:00
- `hy` Armenian
2020-12-21 19:38:58 +03:00
- `it` Italian
- `ja` Japanese
- `kn` - Kannada
2023-04-28 23:58:12 +03:00
- `ko` Korean
2020-12-21 19:38:58 +03:00
- `no` Norwegian
- `pt` Portuguese
- `ro` Romanian
- `ru` Russian
2023-04-29 15:30:19 +03:00
- `sa` Sanskrit
2020-12-21 19:38:58 +03:00
- `sv` Swedish
2023-04-28 23:58:12 +03:00
- `ta` Tamil
- `te` Telugu
2020-12-21 19:38:58 +03:00
- `th` Thai
- `tr` Turkish
- `vi` Vietnamese
2023-04-28 23:58:12 +03:00
- `zh` Chinese
2020-12-21 19:38:58 +03:00
</div>
2020-07-20 16:18:09 +03:00
2021-10-11 14:38:03 +03:00
Material for MkDocs goes to great lengths to support languages that are not
part of this list by automatically falling back to the stemmer yielding the
best result.
2020-07-20 16:18:09 +03:00
2022-09-11 20:25:40 +03:00
[`separator`](#+search.separator){ #+search.separator }
2020-07-20 16:18:09 +03:00
2021-10-10 20:22:13 +03:00
: :octicons-milestone-24: Default: _automatically set_ The separator for
2020-07-26 15:46:09 +03:00
indexing and query tokenization can be customized, making it possible to
index parts of words separated by other characters than whitespace and `-`,
e.g. by including `.`:
2020-07-20 16:18:09 +03:00
``` yaml
plugins:
- search:
2022-12-31 16:57:05 +03:00
separator: '[\s\-\.]+'
2020-07-20 16:18:09 +03:00
```
2022-12-31 16:57:05 +03:00
With :octicons-tag-24: 9.0.0, a faster and more flexible tokenizer method
is shipped, allowing for __tokenizing with lookahead__, which yields more
influence on the way documents are indexed. As a result, we use the
following separator setting for this site's search:
2020-07-20 16:18:09 +03:00
``` yaml
plugins:
- search:
2022-12-31 16:57:05 +03:00
separator: '[\s\-,:!=\[\]()"/]+|(?!\b)(?=[A-Z][a-z])|\.(?!\d)|&[lg]t;'
2020-07-20 16:18:09 +03:00
```
2021-10-02 16:53:47 +03:00
2022-12-31 16:57:05 +03:00
Broken into its parts, the separator induces the following behavior:
=== "Special characters"
2021-10-11 14:38:03 +03:00
2022-12-31 16:57:05 +03:00
```
[\s\-,:!=\[\]()"/]+
```
2021-10-11 14:38:03 +03:00
2022-12-31 16:57:05 +03:00
The first part of the expression inserts token boundaries for each
document before and after whitespace, hyphens, commas, brackets and
other special characters. If several of those special characters are
adjacent, they are treated as one.
=== "Case changes"
```
(?!\b)(?=[A-Z][a-z])
```
Many programming languages have naming conventions like `PascalCase` or
`camelCase`. By adding this subexpression to the separator,
[words are split at case changes], tokenizing the word `PascalCase`
into `Pascal` and `Case`.
[:octicons-arrow-right-24: Read more on tokenizing case changes]
[tokenize case changes]
=== "Version strings"
```
\.(?!\d)
```
When adding `.` to the separator, version strings like `1.2.3` are split
into `1`, `2` and `3`, which makes them undiscoverable via search. When
using this subexpression, a small lookahead is introduced which will
[preserve version strings] and keep them discoverable.
[:octicons-arrow-right-24: Read more on tokenizing version numbers]
[tokenize version numbers]
=== "HTML/XML tags"
```
&[lg]t;
```
If your documentation includes HTML/XML code examples, you may want to allow
users to find specific tag names. Unfortunately, the `<` and `>` control
characters are encoded in code blocks as `&lt;` and `&gt;`. Adding this
subexpression to the separator allows for just that.
[:octicons-arrow-right-24: Read more on tokenizing HTML/XML tags]
[tokenize html-xml tags]
2021-10-11 14:38:03 +03:00
2022-09-11 20:25:40 +03:00
[Search support]: https://github.com/squidfunk/mkdocs-material/releases/tag/0.1.0
2021-10-11 14:38:03 +03:00
[lunr]: https://lunrjs.com
[lunr-languages]: https://github.com/MihaiValentin/lunr-languages
[lunr's default tokenizer]: https://github.com/olivernn/lunr.js/blob/aa5a878f62a6bba1e8e5b95714899e17e8150b38/lunr.js#L413-L456
[site language]: changing-the-language.md#site-language
2022-12-31 16:57:05 +03:00
[words are split at case changes]: ?q=searchHighlight
[preserve version strings]: ?q=9.0.0
[tokenize case changes]: ../blog/posts/search-better-faster-smaller.md#case-changes
[tokenize version numbers]: ../blog/posts/search-better-faster-smaller.md#version-numbers
[tokenize html-xml tags]: ../blog/posts/search-better-faster-smaller.md#htmlxml-tags
2021-10-11 14:38:03 +03:00
2022-06-05 19:16:51 +03:00
#### Chinese language support
[:octicons-heart-fill-24:{ .mdx-heart } Sponsors only][Insiders]{ .mdx-insiders } ·
[:octicons-tag-24: insiders-4.14.0][Insiders] ·
:octicons-beaker-24: Experimental
[Insiders] adds search support for the Chinese language (see our [blog article]
[chinese search] from May 2022) by integrating with the text segmentation
library [jieba], which can be installed with `pip`.
``` sh
pip install jieba
```
If [jieba] is installed, the [built-in search plugin] automatically detects
Chinese characters and runs them through the segmenter. The following
configuration options are available:
2022-09-11 20:25:40 +03:00
[`jieba_dict`](#+search.jieba_dict){ #+search.jieba_dict }
2022-06-05 19:16:51 +03:00
: [:octicons-tag-24: insiders-4.17.2][Insiders] · :octicons-milestone-24:
Default: _none_ This option allows for specifying a [custom dictionary]
to be used by [jieba] for segmenting text, replacing the default dictionary:
``` yaml
plugins:
- search:
jieba_dict: dict.txt # (1)!
```
1. The following alternative dictionaries are provided by [jieba]:
- [dict.txt.small] 占用内存较小的词典文件
- [dict.txt.big] 支持繁体分词更好的词典文件
2022-09-11 20:25:40 +03:00
[`jieba_dict_user`](#+search.jieba_dict_user){ #+search.jieba_dict_user }
2022-06-05 19:16:51 +03:00
: [:octicons-tag-24: insiders-4.17.2][Insiders] · :octicons-milestone-24:
Default: _none_ This option allows for specifying an additional
[user dictionary] to be used by [jieba] for segmenting text, augmenting the
default dictionary:
``` yaml
plugins:
- search:
jieba_dict_user: user_dict.txt
```
User dictionaries can be used for tuning the segmenter to preserve
technical terms.
2022-12-31 16:57:05 +03:00
[Insiders]: ../insiders/index.md
2022-09-11 20:25:40 +03:00
[chinese search]: ../blog/posts/chinese-search-support.md
2022-06-05 19:16:51 +03:00
[jieba]: https://pypi.org/project/jieba/
[built-in search plugin]: #built-in-search-plugin
[custom dictionary]: https://github.com/fxsjy/jieba#%E5%85%B6%E4%BB%96%E8%AF%8D%E5%85%B8
[dict.txt.small]: https://github.com/fxsjy/jieba/raw/master/extra_dict/dict.txt.small
[dict.txt.big]: https://github.com/fxsjy/jieba/raw/master/extra_dict/dict.txt.big
[user dictionary]: https://github.com/fxsjy/jieba#%E8%BD%BD%E5%85%A5%E8%AF%8D%E5%85%B8
### Search suggestions
2022-09-11 20:25:40 +03:00
[:octicons-tag-24: 7.2.0][Search suggestions support] ·
:octicons-unlock-24: Feature flag ·
2021-10-11 14:38:03 +03:00
:octicons-beaker-24: Experimental
2021-10-10 22:04:22 +03:00
When search suggestions are enabled, the search will display the likeliest
2021-10-11 14:38:03 +03:00
completion for the last word which can be accepted with the ++arrow-right++ key.
2021-01-31 21:23:28 +03:00
Add the following lines to `mkdocs.yml`:
``` yaml
theme:
features:
- search.suggest
```
2022-09-11 20:25:40 +03:00
Searching for [:octicons-search-24: search su][Search suggestions example]
yields ^^search suggestions^^ as a suggestion.
2022-09-11 20:25:40 +03:00
[Search suggestions support]: https://github.com/squidfunk/mkdocs-material/releases/tag/7.2.0
[Search suggestions example]: ?q=search+su
### Search highlighting
2022-09-11 20:25:40 +03:00
[:octicons-tag-24: 7.2.0][Search highlighting support] ·
:octicons-unlock-24: Feature flag ·
2021-10-11 14:38:03 +03:00
:octicons-beaker-24: Experimental
2021-10-10 22:04:22 +03:00
When search highlighting is enabled and a user clicks on a search result,
2020-08-02 23:09:44 +03:00
Material for MkDocs will highlight all occurrences after following the link.
2021-01-31 21:23:28 +03:00
Add the following lines to `mkdocs.yml`:
``` yaml
theme:
features:
- search.highlight
```
2022-09-11 20:25:40 +03:00
Searching for [:octicons-search-24: code blocks][Search highlighting example]
2021-10-11 14:38:03 +03:00
highlights all occurrences of both terms.
2022-09-11 20:25:40 +03:00
[Search highlighting support]: https://github.com/squidfunk/mkdocs-material/releases/tag/7.2.0
[Search highlighting example]: ../reference/code-blocks.md?h=code+blocks
2020-12-30 21:02:02 +03:00
### Search sharing
2022-09-11 20:25:40 +03:00
[:octicons-tag-24: 7.2.0][Search sharing support] ·
2022-12-31 16:57:05 +03:00
:octicons-unlock-24: Feature flag
2020-12-30 21:02:02 +03:00
2021-10-10 22:04:22 +03:00
When search sharing is activated, a :material-share-variant: share button is
2020-12-30 21:02:02 +03:00
rendered next to the reset button, which allows to deep link to the current
2021-01-31 21:23:28 +03:00
search query and result. Add the following lines to `mkdocs.yml`:
2020-12-30 21:02:02 +03:00
``` yaml
theme:
features:
- search.share
```
When a user clicks the share button, the URL is automatically copied to the
clipboard.
2022-09-11 20:25:40 +03:00
[Search sharing support]: https://github.com/squidfunk/mkdocs-material/releases/tag/7.2.0
2020-12-30 21:02:02 +03:00
2021-05-12 10:29:40 +03:00
## Usage
### Search boosting
2021-05-12 10:29:40 +03:00
2022-12-31 16:57:05 +03:00
[:octicons-tag-24: 8.3.0][boost support]
2021-05-12 10:29:40 +03:00
Pages can be boosted in search with the front matter `search.boost` property,
which will make them rank higher. Add the following lines at the top of a
Markdown file:
2021-05-12 10:29:40 +03:00
2022-12-31 16:57:05 +03:00
=== ":material-arrow-up-circle: Rank up"
2021-05-12 10:29:40 +03:00
2022-12-31 16:57:05 +03:00
``` yaml
---
search:
boost: 2 # (1)!
---
# Document title
...
```
1. :woman_in_lotus_position: When boosting pages, be gentle and start with
__low values__.
=== ":material-arrow-down-circle: Rank down"
2021-05-12 10:29:40 +03:00
2022-12-31 16:57:05 +03:00
``` yaml
---
search:
boost: 0.5
---
# Document title
...
```
2021-10-11 14:38:03 +03:00
[boost support]: https://github.com/squidfunk/mkdocs-material/releases/tag/8.3.0
### Search exclusion
2022-12-31 16:57:05 +03:00
[:octicons-tag-24: 9.0.0][exclusion support] ·
2021-10-11 14:38:03 +03:00
:octicons-beaker-24: Experimental
Pages can be excluded from search with the front matter `search.exclude`
property, removing them from the index. Add the following lines at the top of a
2021-10-11 14:38:03 +03:00
Markdown file:
2022-09-11 20:25:40 +03:00
``` yaml
---
search:
exclude: true
---
# Document title
...
```
2022-12-31 16:57:05 +03:00
[exclusion support]: https://github.com/squidfunk/mkdocs-material/releases/tag/9.0.0
#### Excluding sections
2021-10-11 14:38:03 +03:00
When [Attribute Lists] is enabled, specific sections of pages can be excluded
2022-12-31 16:57:05 +03:00
from search by adding the `data-search-exclude` pragma after a Markdown
2021-10-11 14:38:03 +03:00
heading:
2022-09-11 20:25:40 +03:00
=== ":octicons-file-code-16: `docs/page.md`"
``` markdown
# Document title
## Section 1
The content of this section is included
## Section 2 { data-search-exclude }
The content of this section is excluded
```
2022-09-11 20:25:40 +03:00
=== ":octicons-codescan-16: `search_index.json`"
``` json
{
...
"docs": [
{
"location":"page/",
"text":"",
"title":"Document title"
},
{
"location":"page/#section-1",
"text":"<p>The content of this section is included</p>",
"title":"Section 1"
}
]
}
```
2021-10-11 14:38:03 +03:00
[Attribute Lists]: extensions/python-markdown.md#attribute-lists
#### Excluding blocks
2021-10-11 14:38:03 +03:00
When [Attribute Lists] is enabled, specific sections of pages can be excluded
2022-12-31 16:57:05 +03:00
from search by adding the `data-search-exclude` pragma after a Markdown
2021-10-11 14:38:03 +03:00
inline- or block-level element:
2022-09-11 20:25:40 +03:00
=== ":octicons-file-code-16: `docs/page.md`"
``` markdown
# Document title
The content of this block is included
The content of this block is excluded
{ data-search-exclude }
```
2022-09-11 20:25:40 +03:00
=== ":octicons-codescan-16: `search_index.json`"
``` json
{
...
"docs": [
{
"location":"page/",
"text":"<p>The content of this block is included</p>",
"title":"Document title"
}
]
}
```