mirror of
https://github.com/facebook/docusaurus.git
synced 2025-04-29 02:08:36 +02:00
377 lines
15 KiB
Text
377 lines
15 KiB
Text
---
|
|
keywords:
|
|
- algolia
|
|
- search
|
|
---
|
|
|
|
# Search
|
|
|
|
There are a few options you can use to add search to your website:
|
|
|
|
- 🥇 [Algolia DocSearch](#using-algolia-docsearch) (**official**)
|
|
- 👥 [Typesense DocSearch](#using-typesense-docsearch)
|
|
- 👥 [Local Search](#using-local-search)
|
|
- 👥 [Your own `SearchBar` component](#using-your-own-search)
|
|
|
|
:::info
|
|
|
|
🥇 Docusaurus provides **first-class support** for [Algolia DocSearch](#using-algolia-docsearch).
|
|
|
|
👥 Other options are **maintained by the community**: please report bugs to their respective repositories.
|
|
|
|
:::
|
|
|
|
## 🥇 Using Algolia DocSearch {#using-algolia-docsearch}
|
|
|
|
Docusaurus has **official support** for [Algolia DocSearch](https://docsearch.algolia.com).
|
|
|
|
The service is **free** for any developer documentation or technical blog: just make sure to read the [checklist](https://docsearch.algolia.com/docs/who-can-apply/) and [apply to the DocSearch program](https://docsearch.algolia.com/apply).
|
|
|
|
DocSearch crawls your website once a week (the schedule is configurable from the web interface) and aggregates all the content in an Algolia index. This content is then queried directly from your front-end using the Algolia API.
|
|
|
|
If your website is [not eligible](https://docsearch.algolia.com/docs/who-can-apply) for the free, hosted version of DocSearch, or if your website sits behind a firewall and is not public, then you can [run your own](https://docsearch.algolia.com/docs/run-your-own/) DocSearch crawler.
|
|
|
|
:::note
|
|
|
|
By default, the Docusaurus preset generates a [sitemap.xml](https://docusaurus.io/sitemap.xml) that the Algolia crawler can use.
|
|
|
|
:::
|
|
|
|
:::info From the old docsearch?
|
|
|
|
You can read more about migration from the legacy DocSearch infra in [our blog post](/blog/2021/11/21/algolia-docsearch-migration) or [the DocSearch migration docs](https://docsearch.algolia.com/docs/migrating-from-legacy).
|
|
|
|
:::
|
|
|
|
### Index Configuration {#algolia-index-configuration}
|
|
|
|
After your application has been approved and deployed, you will receive an email with all the details for you to add DocSearch to your project. Editing and managing your crawls can be done via [the web interface](https://crawler.algolia.com/). Indices are readily available after deployment, so manual configuration usually isn't necessary.
|
|
|
|
:::danger Use the recommended crawler config
|
|
|
|
It is highly recommended to use our official [**Docusaurus v3 crawler configuration**](https://docsearch.algolia.com/docs/templates/#docusaurus-v3-template). We cannot support you if you choose a different crawler configuration.
|
|
|
|
:::
|
|
|
|
:::warning When updating your crawler config
|
|
|
|
The crawler configuration contains a `initialIndexSettings`, which will only be used to initialize your Algolia index if it does not exist yet.
|
|
|
|
If you update your `initialIndexSettings` crawler setting, it is possible to update the index manually through the interface, but [the Algolia team recommends to delete your index and then restart a crawl](https://github.com/facebook/docusaurus/issues/9200#issuecomment-1667338492) to fully reinitialize it with the new settings.
|
|
|
|
:::
|
|
|
|
### Connecting Algolia {#connecting-algolia}
|
|
|
|
Docusaurus' own `@docusaurus/preset-classic` supports Algolia DocSearch integration. If you use the classic preset, no additional installation is needed.
|
|
|
|
<details>
|
|
<summary>Installation steps when not using <code>@docusaurus/preset-classic</code></summary>
|
|
|
|
1. Install the package:
|
|
|
|
```bash npm2yarn
|
|
npm install --save @docusaurus/theme-search-algolia
|
|
```
|
|
|
|
2. Register the theme in `docusaurus.config.js`:
|
|
|
|
```js title="docusaurus.config.js"
|
|
export default {
|
|
title: 'My site',
|
|
// ...
|
|
themes: ['@docusaurus/theme-search-algolia'],
|
|
themeConfig: {
|
|
// ...
|
|
},
|
|
};
|
|
```
|
|
|
|
</details>
|
|
|
|
Then, add an `algolia` field in your `themeConfig`. **[Apply for DocSearch](https://docsearch.algolia.com/apply/)** to get your Algolia index and API key.
|
|
|
|
```js title="docusaurus.config.js"
|
|
export default {
|
|
// ...
|
|
themeConfig: {
|
|
// ...
|
|
// highlight-start
|
|
algolia: {
|
|
// The application ID provided by Algolia
|
|
appId: 'YOUR_APP_ID',
|
|
|
|
// Public API key: it is safe to commit it
|
|
apiKey: 'YOUR_SEARCH_API_KEY',
|
|
|
|
indexName: 'YOUR_INDEX_NAME',
|
|
|
|
// Optional: see doc section below
|
|
contextualSearch: true,
|
|
|
|
// Optional: Specify domains where the navigation should occur through window.location instead on history.push. Useful when our Algolia config crawls multiple documentation sites and we want to navigate with window.location.href to them.
|
|
externalUrlRegex: 'external\\.com|domain\\.com',
|
|
|
|
// Optional: Replace parts of the item URLs from Algolia. Useful when using the same search index for multiple deployments using a different baseUrl. You can use regexp or string in the `from` param. For example: localhost:3000 vs myCompany.com/docs
|
|
replaceSearchResultPathname: {
|
|
from: '/docs/', // or as RegExp: /\/docs\//
|
|
to: '/',
|
|
},
|
|
|
|
// Optional: Algolia search parameters
|
|
searchParameters: {},
|
|
|
|
// Optional: path for search page that enabled by default (`false` to disable it)
|
|
searchPagePath: 'search',
|
|
|
|
// Optional: whether the insights feature is enabled or not on Docsearch (`false` by default)
|
|
insights: false,
|
|
|
|
//... other Algolia params
|
|
},
|
|
// highlight-end
|
|
},
|
|
};
|
|
```
|
|
|
|
:::info
|
|
|
|
The `searchParameters` option used to be named `algoliaOptions` in Docusaurus v1.
|
|
|
|
Refer to its [official DocSearch documentation](https://docsearch.algolia.com/docs/api#searchparameters) for possible values.
|
|
|
|
:::
|
|
|
|
:::warning
|
|
|
|
The search feature will not work reliably until Algolia crawls your site.
|
|
|
|
If search doesn't work after any significant change, please use the Algolia dashboard to **trigger a new crawl**.
|
|
|
|
:::
|
|
|
|
### Contextual search {#contextual-search}
|
|
|
|
Contextual search is **enabled by default**.
|
|
|
|
It ensures that search results are **relevant to the current language and version**.
|
|
|
|
```js title="docusaurus.config.js"
|
|
export default {
|
|
// ...
|
|
themeConfig: {
|
|
// ...
|
|
// highlight-start
|
|
algolia: {
|
|
contextualSearch: true,
|
|
},
|
|
// highlight-end
|
|
},
|
|
};
|
|
```
|
|
|
|
Let's consider you have 2 docs versions (**v1** and **v2**) and 2 languages (`en` and `fr`).
|
|
|
|
When 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).
|
|
|
|
Similarly, when browsing the French site, it would be odd to return search results for the English docs.
|
|
|
|
To solve this problem, the contextual search feature understands that you are browsing a specific docs version and language, and will create the search query filters dynamically.
|
|
|
|
- on `/en/docs/v1/myDoc`, search results will only include **English** results for the **v1** docs (+ other unversioned pages)
|
|
- on `/fr/docs/v2/myDoc`, search results will only include **French** results for the **v2** docs (+ other unversioned pages)
|
|
|
|
:::info
|
|
|
|
When using `contextualSearch: true` (default), the contextual facet filters will be merged with the ones provided with `algolia.searchParameters.facetFilters` .
|
|
|
|
For specific needs, you can disable `contextualSearch` and define your own `facetFilters`:
|
|
|
|
```js title="docusaurus.config.js"
|
|
export default {
|
|
// ...
|
|
themeConfig: {
|
|
// ...
|
|
// highlight-start
|
|
algolia: {
|
|
contextualSearch: false,
|
|
searchParameters: {
|
|
facetFilters: ['language:en', ['filter1', 'filter2'], 'filter3'],
|
|
},
|
|
},
|
|
// highlight-end
|
|
},
|
|
};
|
|
```
|
|
|
|
Refer to the relevant [Algolia faceting documentation](https://www.algolia.com/doc/guides/managing-results/refine-results/faceting/).
|
|
|
|
:::
|
|
|
|
:::warning Contextual search doesn't work?
|
|
|
|
If you only get search results when Contextual Search is disabled, this is very likely because of an [index configuration issue](#algolia-no-search-results).
|
|
|
|
:::
|
|
|
|
### Styling your Algolia search {#styling-your-algolia-search}
|
|
|
|
By 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](styling-layout.mdx#styling-your-site-with-infima) from Docusaurus to style DocSearch by editing the `/src/css/custom.css` file.
|
|
|
|
```css title="/src/css/custom.css"
|
|
[data-theme='light'] .DocSearch {
|
|
/* --docsearch-primary-color: var(--ifm-color-primary); */
|
|
/* --docsearch-text-color: var(--ifm-font-color-base); */
|
|
--docsearch-muted-color: var(--ifm-color-secondary-darkest);
|
|
--docsearch-container-background: rgba(94, 100, 112, 0.7);
|
|
/* Modal */
|
|
--docsearch-modal-background: var(--ifm-color-secondary-lighter);
|
|
/* Search box */
|
|
--docsearch-searchbox-background: var(--ifm-color-secondary);
|
|
--docsearch-searchbox-focus-background: var(--ifm-color-white);
|
|
/* Hit */
|
|
--docsearch-hit-color: var(--ifm-font-color-base);
|
|
--docsearch-hit-active-color: var(--ifm-color-white);
|
|
--docsearch-hit-background: var(--ifm-color-white);
|
|
/* Footer */
|
|
--docsearch-footer-background: var(--ifm-color-white);
|
|
}
|
|
|
|
[data-theme='dark'] .DocSearch {
|
|
--docsearch-text-color: var(--ifm-font-color-base);
|
|
--docsearch-muted-color: var(--ifm-color-secondary-darkest);
|
|
--docsearch-container-background: rgba(47, 55, 69, 0.7);
|
|
/* Modal */
|
|
--docsearch-modal-background: var(--ifm-background-color);
|
|
/* Search box */
|
|
--docsearch-searchbox-background: var(--ifm-background-color);
|
|
--docsearch-searchbox-focus-background: var(--ifm-color-black);
|
|
/* Hit */
|
|
--docsearch-hit-color: var(--ifm-font-color-base);
|
|
--docsearch-hit-active-color: var(--ifm-color-white);
|
|
--docsearch-hit-background: var(--ifm-color-emphasis-100);
|
|
/* Footer */
|
|
--docsearch-footer-background: var(--ifm-background-surface-color);
|
|
--docsearch-key-gradient: linear-gradient(
|
|
-26.5deg,
|
|
var(--ifm-color-emphasis-200) 0%,
|
|
var(--ifm-color-emphasis-100) 100%
|
|
);
|
|
}
|
|
```
|
|
|
|
### Customizing the Algolia search behavior {#customizing-the-algolia-search-behavior}
|
|
|
|
Algolia DocSearch supports a [list of options](https://docsearch.algolia.com/docs/api/) that you can pass to the `algolia` field in the `docusaurus.config.js` file.
|
|
|
|
```js title="docusaurus.config.js"
|
|
export default {
|
|
themeConfig: {
|
|
// ...
|
|
algolia: {
|
|
apiKey: 'YOUR_API_KEY',
|
|
indexName: 'YOUR_INDEX_NAME',
|
|
// Options...
|
|
},
|
|
},
|
|
};
|
|
```
|
|
|
|
### Editing the Algolia search component {#editing-the-algolia-search-component}
|
|
|
|
If you prefer to edit the Algolia search React component, [swizzle](swizzling.mdx) the `SearchBar` component in `@docusaurus/theme-search-algolia`:
|
|
|
|
```bash npm2yarn
|
|
npm run swizzle @docusaurus/theme-search-algolia SearchBar
|
|
```
|
|
|
|
### Troubleshooting {#algolia-troubleshooting}
|
|
|
|
Here are the most common issues Docusaurus users face when using Algolia DocSearch.
|
|
|
|
#### No Search Results {#algolia-no-search-results}
|
|
|
|
Seeing no search results is usually related to an **index configuration problem**.
|
|
|
|
<details>
|
|
<summary>How to check if I have a config problem?</summary>
|
|
|
|
Docusaurus uses [Algolia faceting](https://www.algolia.com/doc/guides/managing-results/refine-results/faceting/) for its [Contextual Search](#contextual-search) feature, to create dynamic queries such as:
|
|
|
|
```json
|
|
[
|
|
"language:en",
|
|
[
|
|
"docusaurus_tag:default",
|
|
"docusaurus_tag:docs-default-3.2.1",
|
|
"docusaurus_tag:docs-community-current",
|
|
"docusaurus_tag:docs-docs-tests-current"
|
|
]
|
|
]
|
|
```
|
|
|
|
On the Algolia UI, your index should allow to create facet queries on fields `docusaurus_tag`, `language`, `lang`, `version`, `type`, as shown in the screenshot below:
|
|
|
|

|
|
|
|
Alternatively, if you disable [Contextual Search](#contextual-search) with `{contextualSearch: false}` (which we don't particularly recommend), Docusaurus will not use facet queries, and you should start seeing results.
|
|
|
|
</details>
|
|
|
|
:::danger Use the recommended configuration
|
|
|
|
We [recommend a specific crawler configuration](#algolia-index-configuration) for a good reason. We cannot support you if you choose to use a different configuration.
|
|
|
|
:::
|
|
|
|
You can fix index configuration problems by following those steps:
|
|
|
|
1. Use the [recommend crawler configuration](#algolia-index-configuration)
|
|
2. Delete your index through the UI
|
|
3. Trigger a new crawl through the UI
|
|
4. Check your index is recreated with the appropriate faceting fields: `docusaurus_tag`, `language`, `lang`, `version`, `type`
|
|
5. See that you now get search results, even with [Contextual Search](#contextual-search) enabled
|
|
|
|
### Support {#algolia-support}
|
|
|
|
The Algolia DocSearch team can help you figure out search problems on your site.
|
|
|
|
You can reach out to Algolia via [their support page](https://algolia.com/support) or on [Discord](https://discord.gg/wr2m5j948P).
|
|
|
|
Docusaurus also has an `#algolia` channel on [Discord](https://discordapp.com/invite/docusaurus).
|
|
|
|
## 👥 Using Typesense DocSearch {#using-typesense-docsearch}
|
|
|
|
[Typesense](https://typesense.org) DocSearch works similar to Algolia DocSearch, except that your website is indexed into a Typesense search cluster.
|
|
|
|
Typesense is an [open source](https://github.com/typesense/typesense) instant-search engine that you can either:
|
|
|
|
- [Self-Host](https://typesense.org/docs/guide/install-typesense.html#option-2-local-machine-self-hosting) on your own servers or
|
|
- Use the Managed [Typesense Cloud](https://cloud.typesense.org) service.
|
|
|
|
Similar to Algolia DocSearch, there are two components:
|
|
|
|
- [typesense-docsearch-scraper](https://github.com/typesense/typesense-docsearch-scraper) - which scrapes your website and indexes the data in your Typesense cluster.
|
|
- [docusaurus-theme-search-typesense](https://github.com/typesense/docusaurus-theme-search-typesense) - a search bar UI component to add to your website.
|
|
|
|
Read a step-by-step walk-through of how to [run typesense-docsearch-scraper here](https://typesense.org/docs/guide/docsearch.html#step-1-set-up-docsearch-scraper) and how to [install the Search Bar in your Docusaurus Site here](https://typesense.org/docs/guide/docsearch.html#option-a-docusaurus-powered-sites).
|
|
|
|
## 👥 Using Local Search {#using-local-search}
|
|
|
|
You can use a local search plugin for websites where the search index is small and can be downloaded to your users' browsers when they visit your website.
|
|
|
|
You'll find a list of community-supported [local search plugins listed here](https://docusaurus.io/community/resources#search).
|
|
|
|
## 👥 Using your own search {#using-your-own-search}
|
|
|
|
To use your own search, swizzle the `SearchBar` component in `@docusaurus/theme-classic`
|
|
|
|
```bash npm2yarn
|
|
npm run swizzle @docusaurus/theme-classic SearchBar
|
|
```
|
|
|
|
This will create an `src/theme/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](#editing-the-algolia-search-component) and create your own search component from there.
|