Skip to main content
Predictive search suggestions lets users quickly access the content they’re looking for because the most relevant suggestions appear directly inside the search box. They can also see the most popular searches and discover more of your content.

Open CodeSandbox

Run and edit the Predictive search suggestions example in CodeSandbox.

View code on GitHub

View the Predictive search suggestions example code on GitHub.

Before you begin

This guide requires:

Implementation guide

Create a Query Suggestions index

  1. Go to the Query suggestions tab of the dashboard.
  2. Click the New Query Suggestion button.
  3. 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 Query Suggestions 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).
  1. Wrap your search input in a container (for example, a <div>).
  2. Declare a second container within the first one. This second container displays the suggestions.
  3. Use CSS to adjust the position of the search box so that it perfectly overlaps with the suggestions container.
  4. Change the text color of the suggestion container so that the query and the recommendation are distinctive.
  5. At every keystroke, send the query to your Query Suggestions index.
  6. 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 <div>s:
  • The search box,
  • The overlapping predictive box
  • A display area for extra suggestions.
JavaScript
The search box is composed of three main parts. This guide refers to them as:
  • 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

  1. Create a PredictiveSearchBox class. You’ll add methods in the following steps.
    JavaScript
  2. Add a constructor for the PredictiveSearchBox.
    JavaScript
  3. Add an initialization method for the PredictiveSearchBox class.
    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

  1. Add a method that removes all suggestions tags so they can be updated.
    JavaScript
  2. Add a method that removes the predictive box suggestion.
    JavaScript
  3. 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
Last modified on July 14, 2026