autocomplete-plugin-query-suggestions package.
Before you begin
This guide assumes that you know HTML, CSS, and JavaScript and that you have existing HTML with an input element where you want to insert the autocomplete drop-down menu. It also assumes that you have an Algolia with a populated Query Suggestions .Get started
Create anindex.js file in your src directory, and add the following code:
JavaScript
autocomplete as id.
Update the container to match your markup parameter to match your HTML.
Setting openOnFocus to true ensures that the drop-down menu appears as soon as a user focuses the input.
For now, plugins is an empty array, but you’ll learn how to add the Query Suggestions plugin next.
Add Query Suggestions
Theautocomplete-plugin-query-suggestions package provides the createQuerySuggestionsPlugin function for creating a Query Suggestions plugin out-of-the-box.
It requires an Algolia search client
initialized with an Algolia application ID and API key and an indexName.
The indexName is the name of your Query Suggestions index.
JavaScript
getSearchParams function to apply Algolia query parameters to the suggestions returned from the plugin.
For example, you may choose to display 10 Query Suggestions if users haven’t typed anything yet, but only five if they have:
JavaScript
Add categories
Displaying relevant categories, along with suggestions, is helpful since it lets users limit their search scope. When a user selects a suggestion with a category, you can use both the suggestion and the associated category to show only the most relevant results. This lets users skip the additional task of selecting a category once they’re on the results page. By including categories in your suggestions, you enable users to land on the most relevant set of results with as little friction as possible. With some configuration, the Algolia Query Suggestions feature adds relevant categories to suggestion . Refer to the index schema to see how the feature stores information about each suggestion record. To display categories with the suggestions, you need to define the attribute to retrieve category information from, using thecategoryAttribute option when instantiating the plugin.
In this example, the category data is stored in the nested attribute instant_search.facets.exact_matches.categories.
With this structure, you need to provide the path ["instant_search", "facets", "exact_matches", "categories"]
as the categoryAttribute.
You can also set the number of items to display categories for using itemsWithCategories and the maximum number of categories to display per item using categoriesPerItem.
Both default to 1.
JavaScript
Apply categories
Now that the autocomplete displays categories on suggestions, you need to apply them to the search results when a user selects a suggestion.On the same page
If the search results are on the same page as the autocomplete, you can use theonSelect hook to refine the search results.
You can access this hook within transformSource.
The function includes the original source, which you should return along with any options you want to add or overwrite.
JavaScript
On a linked search results page
If your autocomplete links to a separate search results page and you’d like to apply selected categories there, you can modify the results page URL with query parameters. This example writes acreateUrl function within transformSource to do so.
It uses an item’s __autocomplete_qsCategory property to construct the appropriate query parameters.
It then uses the function within the item template and in getItemUrl.
This way, whether users click a link or use keyboard navigation, they land on a search results page with categories applied.
Customize Query Suggestions
ThecreateQuerySuggestionsPlugin creates a functional plugin out of the box. You may want to customize some aspects of it, depending on your use case. To change templates or other source configuration options, you can use transformSource. The function includes the original source, which you should return along with any options you want to add or overwrite.
For example, if you use Autocomplete as an entry point to a search results page,
you can turn Query Suggestions into links by modifying getItemUrl and the item template.
onSelect:
JavaScript