getSources option.
This tutorial outlines how to combine static predefined items, recent searches and Query Suggestions.
Using plugins for each source makes the code modular, reusable, and sharable.
However, instead of using plugins, you could add different sources directly in getSources.
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 application with a populated Query Suggestions index.Starter code
Begin by adding the following Autocomplete starter code. Create a file calledindex.js in your src directory.
JavaScript
autocomplete as an id.
Change the container to match your markup. 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 create and add plugins for predefined items, recent searches, and Query Suggestions next.
Add predefined items
A popular search pattern for autocomplete menus shows predefined search terms as soon as a user clicks on the search box and before they begin typing anything. This provides a guided experience and exposes users to helpful resources or other content you want them to see. This tutorial describes how to create a plugin to show static, predefined items. In particular, it exposes helpful links users may want to refer to.Create a predefined items plugin
Begin by creating apredefinedItemsPlugin.js file in your src directory, with the following code:
JavaScript
predefinedItemsPlugin plugin has a similar signature as any other autocomplete implementation:
it uses the getSources option to return an array of items to display.
Each object in the array defines where to get items using getItems.
An Autocomplete plugin is an object that implements the AutocompletePlugin interface.
For more information, see Building your own plugin.
In this example, getItems returns a filtered array of predefinedItems.
The code filters the array to return items that match the query, if it exists.
If it doesn’t, it returns the entire array.
You can return whatever predefined items you like and format them accordingly.
For example, suppose you want to show trending search items instead of helpful links.
Use getItems to retrieve them from another source,
including an asynchronous API.
The getItemUrl function defines how to get the URL of an item.
In this case, since it’s an attribute on each object in the predefinedItems array, just return the attribute.
Use getItemUrl to add keyboard navigation to the autocomplete menu.
Users can scroll through items in the menu with the up and down keys.
When they hit Enter on one of the predefinedItems or any source that includes getItemUrl, it opens the URL retrieved from getItemUrl.
Templates define how to display each section of the autocomplete, including the header, footer, and each item.
Templates can return any valid virtual DOM element.
This example defines how to display each predefined item with the item template and gives a header for the entire section.
Add the predefined items plugin to the autocomplete
Import the newly createdpredefinedItemsPlugin and add it toplugins in index.js.
Once you’ve done that, the file should look like this:
JavaScript
Add recent searches and Query Suggestions
You can add recent searches and Query Suggestions using out-of-the-box plugins.Create a recent searches plugin
Use the out-of-the-boxcreateLocalStorageRecentSearchesPlugin function to create a recent searches plugin:
JavaScript
key can be any string and is required to differentiate search histories if you have several Autocomplete experiences on one page.
The limit defines the maximum number of recent searches to display.
Create a Query Suggestions plugin
If you don’t have a Query Suggestions index yet, create one.
Use the demo app credentials and index name provided in this tutorial.
createQuerySuggestionsPlugin function to create a Query Suggestions plugin.
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
Coordinate Query Suggestions with other sources
When instantiating your Query Suggestions plugin, you can optionally pass agetSearchParams function to apply Algolia query parameters to the suggestions returned from the plugin.
This is helpful if you need to coordinate your Query Suggestions with other sections displayed in the autocomplete,
like recent searches.
For example, if you’d like to show a combined total of 10 search terms (recent searches plus Query Suggestions),
you can indicate this:
JavaScript
limit parameter) and up to 10 total search terms.
If there’s only one recent search in local storage,
the autocomplete displays nine Query Suggestions, assuming that there are nine relevant suggestions.
Separate result types
When using sources other than just recent and suggested searches, it’s best to label the different result types withheaders.
For the createLocalStorageRecentSearchesPlugin and createQuerySuggestionsPlugin plugins,
use the transformSource option to do this.
Add plugins
All that’s left to do is add all your plugins to your Autocomplete instance:JavaScript