Examples and extensions
このページは PageTurner AI で翻訳されました(ベータ版)。プロジェクト公式の承認はありません。 エラーを見つけましたか? 問題を報告 →
These examples are interactive. Click a button to open the modal and try a query.
基本キーワード検索
Use the default experience with your index credentials. This works great for typical docs, blogs, and any site with a DocSearch-compliant index.
<DocSearch
appId="PMZUYBQDAK"
apiKey="24b09689d5b4223813d9b8e48563c8f6"
indexName="docsearch"
insights={true}
translations={{ button: { buttonText: 'keyword search (demo)' } }}
/>
Ask AI: AI支援型回答
Add Algolia Ask AI to get synthesized answers grounded in your indexed content. You can scope the LLM context using searchParameters like facetFilters, filters, attributesToRetrieve,restrictSearchableAttributes, and distinct.
<DocSearch
appId="PMZUYBQDAK"
apiKey="24b09689d5b4223813d9b8e48563c8f6"
indexName="docsearch"
askAi={{
assistantId: 'askAIDemo',
searchParameters: {
facetFilters: ['language:en'],
},
}}
insights={true}
translations={{ button: { buttonText: 'search with askai (demo)' } }}
/>
サイドパネル: 永続的AIチャット
サイドパネルはページのサイドに固定された永続的なチャットインターフェースを提供します。現在の位置を失うことなくフォローアップの質問が可能なため、ドキュメントサイトに最適です。デモをお試しになるには、画面右下のボタンをご利用ください。
<DocSearchSidepanel
appId="PMZUYBQDAK"
apiKey="24b09689d5b4223813d9b8e48563c8f6"
indexName="docsearch"
assistantId="askAIDemo"
/>
コンポーザブルAPI: DocSearchButton + DocSearchModal
コンポーザブルAPIを使用すると、ボタンとモーダルを独立したコンポーネントとしてレンダリングできます。これにより各要素の表示位置とモーダルコードの読み込みタイミングを明示的に制御可能です。
import { DocSearch } from '@docsearch/core';
import { DocSearchButton, DocSearchModal } from '@docsearch/modal';
import '@docsearch/css/style.css';
<DocSearch>
<DocSearchButton translations={{ buttonText: 'Composable search (demo)' }} />
<DocSearchModal
appId="PMZUYBQDAK"
indexName="docsearch"
apiKey="a00716d83c64f6c61905c078b7d5ab66"
askAi={{
assistantId: 'ccdec697-e3fe-465b-a1c3-657e7bf18aef',
agentStudio: true,
}}
/>
</DocSearch>;
カスタム検索結果表示 (hitComponent)
Replace the default hit markup to match your brand and layout. Below is a minimal example of a custom component.
function CustomHit({ hit }) {
// render a compact, branded hit card
return (
<a
href={hit.url}
style={{ display: 'block', padding: '12px 16px', textDecoration: 'none' }}
>
<div style={{ display: 'flex', gap: 12 }}>
<div
style={{
width: 40,
height: 40,
backgroundColor: '#e3f2fd',
borderRadius: 6,
display: 'flex',
alignItems: 'center',
justifyContent: 'center',
fontWeight: 600,
color: '#1976d2',
}}
>
{hit.type?.toUpperCase?.() || 'DOC'}
</div>
<div style={{ minWidth: 0 }}>
<div
style={{
fontWeight: 600,
whiteSpace: 'nowrap',
overflow: 'hidden',
textOverflow: 'ellipsis',
}}
>
{hit.hierarchy?.lvl1 || 'untitled'}
</div>
{hit.hierarchy?.lvl2 && (
<div
style={{
color: '#666',
whiteSpace: 'nowrap',
overflow: 'hidden',
textOverflow: 'ellipsis',
}}
>
{hit.hierarchy.lvl2}
</div>
)}
{hit.content && (
<div style={{ color: '#888', marginTop: 4 }}>{hit.content}</div>
)}
</div>
</div>
</a>
);
}
<DocSearch
appId="PMZUYBQDAK"
apiKey="24b09689d5b4223813d9b8e48563c8f6"
indexName="docsearch"
hitComponent={CustomHit}
insights={true}
translations={{ button: { buttonText: 'custom hits (demo)' } }}
/>;
新規タブでリンクを開く
デフォルトでは検索結果リンクは現在のウィンドウで開きます。新規タブで開くには、カスタムhitComponentとnavigatorプロパティを併用し、クリックとキーボードナビゲーションを一貫して処理する必要があります。
// Custom hit component with target="_blank"
function HitWithNewTab({ hit, children }) {
return (
<a href={hit.url} target="_blank" rel="noopener noreferrer">
{children}
</a>
);
}
// Navigator configuration to handle keyboard navigation
const newTabNavigator = {
navigate: ({ itemUrl }) => window.open(itemUrl, '_blank'),
navigateNewTab: ({ itemUrl }) => window.open(itemUrl, '_blank'),
navigateNewWindow: ({ itemUrl }) => window.open(itemUrl, '_blank'),
};
<DocSearch
appId="PMZUYBQDAK"
apiKey="24b09689d5b4223813d9b8e48563c8f6"
indexName="docsearch"
hitComponent={HitWithNewTab}
navigator={newTabNavigator}
insights={true}
translations={{ button: { buttonText: 'open in new tabs (demo)' } }}
/>;
注意: hitComponent単体でtarget="_blank"を使用するとマウスクリックでは機能しますが、キーボード操作(矢印+Enter)で新規タブを一貫して開くにはnavigatorプロパティが必須です。
任意データ構造の適用 (transformItems)
DocSearch is not limited to DocSearch-like records. Use transformItems to adapt any record shape into the internal structure DocSearch expects. This lets you build search for apps, help centers, changelogs, or any custom content.
The snippet below maps a non-standard record to the internal format. Try it live:
<DocSearch
appId="PMZUYBQDAK"
apiKey="24b09689d5b4223813d9b8e48563c8f6"
indexName="crawler_doc"
askAi={{ assistantId: 'askAIDemo' }}
searchParameters={{
attributesToRetrieve: ['*'],
attributesToSnippet: ['*'],
hitsPerPage: 20,
}}
transformItems={(items) =>
items.map((item) => ({
objectID: item.objectID,
content: item.content ?? '',
url: item.domain + item.path,
hierarchy: {
lvl0: (item.breadcrumb || []).join(' > ') ?? '',
lvl1: item.h1 ?? '',
lvl2: item.h2 ?? '',
lvl3: null,
lvl4: null,
lvl5: null,
lvl6: null,
},
url_without_anchor: item.domain + item.path,
type: 'content',
anchor: null,
_highlightResult: item._highlightResult,
_snippetResult: item._snippetResult,
}))
}
insights={true}
translations={{ button: { buttonText: 'transform items (demo)' } }}
/>
活用のヒント
-
計測:
insightsを有効化して利用分析データを送信し、関連性の改善を反復的に実施 -
Ask AI scoping: use
facetFilters,filters,attributesToRetrieve,restrictSearchableAttributes, anddistinctto control AI context and improve answer quality. -
Customization: use
hitComponent,transformItems, andtranslationsto make DocSearch feel native to any product surface.