Learn how to use Autocomplete with React InstantSearch.
When you think of search experiences on sites like Amazon (ecommerce) or YouTube (media),
you may notice that both sites use an autocomplete experience.
It’s the autocomplete, and not just a search input, that powers the search page.If you have an existing React InstantSearch implementation,
you can create a similar experience by adding Autocomplete to your React InstantSearch application.
Adding Autocomplete to an existing React InstantSearch implementation lets you enhance the search experience and create a richer,
more contextual search.
You can use context from the current user and how they interacted with your site,
save their recent searches, provide suggested queries, and more.
This autocomplete can work as a rich search box in a search page,
and a portable all-in-one search experience anywhere else on your site.This guide shows you how to integrate Autocomplete with React InstantSearch on your site.
The template uses a two-column layout with categories on the left and a search box,
hits, and a pagination widget on the right.
Simplify it by replacing the content of App.tsx and App.css with the following:
The React InstantSearch <SearchBox>
widget doesn’t offer autocomplete features like those seen on YouTube and Amazon.
To implement that, replace <SearchBox>
with a search box from Algolia’s Autocomplete UI library.You can store all the logic of Autocomplete in a React component.
By leveraging the hooks offered in React InstantSearch,
you can directly interface with InstantSearch in a simple but powerful way.
When you search on YouTube or Google and come back to the search box later on,
the autocomplete displays your recent searches.
This pattern lets users quickly access content by using the same path they took to find it in the first place.Add recent searches to Autocomplete with the @algolia/autocomplete-plugin-recent-searches package.
It exposes a createLocalStorageRecentSearchesPlugin
function to let you create a recent searches plugin.
Some of this code is abstracted in the following sections to simplify the examples.
Since the recentSearchesPlugin reads from localStorage, you can’t see any recent searches until you perform at least one query. To submit a search, make sure to press Enter on the query. Once you do, you’ll see it appear as a recent search.See also:
The most typical pattern you can see on every autocomplete is suggestions.
They’re predictions of queries that match what users are typing and are guaranteed to return results.
For example, when typing “how to” in Google, the search engine suggests matching suggestions for users to complete their query.
It’s beneficial on mobile devices, where typing is more demanding than a physical keyboard.Autocomplete lets you add Query Suggestions with the @algolia/autocomplete-plugin-query-suggestions package. It exposes a createQuerySuggestionsPlugin function to let you create a Query Suggestions plugin.This plugin requires a Query Suggestions index.
Having two sets of results update as you type generates many UI flashes.
This is distracting for users because two distinct sections compete for their attention.You can mitigate this problem by debouncing search results.
A key feature of Algolia’s Autocomplete is the ability to pre-configure your InstantSearch page.
The Query Suggestions plugin supports categories that you can use to refine the query and the category in a single interaction.
This brings users to the correct category without interacting with the <HierarchicalMenu> widget, but with Autocomplete instead.First, you need to refine on categories, and support categories in the helpers you created at the beginning.
For an even richer Autocomplete experience, you can pick up the currently active InstantSearch category and provide suggestions for both this specific category and others. This pattern lets you reduce the search scope to the current category, like an actual department store, or broaden the suggestions to get out of the current category.First, make sure to set your category attribute as a facet in your Query Suggestions index.
In this demo, the attribute to facet is instant_search.facets.exact_matches.hierarchicalCategories.lvl0.value.
Autocomplete is now the primary method for users to refine React InstantSearch results.
From now on, you’re leveraging the complete Autocomplete ecosystem to bring a state-of-the-art search experience for desktop and mobile.You can now add Autocomplete everywhere on your site and redirect users to the search page whenever they submit a search or after they select a suggestion.
You can also use context from the current page to personalize the autocomplete experience.
For example, you could display a preview of matching results in a panel for each suggestion and let InstantSearch provide these results once on the search page.Code examples: