1
0
Fork 0

feat(askai): add facet filtering (#2676)

This commit is contained in:
Dylan Tientcheu 2025-07-22 11:28:18 +02:00 committed by GitHub
parent b6e07733d2
commit 55e3eb301c
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
6 changed files with 76 additions and 15 deletions

View file

@ -12,7 +12,12 @@ function App(): JSX.Element {
indexName="docsearch" indexName="docsearch"
appId="PMZUYBQDAK" appId="PMZUYBQDAK"
apiKey="24b09689d5b4223813d9b8e48563c8f6" apiKey="24b09689d5b4223813d9b8e48563c8f6"
askAi="askAIDemo" askAi={{
assistantId: 'askAIDemo',
searchParameters: {
facetFilters: ['language:en'],
},
}}
insights={true} insights={true}
/> />
</div> </div>

View file

@ -10,7 +10,6 @@
width: 320px; width: 320px;
height: 36px; height: 36px;
justify-content: space-between; justify-content: space-between;
margin: 0 0 0 16px;
padding: 0 8px; padding: 0 8px;
user-select: none; user-select: none;
} }
@ -54,8 +53,8 @@
color: var(--docsearch-key-color); color: var(--docsearch-key-color);
box-shadow: none !important; box-shadow: none !important;
display: flex; display: flex;
height: 20px; height: 24px;
width: 20px; width: 24px;
justify-content: center; justify-content: center;
position: relative; position: relative;
border: 0; border: 0;

View file

@ -43,6 +43,12 @@ export type DocSearchAskAi = {
* The assistant ID to use for the ask AI feature. * The assistant ID to use for the ask AI feature.
*/ */
assistantId: string | null; assistantId: string | null;
/**
* The search parameters to use for the ask AI feature.
*/
searchParameters?: {
facetFilters?: SearchParamsObject['facetFilters'];
};
}; };
export interface DocSearchProps { export interface DocSearchProps {

View file

@ -330,6 +330,7 @@ export function DocSearchModal({
const askAiConfig = typeof askAi === 'object' ? askAi : null; const askAiConfig = typeof askAi === 'object' ? askAi : null;
const askAiConfigurationId = typeof askAi === 'string' ? askAi : askAiConfig?.assistantId || null; const askAiConfigurationId = typeof askAi === 'string' ? askAi : askAiConfig?.assistantId || null;
const askAiSearchParameters = askAiConfig?.searchParameters;
const [askAiStreamError, setAskAiStreamError] = React.useState<Error | null>(null); const [askAiStreamError, setAskAiStreamError] = React.useState<Error | null>(null);
@ -363,6 +364,7 @@ export function DocSearchModal({
'X-Algolia-Index-Name': askAiConfig?.indexName || indexName, 'X-Algolia-Index-Name': askAiConfig?.indexName || indexName,
'X-Algolia-Assistant-Id': askAiConfigurationId || '', 'X-Algolia-Assistant-Id': askAiConfigurationId || '',
}, },
body: askAiSearchParameters ? { searchParameters: askAiSearchParameters } : {},
onError(streamError) { onError(streamError) {
setAskAiStreamError(streamError); setAskAiStreamError(streamError);
}, },

View file

@ -87,7 +87,7 @@ docsearch({
}); });
``` ```
or or if you want to use different credentials for askAi and add search parameters
```js ```js
docsearch({ docsearch({
@ -97,6 +97,9 @@ docsearch({
apiKey: 'ANOTHER_SEARCH_API_KEY', apiKey: 'ANOTHER_SEARCH_API_KEY',
appId: 'ANOTHER_APP_ID', appId: 'ANOTHER_APP_ID',
assistantId: 'YOUR_ALGOLIA_ASSISTANT_ID', assistantId: 'YOUR_ALGOLIA_ASSISTANT_ID',
searchParameters: {
facetFilters: ['language:en'],
},
}, },
// ... // ...
}); });

View file

@ -204,6 +204,8 @@ docsearch({
### Filtering search results ### Filtering search results
#### Keyword search
If your website uses [DocSearch meta tags][13] or if you've added [custom variables to your config][14], you'll be able to use the [`facetFilters`][16] option to scope your search results to a [`facet`][15] If your website uses [DocSearch meta tags][13] or if you've added [custom variables to your config][14], you'll be able to use the [`facetFilters`][16] option to scope your search results to a [`facet`][15]
This is useful to limit the scope of the search to one language or one version. This is useful to limit the scope of the search to one language or one version.
@ -242,6 +244,54 @@ docsearch({
</Tabs> </Tabs>
#### AskAI
Filtering also applies when using AskAI. This is useful to limit the scope of the LLM's search to only relevant results.
:::info
We recommend using the `facetFilters` option when using AskAI with multiple languages or any multi-faceted index.
:::
<Tabs
groupId="language"
defaultValue="js"
values={[
{ label: 'JavaScript', value: 'js', },
{ label: 'React', value: 'react', }
]
}>
<TabItem value="js">
```js
docsearch({
askAi: {
assistantId: 'YOUR_ALGOLIA_ASSISTANT_ID',
searchParameters: {
facetFilters: ['language:en', 'version:1.0.0'],
},
},
});
```
</TabItem>
<TabItem value="react">
```js
```
```jsx
<DocSearch
askAi={{
assistantId: 'YOUR_ALGOLIA_ASSISTANT_ID',
searchParameters: {
facetFilters: ['language:en', 'version:1.0.0'],
},
}}
/>
```
</TabItem>
</Tabs>
### Sending events ### Sending events
You can send search events to your DocSearch index by passing in the `insights` parameter when creating your DocSearch instance. You can send search events to your DocSearch index by passing in the `insights` parameter when creating your DocSearch instance.
@ -256,12 +306,10 @@ You can send search events to your DocSearch index by passing in the `insights`
}> }>
<TabItem value="js"> <TabItem value="js">
```js ```diff
docsearch({ docsearch({
searchParameters: { // other options
facetFilters: ['language:en', 'version:1.0.0'], + insights: true,
},
insights: true,
}); });
``` ```
@ -269,12 +317,10 @@ docsearch({
<TabItem value="react"> <TabItem value="react">
```jsx ```diff
<DocSearch <DocSearch
searchParameters={{ // other options
facetFilters: ['language:en', 'version:1.0.0'], + insights={true}
}}
insights
/> />
``` ```