
Create a search page with InstantSearch
First, begin with some starter code. In a new project, create anindex.html
file and add the following code:
HTML
app.js
file to instantiate InstantSearch.
JavaScript
historyRouter
router which you want to reuse in your Autocomplete integration.
This lets InstantSearch understand query parameters from the URL to derive its state.
If your Algolia index doesn’t have hierarchical attributes, you can use a regular attribute with a menu
widget.
You should now have a working InstantSearch application.
Use Autocomplete as a search box
InstantSearch ships with asearchBox
component,
but it doesn’t provide autocomplete features like you see on YouTube and Amazon. Instead,
you can replace the searchBox
with an autocomplete experience, using Autocomplete.
Start by removing the InstantSearch #searchbox
container and adding the Autocomplete container,
named #autocomplete
here.
HTML
searchBox
from your InstantSearch implementation and instantiate Autocomplete in your app.js
file.
JavaScript
Add recent searches
When you make a 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. Autocomplete lets you add recent searches with the@algolia/autocomplete-plugin-recent-searches
package.
It exposes a createLocalStorageRecentSearchesPlugin
function to let you create a recent searches plugin.
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:
Add Query Suggestions
The most typical pattern you can see on every autocomplete is suggestions. They’re predictions of queries that match what users are currently typing, and that guarantee 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 especially useful on mobile, where typing is harder than on 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.
Debounce search results
Having two sets of results update as you type generates many UI flashes. This is distracting for users, because two distinct sections of the page are competing for their attention. You can mitigate this problem by debouncing search results.JavaScript
Support categories in Query Suggestions
A key feature to Autocomplete is to pre-configure your InstantSearch page. The Query Suggestions plugin supports categories that you can use to refine both the query and the category in a single interaction This pattern brings users to the right category without interacting with thehierarchicalMenu
widget, only with the autocomplete.
First, you need to support categories in the helpers you created at the beginning.
onReset
function on your Autocomplete instance to also reset the InstantSearch category.
JavaScript
Add contextual Query Suggestions
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 scope of the search to the current category, like an actual department store, or broaden the suggestions to get out of the current category.
instant_search.facets.exact_matches.hierarchicalCategories.lvl0.value
.