You are currently reading the documentation for InstantSearch.js v4.
Read the migration guide to learn how to upgrade from v3 to v4.
You can still access the v3 documentation for this page.
searchBox
.
Once you’ve configured the generation of the Query Suggestions index, you need to query this index as well.
You can use a multi-index search for that.
This guide describes how to build a searchBox
that displays a list of suggestions along with the associated categories.
Once user select a suggestion, Algolia will apply the query and the category.
If you’re building an autocomplete with Query Suggestions,
you should use the Autocomplete library which lets you build a full-featured, accessible autocomplete experience.
For more information, see:
Refine your results with the suggestions
The first step of this guide is to set up your custom autocomplete component. You can use the library Selectize that provides an API to create an Autocomplete menu. Once you have this component, you need to wrap it with theautocomplete
connector. You can find more information in the guide on autocomplete
.
All examples in this guide assume you’ve included InstantSearch.js in your web page from a CDN.
If you’re using a package manager, adjust how you import InstantSearch.js and its widgets.
JavaScript
JavaScript
Use the related categories in the autocomplete
A typical use of autocomplete is to display relevant categories and suggestions. Then when a user selects a suggestion, both the suggestion and the associated category are used to refine the search. For this example, the relevant categories are stored on the suggestions records. You have to update the render function to display the categories with the suggestions. For simplicity and brevity of the code, assume that all suggestions have categories, but this isn’t the case in the actual dataset. Take a look at the complete example to see the actual implementation.JavaScript
data-*
attributes API available on DOM elements. You can store the category on the suggestion element to find it when the suggestion is selected.
JavaScript
onChange
callback. You can use the same strategy as for the query to provide the category back to the main instance.
JavaScript
Refine your results with the related categories
The last section of the guide explains how to use the related categories on the main search. The first step is to create a virtual widget. This widget is only used for filtering: it doesn’t render anything on the page (hence the name “virtual”). It’s handy when you have a situation where you have to manipulate the search state manually. If this is desired, refine a category without using built-in widgets likemenu
or refinementList
.
JavaScript
onSelectChange
callback provided to the autocomplete component. To update the search parameters, you can use the Algolia search helper.
JavaScript