-
Notifications
You must be signed in to change notification settings - Fork 435
feat(v4): Add support for multi-index search #2736
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 1 commit
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| /* eslint-disable react/react-in-jsx-scope */ | ||
| import { DocSearch } from '@docsearch/react'; | ||
| import type { JSX } from 'react'; | ||
|
|
||
| export default function MultiIndex(): JSX.Element { | ||
| return ( | ||
| <DocSearch | ||
| indices={[ | ||
| { | ||
| name: 'docsearch', | ||
| }, | ||
| { | ||
| name: 'tailwindcss', | ||
| }, | ||
| { | ||
| name: 'kubernetes', | ||
| }, | ||
| ]} | ||
| appId="PMZUYBQDAK" | ||
| apiKey="24b09689d5b4223813d9b8e48563c8f6" | ||
| translations={{ button: { buttonText: 'Multi index search' } }} | ||
| insights={true} | ||
| /> | ||
| ); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -51,6 +51,11 @@ export type DocSearchAskAi = { | |
| }; | ||
| }; | ||
|
|
||
| export interface DocSearchIndex { | ||
| name: string; | ||
| searchParameters?: SearchParamsObject; | ||
| } | ||
|
|
||
| export interface DocSearchProps { | ||
| /** | ||
| * Algolia application id used by the search client. | ||
|
|
@@ -62,8 +67,14 @@ export interface DocSearchProps { | |
| apiKey: string; | ||
| /** | ||
| * Name of the algolia index to query. | ||
| * | ||
| * @deprecated `indexName` will be removed in a future version. Please use `indices` property going forward. | ||
| */ | ||
| indexName: string; | ||
| indexName?: string; | ||
| /** | ||
| * List of indices to be used for search. | ||
| */ | ||
| indices?: Array<DocSearchIndex | string>; | ||
| /** | ||
| * Configuration or assistant id to enable ask ai mode. Pass a string assistant id or a full config object. | ||
| */ | ||
|
|
@@ -78,6 +89,8 @@ export interface DocSearchProps { | |
| placeholder?: string; | ||
| /** | ||
| * Additional algolia search parameters to merge into each query. | ||
| * | ||
| * @deprecated `searchParameters` will be removed in a future version. Please use `indices` property going forward. | ||
| */ | ||
| searchParameters?: SearchParamsObject; | ||
| /** | ||
|
|
@@ -142,7 +155,7 @@ export interface DocSearchProps { | |
| recentSearchesWithFavoritesLimit?: number; | ||
| } | ||
|
|
||
| export function DocSearch({ ...props }: DocSearchProps): JSX.Element { | ||
| export function DocSearch({ indexName, searchParameters, indices = [], ...props }: DocSearchProps): JSX.Element { | ||
| const searchButtonRef = React.useRef<HTMLButtonElement>(null); | ||
| const [isOpen, setIsOpen] = React.useState(false); | ||
| const [initialQuery, setInitialQuery] = React.useState<string | undefined>(props?.initialQuery || undefined); | ||
|
|
@@ -200,6 +213,26 @@ export function DocSearch({ ...props }: DocSearchProps): JSX.Element { | |
| }); | ||
| useTheme({ theme: props.theme }); | ||
|
|
||
| // Format the `indexes` to be used until `indexName` and `searchParameters` props are fully removed. | ||
| const indexes: DocSearchIndex[] = []; | ||
|
|
||
| if (indexName && indexName !== '') { | ||
| indexes.push({ | ||
| name: indexName, | ||
| searchParameters, | ||
| }); | ||
| } | ||
|
|
||
| if (indices.length > 0) { | ||
| indices.forEach((index) => { | ||
| indexes.push(typeof index === 'string' ? { name: index } : index); | ||
| }); | ||
| } | ||
|
|
||
| if (indexes.length < 1) { | ||
| throw new Error('Must supply either `indexName` or `indices` for DocSearch to work'); | ||
| } | ||
|
|
||
| return ( | ||
| <> | ||
| <DocSearchButton ref={searchButtonRef} translations={props?.translations?.button} onClick={onOpen} /> | ||
|
|
@@ -214,6 +247,7 @@ export function DocSearch({ ...props }: DocSearchProps): JSX.Element { | |
| translations={props?.translations?.modal} | ||
| isAskAiActive={isAskAiActive} | ||
| canHandleAskAi={canHandleAskAi} | ||
| indexes={indexes} | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why is DocSearch prop named |
||
| onAskAiToggle={onAskAiToggle} | ||
| onClose={onClose} | ||
| />, | ||
|
|
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
From what I understand, this fallback logic only applies when using
<DocSearch>, but it doesn't apply when using<DocSearchButton>and<DocSearchModal>directly, like Docusaurus does.Unfortunately, this PR broke the DocSearch v3/v4 retrocompatibility we expected for Docusaurus, and upgrading from beta8 to GA makes the search unusable: facebook/docusaurus#11327