--- id: examples title: Examples and extensions description: live demos showing how to use and extend docsearch beyond documentation-only use cases. --- import { DocSearch } from '@docsearch/react'; import '@docsearch/css/dist/style.css'; > These examples are interactive. click a button to open the modal and try a query. ## Basic keyword search Use the default experience with your index credentials. this works great for typical docs, blogs, and any site with a docsearch-compliant index. ```jsx ``` --- ## Ask AI: ai-assisted answers Add algolia askai to get synthesized answers grounded in your indexed content. you can scope the llm context using `searchParameters` like `facetFilters`, `filters`, `attributesToRetrieve`,`restrictSearchableAttributes`, and `distinct`. ```jsx ``` --- ## Custom hit rendering (`hitComponent`) Replace the default hit markup to match your brand and layout. below is a minimal example of a custom component. ```jsx function CustomHit({ hit }) { // render a compact, branded hit card return (
{hit.type?.toUpperCase?.() || 'DOC'}
{hit.hierarchy?.lvl1 || 'untitled'}
{hit.hierarchy?.lvl2 && (
{hit.hierarchy.lvl2}
)} {hit.content && (
{hit.content}
)}
); } ; ``` { // render a compact, branded hit card return (
{hit.type?.toUpperCase?.() || 'DOC'}
{hit.hierarchy?.lvl1 || 'untitled'}
{hit.hierarchy?.lvl2 && (
{hit.hierarchy.lvl2}
)} {hit.content && (
{hit.content}
)}
); }} insights={true} translations={{ button: { buttonText: 'custom hits (demo)' } }} /> --- ## Opening links in new tabs By default, DocSearch opens search result links in the current window. If you want results to open in new tabs, you need to use both a custom `hitComponent` and the `navigator` prop to handle both click and keyboard navigation consistently. ```jsx // Custom hit component with target="_blank" function HitWithNewTab({ hit, children }) { return ( {children} ); } // 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'), }; ; ``` ( {children} )} navigator={{ navigate: ({ itemUrl }) => window.open(itemUrl, '_blank'), navigateNewTab: ({ itemUrl }) => window.open(itemUrl, '_blank'), navigateNewWindow: ({ itemUrl }) => window.open(itemUrl, '_blank'), }} insights={true} translations={{ button: { buttonText: 'open in new tabs (demo)' } }} />

:::warning **Note**: Using only `hitComponent` with `target="_blank"` will work for mouse clicks, but keyboard navigation (arrows + Enter) requires the `navigator` prop to consistently open links in new tabs. ::: --- ## Bring-your-own-data shape with `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: ```jsx 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)' } }} /> ``` 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)' } }} /> --- ## Tips - **Instrumentation**: enable `insights` to send usage analytics and iterate on relevance. - **Ask AI scoping**: use `facetFilters`, `filters`, `attributesToRetrieve`, `restrictSearchableAttributes`, and `distinct` to control ai context and improve answer quality. - **Customization**: use `hitComponent`, `transformItems`, and `translations` to make docsearch feel native to any product surface.