Before you begin
This guide requires InstantSearch.js and a Query Suggestions index. If you already have one, go directly to the detailed implementation overview.Implementation guide
Create a Query Suggestions index
- Go to the Query suggestions tab of the dashboard.
- Click the New Query Suggestion button.
- Fill in the relevant “Name”, “Languages”, and “Source Index” inputs, then click the Accept button.
For more information, see Query Suggestions.
High-level overview
To create predictive search suggestions, you overlap two containers (a search box and a predictive box), then send your user’s current query to your suggestions index at every keystroke. Based on the query, the predictive box displays and consistently updates the first result from the suggestions. You can also display other results under the search box (in a drop-down menu or a grid).- Wrap your search input in a container (for example, a
<div>). - Declare a second container within the first one. This second container displays the suggestions.
- Use CSS to adjust the position of the search box so that it perfectly overlaps with the suggestions container.
- Change the text color of the suggestion container so that the query and the recommendation are distinctive.
- At every keystroke, send the query to your Query Suggestions index.
- Every time you get a response from the Query Suggestions index, update the text in your suggestions container with the first result that starts with the same letters as the input query. You can also display other suggestions beneath your search box if you want.
Predicted suggestions must begin with the same letters as your user’s input.
Detailed overview
This guide walks you through creating a predictive search box widget, which you can import into your app.1
Create search box
Write a render function that creates a search box with three The search box is composed of three main parts. This guide refers to them as:
<div>s:- The search box,
- The overlapping predictive box
- A display area for extra suggestions.
JavaScript
- The search box: the input field where users type in their search queries (
.search-box-container). - The predictive box: the
<div>that overlaps with the search box and contains the top suggestion (.predictive-box). - The suggestions tags: the
<div>that contains all the extra suggestions from the Query Suggestions index (.suggestion-tags-container).
2
Style the search box
Write the CSS to style the search box and predictive box. The goal is to ensure:
- The
<div>s overlap perfectly - The predictive box appears behind the search box (so that your users can still type their queries)
- The predictive box and search box have different text colors.
CSS
This guide doesn’t include the complete CSS.
You can find it in the source code.
3
Detect user key press
Define a helper function that checks if users have pressed a specific key.
JavaScript
4
Create a predictive search box
-
Create a
PredictiveSearchBoxclass. You’ll add methods in the following steps.JavaScript -
Add a constructor for the
PredictiveSearchBox.JavaScript -
Add an initialization method for the
PredictiveSearchBoxclass.JavaScript
5
Register handlers
Register handlers for searching, handling suggestions, tab completion, and keyboard navigation.
JavaScript
6
Add setter method
Add a setter method for the value of the search box input.
JavaScript
7
Set search box value to the suggestion in the predictive search box
Add a handler method that calls
setSearchBoxValue to set the search box value to the suggestion displayed in the predictive box when users press the right arrow.JavaScript
8
Update suggestions
Add a method that updates the suggestions tags based on the new hits from the query response.
JavaScript
9
Search Query Suggestions index
Add a method that dispatches a search on your Query Suggestions index and uses the results to update the suggestions in the predictive box and suggestions tags. This method triggers whenever the query changes.
JavaScript
10
Clear suggestions
-
Add a method that removes all suggestions tags so they can be updated.
JavaScript
-
Add a method that removes the predictive box suggestion.
JavaScript
-
Add a method that clears all suggestion content from your search container, letting you update it with new suggestions.
JavaScript
11
Export predictive search box
Export the
PredictiveSearchBox class so you can import it into your app.JavaScript