Search
Docusaurus' own @docusaurus/preset-classic
supports a search integration.
There are two main options, you can use Algolia DocSearch or bring in your own SearchBar
component.
#
Using Algolia DocSearchAlgolia DocSearch works by crawling the content of your website every 24 hours and putting all the content in an Algolia index. This content is then queried directly from your front-end using the Algolia API. Note that your website needs to be publicly available for this to work (i.e., not behind a firewall). The service is free.
If your website is not eligible for the free, hosted version of DocSearch, or if your website sits behind a firewall, then you can run your own DocSearch crawler. For best results, you may want to use a config file based on the Docusaurus 2 config.
#
Connecting AlgoliaTo connect your docs with Algolia, add an algolia
field in your themeConfig
. Apply for DocSearch to get your Algolia index and API key.
info
The searchParameters
option used to be named algoliaOptions
in Docusaurus v1.
#
Contextual searchContextual search is mostly useful for versioned Docusaurus sites.
Let's consider you have 2 docs versions, v1 and v2. When you are browsing v2 docs, it would be odd to return search results for the v1 documentation. Sometimes v1 and v2 docs are quite similar, and you would end up with duplicate search results for the same query (one result per version).
To solve this problem, the contextual search feature understands that you are browsing a specific docs version, and will create the search query filters dynamically.
- browsing
/docs/v1/myDoc
, search results will only include v1 docs (+ other unversioned pages) - browsing
/docs/v2/myDoc
, search results will only include v2 docs (+ other unversioned pages)
caution
When using contextualSearch: true
, the contextual facet filters will be merged with the ones provided with algolia.searchParameters.facetFilters
.
#
Custom Application IDWhen running your own DocSearch crawler, it is required to set the appId
configuration key to your own Application ID. If left unset, the appId
will fallback to the one used with the free, hosted version of Algolia DocSearch.
#
Styling your Algolia searchBy default, DocSearch comes with a fine-tuned theme that was designed for accessibility, making sure that colors and contrasts respect standards.
Still, you can reuse the Infima CSS variables from Docusaurus to style DocSearch by editing the /src/css/custom.css
file.
#
Customizing the Algolia search behaviorAlgolia DocSearch supports a list of options that you can pass to the algolia
field in the docusaurus.config.js
file.
#
Editing the Algolia search componentIf you prefer to edit the Algolia search React component, swizzle the SearchBar
component in @docusaurus/theme-search-algolia
:
- npm
- Yarn
#
Using your own searchTo use your own search, swizzle the SearchBar
component in @docusaurus/theme-classic
- npm
- Yarn
This will create a src/themes/SearchBar
file in your project folder. Restart your dev server and edit the component, you will see that Docusaurus uses your own SearchBar
component now.
Notes: You can alternatively swizzle from Algolia SearchBar and create your own search component from there.