API 参考
本页面由 PageTurner AI 翻译(测试版)。未经项目官方认可。 发现错误? 报告问题 →
- JavaScript
- React
container
type: string | HTMLElement| required
The container for the DocSearch search box. You can either pass a CSS selector or an Element. If there are several containers matching the selector, DocSearch picks up the first one.
environment
type: typeof window|default: window| optional
The environment in which your application is running.
This is useful if you’re using DocSearch in a different context than window.
appId
type: string| 必填
您的 Algolia 应用 ID。
apiKey
type: string| 必填
您的 Algolia 搜索 API 密钥。
indices
type: Array<string | DocSearchIndex>
用于关键词搜索的索引列表及其_可选_的 searchParameters。
列表中的顺序至关重要,搜索结果将按照 indices 的顺序进行排序。
在
indexName处于弃用阶段期间,必须传递indices或indexName中的至少一个参数。未传递任一参数将导致抛出Error。
- JavaScript
- React
docsearch({
// ...
indices: ['YOUR_ALGOLIA_INDEX'],
// ...
});
in case you want to use custom searchParameters for the index
docsearch({
// ...
indices: [
{
name: 'YOUR_ALGOLIA_INDEX',
searchParameters: {
facetFilters: ['language:en'],
// ...
},
},
],
// ...
});
<DocSearch
// ...
indices={['YOUR_ALGOLIA_INDEX']}
// ...
/>
in case you want to use custom searchParameters for the index
<DocSearch
// ...
indices={[
{
name: 'YOUR_ALGOLIA_INDEX',
searchParameters: {
facetFilters: ['language:en'],
// ...
},
},
]}
// ...
/>
indexName
type: string| 已弃用
indexName 目前已被计划弃用,推荐使用的新属性是 indices。
您的 Algolia 索引名称。
在
indexName处于弃用阶段期间, 必须传递indices或indexName中的至少一个参数。未传递任一参数将导致抛出Error。
placeholder
type: string|default: "Search docs"| 可选
The placeholder of the input of the DocSearch pop-up modal. Note: If you add a placeholder, it will replace the dynamic placeholder based on askAi. It would be better to edit translations instead.
askAi
type: AskAiObject|string| 可选
您的 Algolia Assistant ID。
- JavaScript
- React
docsearch({
// ...
askAi: 'YOUR_ALGOLIA_ASSISTANT_ID',
// ...
});
or if you want to use different credentials for askAi and add search parameters
docsearch({
// ...
askAi: {
indexName: 'ANOTHER_INDEX_NAME',
apiKey: 'ANOTHER_SEARCH_API_KEY',
appId: 'ANOTHER_APP_ID',
assistantId: 'YOUR_ALGOLIA_ASSISTANT_ID',
searchParameters: {
// Filtering parameters
facetFilters: ['language:en', 'version:latest'],
filters: 'type:content AND language:en',
// Content control parameters
attributesToRetrieve: ['title', 'content', 'url'],
restrictSearchableAttributes: ['title', 'content'],
// Deduplication
distinct: true,
},
// Enables/disables showing suggested questions on Ask AI's new conversation screen
// NOTE: Only available with version >= 4.3
suggestedQuestions: true,
},
// ...
});
<DocSearch
// ...
askAi="YOUR_ALGOLIA_ASSISTANT_ID"
/>
in case you want to use different credentials for askAi
<DocSearch
// ...
askAi={{
indexName: 'ANOTHER_INDEX_NAME',
apiKey: 'ANOTHER_SEARCH_API_KEY',
appId: 'ANOTHER_APP_ID',
assistantId: 'YOUR_ALGOLIA_ASSISTANT_ID',
searchParameters: {
// Filtering parameters
facetFilters: ['language:en', 'version:latest'],
filters: 'type:content AND language:en',
// Content control parameters
attributesToRetrieve: ['title', 'content', 'url'],
restrictSearchableAttributes: ['title', 'content'],
// Deduplication
distinct: true,
},
// Enables/disables showing suggested questions on Ask AI's new conversation screen
// NOTE: Only available with version >= 4.3
suggestedQuestions: true,
}}
/>
- 过滤:
facetFilters: ['type:content']- 按语言、版本或内容类型筛选 - 复杂过滤:
filters: 'type:content AND language:en'- 应用复杂过滤规则 - 内容控制:
attributesToRetrieve: ['title', 'content', 'url']- 控制返回的属性字段 - 搜索范围:
restrictSearchableAttributes: ['title', 'content']- 限定搜索特定字段 - 去重:
distinct: true- 移除重复结果 (boolean | number | string)
这些参数在保持 API 简洁聚焦的同时,提供了 Ask AI 的核心功能。
askAi.agentStudio
type: boolean| 可选 | 实验性
askAi.agentStudio 目前是实验性属性,预计在 5.0.0 版本中稳定发布。
若将 askAi.agentStudio 设为 true,Ask AI 聊天将使用 Algolia 的 Agent Studio 作为聊天后端而非 Ask AI 后端。详见 Algolia Agent Studio 文档。
docsearch({
// ...
askAi: {
assistantId: 'YOUR_ALGOLIA_ASSISTANT_ID',
agentStudio: true,
searchParameters: {
YOUR_INDEX_NAME: {
filters: 'type:content AND language:en',
attributesToRetrieve: ['title', 'content', 'url'],
restrictSearchableAttributes: ['title', 'content'],
distinct: 'url',
},
},
},
});
- 标准 Ask AI(省略
agentStudio或设为false):searchParameters为扁平对象,支持facetFilters、filters、attributesToRetrieve、restrictSearchableAttributes和distinct - Agent Studio(
agentStudio: true):searchParameters需按索引名称键值对组织,支持filters、attributesToRetrieve、restrictSearchableAttributes和distinct
searchParameters
type: SearchParameters| 可选 | 已弃用
searchParameters 目前已被计划弃用,推荐使用的新属性是 indices。
transformItems
type: function|default: items => items| 可选
接收来自搜索响应的条目,并在显示前进行处理。应返回与原始数组结构相同的新数组,适用于对条目进行转换、删除或重新排序。
- JavaScript
- React
docsearch({
// ...
transformItems(items) {
return items.map((item) => ({
...item,
content: item.content.toUpperCase(),
}));
},
});
<DocSearch
// ...
transformItems={(items) => {
return items.map((item) => ({
...item,
content: item.content.toUpperCase(),
}));
}}
/>
hitComponent
type: ({ hit, children }, { html }) => JSX.Element | string | Function|default: Hit| 可选
用于展示每个条目的组件。支持以下模板模式:
-
使用 html 辅助函数的 HTML 字符串(JS CDN 推荐):
({ hit, children }, { html }) => html... -
JSX 模板(适用于 React/Preact):
({ hit, children }) => <div>...</div> -
基于函数的模板:
(props) => string | JSX.Element | Function
可通过 hit 对象访问搜索结果的全部数据,children 则是默认渲染内容。
参阅默认实现。
- JavaScript
- React
docsearch({
// ...
hitComponent({ hit, children }, { html }) {
// Using HTML strings with html helper
return html`
<a href="${hit.url}" class="custom-hit-class">
<div class="hit-icon">🔍</div>
<div class="hit-content">${children}</div>
</a>
`;
},
});
<DocSearch
// ...
hitComponent={({ hit, children }) => {
// Using JSX templates
return (
<a href={hit.url} className="custom-hit-class">
<div className="hit-icon">🔍</div>
<div className="hit-content">{children}</div>
</a>
);
}}
/>
transformSearchClient
type: function|default: DocSearchTransformClient => DocSearchTransformClient| 可选
用于转换 Algolia 搜索客户端,例如实现 搜索查询防抖
disableUserPersonalization
type: boolean|default: false| 可选
禁用将最近搜索和收藏保存到本地存储的功能
initialQuery
type: string| 可选
搜索输入框的初始查询内容