Skip to main content
Autocomplete is a ubiquitous part of most search experiences. Search providers like Google, ecommerce sites like Amazon, and messaging apps like Slack all offer autocomplete experiences on mobile and desktop. Screenshot of a search box with 'laptop' entered, showing autocomplete suggestions and product listings for Apple MacBooks. Algolia provides a full-featured solution to build autocomplete experiences with InstantSearch. The autocomplete widget lets you build an accessible, as-you-type autocomplete user interface that you can integrate anywhere on your site or app.

Open CodeSandbox

Run and edit the Autocomplete example in CodeSandbox.

Explore source code

Browse the source for the Autocomplete example on GitHub.
A common pattern in search is to implement a search box with an autocomplete as a first step of the search experience. Adding an autocomplete to a search page lets you enhance the search experience and create a richer, more contextual search. You can use the autocomplete widget in an existing or a new InstantSearch.js implementation to create this kind of experience.

Install InstantSearch.js

The autocomplete widget is part of InstantSearch.js. Install InstantSearch.js and the Algolia API client from the npm registry:
Import a theme to style the widget out of the box:
JavaScript

Add a container

The widget renders into a container element. Add one to your markup:
HTML
Provide a container (for example, a div), not an input. The widget generates a fully accessible search box for you.

Add the widget to InstantSearch

Unlike the standalone Autocomplete library, the autocomplete widget runs inside an InstantSearch instance. Create the instance, add the widget with addWidgets, then start the search:
JavaScript
The widget is added to InstantSearch but it doesn’t show results until you configure the indices to query.

Display results from an index

Use the indices option to define which indices to query and how to display their results. Each index can declare its own templates and a getURL function to navigate when a user selects an item:
JavaScript
You can query several indices at once by adding more objects to the indices array. To learn more, see Including multiple result types.

Customize result templates

Each index renders with its own header, item, and noResults templates, and the widget accepts a top-level panel template for the overall layout. The Display results from an index section covers header and item.

Highlight the matched query

Item templates receive a components object with Highlight, ReverseHighlight, Snippet, and ReverseSnippet helpers that emphasize the part of an attribute matching the user’s query:
JavaScript
Use Snippet to highlight within a longer, truncated attribute, and the Reverse variants to emphasize the text that doesn’t match. This is a common style for Query Suggestions.

Show an empty state

The noResults template renders when an index returns nothing. If any index defines it, the panel stays open even when every index is empty, so you can show a helpful message:
JavaScript

Customize the panel layout

The top-level panel template controls the layout of the whole panel, including the order and grouping of recent searches, query suggestions, and each index. It receives an elements object whose keys are recent, suggestions, and each index’s indexName, so you can arrange sections into rows or columns—for example, suggestions on the left and results on the right:
JavaScript
The template only sets the markup. Style the columns with your own CSS:
CSS

Filter results

To narrow what an index returns, set Algolia search parameters on that index with searchParameters. You can apply filters, facetFilters, numericFilters, and other parameters per index:
JavaScript
The widget applies these parameters to every query for that index. For an interactive filter UI inside the autocomplete—such as selectable tags or facet values that users add and remove—use the standalone Autocomplete library, which provides the tags plugin and facet sources.

Reshape and deduplicate results

Use transformItems to adjust results before they’re displayed—for example, to remove duplicates across indices or limit the number of items each index shows. The function receives an array with one entry per index, each shaped as { indexName, indexId, hits, results }, and must return the same shape:
JavaScript
Unlike the standalone library’s reshape API, the widget doesn’t group or sort sources for you—deduplication and per-index limits are manual, as shown above.

Add Query Suggestions and recent searches

The widget can display query suggestions from a Query Suggestions index and recent searches stored in the browser’s local storage. Enable them with showQuerySuggestions and showRecent:
JavaScript
To customize suggestions and recent searches, set a custom storage key, or refine your main index when a user selects a suggestion, see Build a Query Suggestions UI.

Send click and conversion events

To send click and conversion events when users interact with the autocomplete, enable insights on the InstantSearch instance:
JavaScript
The widget then sends view and click events automatically when users see and select results. Conversion events, like add-to-cart, usually happen after the user leaves the autocomplete, on the destination page. Send those where the conversion occurs with the Insights client. The autocomplete item template doesn’t expose a sendEvent function.

Support keyboard navigation

The widget renders an accessible combobox, so users can operate it entirely from the keyboard—no extra configuration:
  • Up and Down arrows move through the items. The highlight wraps around at the ends of the list.
  • Enter opens the highlighted item, or submits the typed query when nothing is highlighted.
  • Escape closes the panel, then clears the highlight.
  • Tab closes the panel and moves focus to the next element.
What happens on Enter depends on how you handle navigation:
  • If the highlighted item’s index defines getURL, the browser opens that URL.
  • Otherwise, when you set getSearchPageURL, submitting the query sends the user to your search results page.
  • To use client-side routing instead of a full page load, provide an onSelect function and handle navigation in your app.
JavaScript
The widget also sets the matching ARIA roles (combobox, aria-expanded, aria-activedescendant), so screen readers announce the active item as users move through the list.

Enable detached (mobile) mode

On small screens, the widget can switch to a full-screen overlay. Use the detachedMediaQuery option to control when this happens. By default, detached mode activates at (max-width: 680px).
JavaScript
To turn off detached mode, set detachedMediaQuery to an empty string.

Explore advanced examples

See also

Last modified on July 24, 2026