How to use Search in Docusaurus with Algolia Docsearch

Introduction#

I use Algolia Docsearch on this docs website. It is free to use on Algolia's Community plan, but you need to display Algolia's logo in your search console. Thank you Algolia! ๐Ÿ™๐Ÿผ

The official tutorial on how to self-host Algolia Docsearch can be found here. I use the crawler from the code base, but the Docker mode is probably easier.

Setup#

When setting up the pipenv, I had some trouble running pipenv install. This command helped:

pipenv --python=/home/linuxbrew/.linuxbrew/opt/[email protected]/bin/python3

It was also helpful to change the required Python version in the Pipfile to the version I had. In my case, it was 3.8.5.

You can create your config file using their bootstrap or create one based off of the Docusaurus v2 config file. You can remove everything between lines 20 and 31.

As a result, my config.json is something like this:

{
"index_name": "docs.theochu.com",
"start_urls": ["https://docs.theochu.com/"],
"sitemap_urls": ["https://docs.theochu.com/sitemap.xml"],
"sitemap_alternate_links": true,
"stop_urls": [
"https://docs.theochu.com/doc1",
"https://docs.theochu.com/doc2",
"https://docs.theochu.com/doc3",
"https://docs.theochu.com/mdx"
],
"selectors": {
"lvl0": "header h1",
"lvl1": "article h2",
"lvl2": "article h3",
"lvl3": "article h4",
"lvl4": "article h5",
"lvl5": "article h6",
"text": "article p, article li, article i, article a, article td"
},
"strip_chars": " .,;:#{}[]",
"nb_hits": 960
}

Run#

If you have the config.json file in your root directory, run pipenv shell, then run

./docsearch run ./config.json

rather than ./docsearch run config.json. The ./ indicates the current directory.

When you put the Algolia information into your docusaurus.config.json, make sure you put in the appId as well as the apiKey and indexName. You can see what I mean in this pull request or this commit for the pull request.

Last updated on