Skip to content

Commit 905e26d

Browse files
feat(v4): Add support for multi-index search (#2736)
* feat(v4): Add support for multi-index search * feat(docs): Document new indices property and mark indeName and searchParameters as deprecated --------- Co-authored-by: Dylan Tientcheu <[email protected]>
1 parent 47be73d commit 905e26d

File tree

7 files changed

+285
-92
lines changed

7 files changed

+285
-92
lines changed

examples/demo-react/src/App.tsx

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import '@docsearch/css/dist/style.css';
77

88
import Basic from './examples/basic';
99
import BasicAskAI from './examples/basic-askai';
10+
import MultiIndex from './examples/multi-index';
1011
import WHitComponent from './examples/w-hit-component';
1112
import WTransformItems from './examples/w-hit-transformItems';
1213

@@ -47,6 +48,13 @@ function App(): JSX.Element {
4748
<WTransformItems />
4849
</div>
4950
</section>
51+
52+
<section className="demo-section">
53+
<p className="section-description">multi index search</p>
54+
<div className="search-wrapper">
55+
<MultiIndex />
56+
</div>
57+
</section>
5058
</main>
5159
</div>
5260
</div>
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/* eslint-disable react/react-in-jsx-scope */
2+
import { DocSearch } from '@docsearch/react';
3+
import type { JSX } from 'react';
4+
5+
export default function MultiIndex(): JSX.Element {
6+
return (
7+
<DocSearch
8+
indices={[
9+
{
10+
name: 'docsearch',
11+
},
12+
{
13+
name: 'tailwindcss',
14+
},
15+
{
16+
name: 'kubernetes',
17+
},
18+
]}
19+
appId="PMZUYBQDAK"
20+
apiKey="24b09689d5b4223813d9b8e48563c8f6"
21+
translations={{ button: { buttonText: 'Multi index search' } }}
22+
insights={true}
23+
/>
24+
);
25+
}

packages/docsearch-react/src/DocSearch.tsx

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,11 @@ export type DocSearchAskAi = {
5151
};
5252
};
5353

54+
export interface DocSearchIndex {
55+
name: string;
56+
searchParameters?: SearchParamsObject;
57+
}
58+
5459
export interface DocSearchProps {
5560
/**
5661
* Algolia application id used by the search client.
@@ -62,8 +67,16 @@ export interface DocSearchProps {
6267
apiKey: string;
6368
/**
6469
* Name of the algolia index to query.
70+
*
71+
* @deprecated `indexName` will be removed in a future version. Please use `indices` property going forward.
72+
*/
73+
indexName?: string;
74+
/**
75+
* List of indices and _optional_ searchParameters to be used for search.
76+
*
77+
* @see {@link https://docsearch.algolia.com/docs/api#indices}
6578
*/
66-
indexName: string;
79+
indices?: Array<DocSearchIndex | string>;
6780
/**
6881
* Configuration or assistant id to enable ask ai mode. Pass a string assistant id or a full config object.
6982
*/
@@ -78,6 +91,8 @@ export interface DocSearchProps {
7891
placeholder?: string;
7992
/**
8093
* Additional algolia search parameters to merge into each query.
94+
*
95+
* @deprecated `searchParameters` will be removed in a future version. Please use `indices` property going forward.
8196
*/
8297
searchParameters?: SearchParamsObject;
8398
/**
@@ -142,7 +157,7 @@ export interface DocSearchProps {
142157
recentSearchesWithFavoritesLimit?: number;
143158
}
144159

145-
export function DocSearch({ ...props }: DocSearchProps): JSX.Element {
160+
export function DocSearch({ indexName, searchParameters, indices = [], ...props }: DocSearchProps): JSX.Element {
146161
const searchButtonRef = React.useRef<HTMLButtonElement>(null);
147162
const [isOpen, setIsOpen] = React.useState(false);
148163
const [initialQuery, setInitialQuery] = React.useState<string | undefined>(props?.initialQuery || undefined);
@@ -200,6 +215,26 @@ export function DocSearch({ ...props }: DocSearchProps): JSX.Element {
200215
});
201216
useTheme({ theme: props.theme });
202217

218+
// Format the `indexes` to be used until `indexName` and `searchParameters` props are fully removed.
219+
const indexes: DocSearchIndex[] = [];
220+
221+
if (indexName && indexName !== '') {
222+
indexes.push({
223+
name: indexName,
224+
searchParameters,
225+
});
226+
}
227+
228+
if (indices.length > 0) {
229+
indices.forEach((index) => {
230+
indexes.push(typeof index === 'string' ? { name: index } : index);
231+
});
232+
}
233+
234+
if (indexes.length < 1) {
235+
throw new Error('Must supply either `indexName` or `indices` for DocSearch to work');
236+
}
237+
203238
return (
204239
<>
205240
<DocSearchButton ref={searchButtonRef} translations={props?.translations?.button} onClick={onOpen} />
@@ -214,6 +249,7 @@ export function DocSearch({ ...props }: DocSearchProps): JSX.Element {
214249
translations={props?.translations?.modal}
215250
isAskAiActive={isAskAiActive}
216251
canHandleAskAi={canHandleAskAi}
252+
indexes={indexes}
217253
onAskAiToggle={onAskAiToggle}
218254
onClose={onClose}
219255
/>,

0 commit comments

Comments
 (0)