docsearch() function is the main entry point for integrating DocSearch into vanilla JavaScript applications. It mounts a DocSearch component to a DOM element and returns an instance for programmatic control.
Function Signature
Parameters
Thedocsearch() function accepts a single DocSearchProps object with the following properties:
Core Configuration
string | HTMLElement
required
The DOM element or CSS selector where DocSearch will be mounted.
string
required
Your Algolia application ID.
string
required
Public API key with search permissions for your index.
string
Name of the Algolia index to query.
Array<DocSearchIndex | string>
List of indices and optional search parameters to be used for search.
Search Customization
SearchParamsObject
Additional Algolia search parameters to merge into each query.
string
Placeholder text for the search input.
number
Maximum number of hits to display per source/group.
string
Query string to prefill when opening the modal.
boolean
Disable storage and usage of recent and favorite searches.
number
default:7
Maximum number of recent searches to save and display.
number
default:4
Maximum number of recent searches to display when there are favorited searches.
Ask AI Configuration
DocSearchAskAi | string
Configuration for Ask AI mode. Pass a string assistant ID or a full config object.
Customization Functions
(items: DocSearchHit[]) => DocSearchHit[]
Hook to post-process hits before rendering.
function
Custom component to render an individual hit. Supports multiple template patterns:Parameters:
props.hit: The search hit objectprops.children: Default hit renderinghelpers.html: HTML template helper
Custom component rendered at the bottom of the results panel.Parameters:
props.state: Autocomplete state objecthelpers.html: HTML template helper
(searchClient: DocSearchTransformClient) => DocSearchTransformClient
Hook to wrap or modify the Algolia search client.
({ query }: { query: string }) => string
Builds a URL to report missing results for a given query.
Custom navigator for controlling link navigation. Useful for client-side routing.
Styling and Theming
DocSearchTheme
Theme overrides applied to the modal and related components.
DocSearchTranslations
Localized strings for the button and modal UI.
Lifecycle Callbacks
() => void
Called once DocSearch is mounted and ready for interaction.
() => void
Called when the modal opens.
() => void
Called when the modal closes.
(initialMessage: InitialAskAiMessage) => boolean | void
Intercept Ask AI requests (e.g., submitting a prompt or selecting a suggested question).Return
true to prevent the default modal Ask AI flow. Useful for routing Ask AI into a different UI (e.g., sidepanel) without flicker.Advanced Options
typeof window
The browser environment object. Useful for testing or SSR scenarios.
DocumentFragment | Element
The container element where the modal should be portaled. Defaults to
document.body.AutocompleteInsights
Insights client integration options to send analytics events.
DocSearchModalShortcuts
Configuration for keyboard shortcuts. Allows enabling/disabling specific shortcuts.Default:
{ 'Ctrl/Cmd+K': true, '/': true }Return Value
object
Returns a DocSearchInstance object with methods and properties for programmatic control.
Usage Examples
TypeScript Types
Related
- DocSearchInstance - Methods and properties for programmatic control
- Getting Started - Installation and basic setup
