Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions examples/demo-react/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import '@docsearch/css/dist/style.css';

import Basic from './examples/basic';
import BasicAskAI from './examples/basic-askai';
import MultiIndex from './examples/multi-index';
import WHitComponent from './examples/w-hit-component';
import WTransformItems from './examples/w-hit-transformItems';

Expand Down Expand Up @@ -47,6 +48,13 @@ function App(): JSX.Element {
<WTransformItems />
</div>
</section>

<section className="demo-section">
<p className="section-description">multi index search</p>
<div className="search-wrapper">
<MultiIndex />
</div>
</section>
</main>
</div>
</div>
Expand Down
25 changes: 25 additions & 0 deletions examples/demo-react/src/examples/multi-index.tsx
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}
/>
);
}
38 changes: 36 additions & 2 deletions packages/docsearch-react/src/DocSearch.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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.
*/
Expand All @@ -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;
/**
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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');
}
Comment on lines +218 to +236
Copy link
Contributor

@slorber slorber Sep 16, 2025

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


return (
<>
<DocSearchButton ref={searchButtonRef} translations={props?.translations?.button} onClick={onOpen} />
Expand All @@ -214,6 +247,7 @@ export function DocSearch({ ...props }: DocSearchProps): JSX.Element {
translations={props?.translations?.modal}
isAskAiActive={isAskAiActive}
canHandleAskAi={canHandleAskAi}
indexes={indexes}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is DocSearch prop named indices, while subcomponents accept an indexes prop? Isn't that a typo? Why not always a consistent name?

onAskAiToggle={onAskAiToggle}
onClose={onClose}
/>,
Expand Down
Loading