
Create a search page with Vue InstantSearch
First, use a starter template for the InstantSearch implementation. The easiest way to achieve that is to usecreate-instantsearch-app
,
a command-line utility that helps to quickly get your app started.
history
router, which you want to reuse in your Autocomplete integration.
This lets InstantSearch understand query parameters from the URL to derive its state.
Vue
Use Autocomplete as a search box
Vue InstantSearch ships with a<ais-search-box>
component, but it doesn’t offer autocomplete features like those seen on YouTube and Amazon. Instead, you can replace the <ais-search-box>
with Algolia’s Autocomplete.
With Vue, you can store all the logic of Autocomplete in a single file, and expose it as a component. Start by creating a file called Autocomplete.vue
. As Autocomplete needs to interact with the InstantSearch state, you can add a mixin to transform it into a custom Vue InstantSearch widget.
Vue
mounted()
hook of the component’s lifecycle. Since it behaves as a custom InstantSearch widget, you have direct access to the underlying InstantSearch instance.
<ais-search-box>
component from your Vue InstantSearch implementation.
This replaces the InstantSearch search box with Autocomplete, and acts exactly like before.
But you can now add many more interesting features.
Add recent searches
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.
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.
For more information, see Recent searches.
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 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.
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 compete for their attention. You can mitigate this problem by debouncing search results.Vue
Support categories in Query Suggestions
A key feature of Autocomplete is 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 pattern brings users to the right category without interacting with theais-hierarchical-menu
widget but using Autocomplete.
First, you need to refine on categories, and support categories in the helpers you created at the beginning.
onReset
function on your Autocomplete instance to also reset the InstantSearch category.
JSX
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 search scope 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
.