> ## Documentation Index
> Fetch the complete documentation index at: https://www.algolia.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Build a Query Suggestions UI with Autocomplete

> Learn how to include suggested searches using the Query Suggestions plugin.

Prefer using the Autocomplete widget from InstantSearch instead of the standalone Autocomplete library.
It integrates more closely with your search experience.
For more information,
see the API reference for [InstantSearch.js](https://www.algolia.com/doc/api-reference/widgets/autocomplete/js.md) or
[React InstantSearch](https://www.algolia.com/doc/api-reference/widgets/autocomplete/react.md).

The most common Autocomplete UX is one that displays a list of suggested searches that your users can select from as they type.
Algolia provides a Query Suggestions feature that generates suggested search terms.
It [bases these terms on what your users search for and the results in your data](https://www.algolia.com/doc/guides/building-search-ui/ui-and-ux-patterns/query-suggestions/js.md#how-query-suggestions-work).

This tutorial explains how to integrate Algolia Query Suggestions into an Autocomplete menu using the [`autocomplete-plugin-query-suggestions`](https://www.algolia.com/doc/ui-libraries/autocomplete/api-reference/autocomplete-plugin-query-suggestions) package.

## Before you begin

This guide assumes that you know HTML, CSS, and JavaScript.
It also assumes 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 (An Algolia application is a self-contained environment with its own indices, configuration, and API keys. Applications don't share data or settings with each other.) with a populated [Query Suggestions](https://www.algolia.com/doc/guides/building-search-ui/ui-and-ux-patterns/query-suggestions/js.md) index (An Algolia index is a searchable dataset that consists of records and configuration settings. These settings define how the records are searched and ranked.).

> [!TIP]
>
> For learning purposes, use the demo application credentials and index provided in this tutorial.

## Get started

Create an `index.js` file in your `src` directory, and add the following code:

```js JavaScript icon=code theme={"system"}
// index.js
import { autocomplete } from "@algolia/autocomplete-js";

autocomplete({
  container: "#autocomplete",
  plugins: [],
  openOnFocus: true,
});
```

This code adds Autocomplete to a DOM element with `autocomplete` as `id`.
Update the [`container`](https://www.algolia.com/doc/ui-libraries/autocomplete/api-reference/autocomplete-js/autocomplete#param-container) to [match your markup](https://www.algolia.com/doc/ui-libraries/autocomplete/core-concepts/basic-configuration-options.md) parameter to match your HTML.
Setting [`openOnFocus`](https://www.algolia.com/doc/ui-libraries/autocomplete/api-reference/autocomplete-js/autocomplete#param-open-on-focus) 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](https://www.algolia.com/doc/ui-libraries/autocomplete/core-concepts/plugins.md) next.

## Add Query Suggestions

The [`autocomplete-plugin-query-suggestions`](https://www.algolia.com/doc/ui-libraries/autocomplete/api-reference/autocomplete-plugin-query-suggestions) package provides the [`createQuerySuggestionsPlugin`](https://www.algolia.com/doc/ui-libraries/autocomplete/api-reference/autocomplete-plugin-query-suggestions/createQuerySuggestionsPlugin) function for creating a Query Suggestions [plugin](https://www.algolia.com/doc/ui-libraries/autocomplete/core-concepts/plugins.md) out-of-the-box.

It requires an [Algolia search client](https://www.algolia.com/doc/libraries/sdk/install.md)
initialized with an [Algolia application ID and API key](https://www.algolia.com/doc/guides/sending-and-managing-data/send-and-update-your-data/how-to/importing-with-the-api.md#required-credentials) and an `indexName`.
The `indexName` is the name of your [Query Suggestions](https://www.algolia.com/doc/guides/building-search-ui/ui-and-ux-patterns/query-suggestions/js.md) index.

```js JavaScript icon=code theme={"system"}
// index.js
import { autocomplete } from "@algolia/autocomplete-js";
import { createQuerySuggestionsPlugin } from "@algolia/autocomplete-plugin-query-suggestions";

const appId = "latency";
const apiKey = "6be0576ff61c053d5f9a3225e2a90f76";
const searchClient = algoliasearch(appId, apiKey);

const querySuggestionsPlugin = createQuerySuggestionsPlugin({
  searchClient,
  indexName: "instant_search_demo_query_suggestions",
});

autocomplete({
  container: "#autocomplete",
  plugins: [querySuggestionsPlugin],
  openOnFocus: true,
});
```

You can optionally pass a `getSearchParams` function to apply [Algolia query parameters](https://www.algolia.com/doc/api-reference/api-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:

```js JavaScript icon=code theme={"system"}
import { autocomplete } from "@algolia/autocomplete-js";
import { createQuerySuggestionsPlugin } from "@algolia/autocomplete-plugin-query-suggestions";

const appId = "latency";
const apiKey = "6be0576ff61c053d5f9a3225e2a90f76";
const searchClient = algoliasearch(appId, apiKey);

const querySuggestionsPlugin = createQuerySuggestionsPlugin({
  searchClient,
  indexName: "instant_search_demo_query_suggestions",
  getSearchParams({ state }) {
    return { hitsPerPage: state.query ? 5 : 10 };
  },
});

autocomplete({
  container: "#autocomplete",
  plugins: [querySuggestionsPlugin],
  openOnFocus: true,
});
```

This option can be useful if you're [displaying other sources](https://www.algolia.com/doc/ui-libraries/autocomplete/guides/including-multiple-result-types.md) along with Query Suggestions.
Use it to always show the same total number of items, or to align your query parameters.

## 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](https://www.algolia.com/doc/guides/building-search-ui/ui-and-ux-patterns/query-suggestions/js.md),
the Algolia [Query Suggestions](https://www.algolia.com/doc/guides/building-search-ui/ui-and-ux-patterns/query-suggestions/js.md) feature adds relevant categories to suggestion records (A record is a searchable object in an Algolia index. Each record consists of named attributes.).
Refer to the [index schema](https://www.algolia.com/doc/guides/building-search-ui/ui-and-ux-patterns/query-suggestions/js.md#query-suggestions-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 the `categoryAttribute` 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 that show categories with `itemsWithCategories`.
Use `categoriesPerItem` to set the number of categories to show per item.
Both default to `1`.

```js JavaScript icon=code theme={"system"}
import { autocomplete } from "@algolia/autocomplete-js";
import { createQuerySuggestionsPlugin } from "@algolia/autocomplete-plugin-query-suggestions";

const appId = "latency";
const apiKey = "6be0576ff61c053d5f9a3225e2a90f76";
const searchClient = algoliasearch(appId, apiKey);

const querySuggestionsPlugin = createQuerySuggestionsPlugin({
  searchClient,
  indexName: "instant_search_demo_query_suggestions",
  getSearchParams({ state }) {
    return { hitsPerPage: state.query ? 5 : 10 };
  },
  categoryAttribute: [
    "instant_search",
    "facets",
    "exact_matches",
    "categories",
  ],
  itemsWithCategories: 2,
  categoriesPerItem: 2,
});

autocomplete({
  container: "#autocomplete",
  plugins: [querySuggestionsPlugin],
  openOnFocus: true,
});
```

### 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 the [`onSelect`](https://www.algolia.com/doc/ui-libraries/autocomplete/core-concepts/sources#param-on-select) hook to refine the search results.
You can access this hook within [`transformSource`](https://www.algolia.com/doc/ui-libraries/autocomplete/api-reference/autocomplete-plugin-query-suggestions/createQuerySuggestionsPlugin#param-transform-source).
The function includes the original `source`.
Return it along with any options you want to add or overwrite.

```js JavaScript icon=code theme={"system"}
import { autocomplete } from "@algolia/autocomplete-js";
import { createQuerySuggestionsPlugin } from "@algolia/autocomplete-plugin-query-suggestions";

const appId = "latency";
const apiKey = "6be0576ff61c053d5f9a3225e2a90f76";
const searchClient = algoliasearch(appId, apiKey);

const querySuggestionsPlugin = createQuerySuggestionsPlugin({
  // ...
  transformSource({ source }) {
    return {
      ...source,
      onSelect({ item }) {
        // Assuming the `setSearchState` function updates the search page state.
        setSearchState({
          query: item.query,
          category: item.__autocomplete_qsCategory,
        });
      },
    };
  },
});

autocomplete({
  container: "#autocomplete",
  plugins: [querySuggestionsPlugin],
  openOnFocus: true,
});
```

#### On a linked search results page

If your autocomplete links to a separate search results page, you can apply selected categories there.
To do this, change the results page URL with query parameters.

This example writes a `createUrl` function within [`transformSource`](https://www.algolia.com/doc/ui-libraries/autocomplete/api-reference/autocomplete-plugin-query-suggestions/createQuerySuggestionsPlugin#param-transform-source) 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](https://www.algolia.com/doc/ui-libraries/autocomplete/core-concepts/templates#param-templates-item) and in [`getItemUrl`](https://www.algolia.com/doc/ui-libraries/autocomplete/core-concepts/sources#param-get-item-url).
This way, whether users click a link or use keyboard navigation, they land on a search results page with categories applied.

```js JavaScript theme={"system"}
import { autocomplete } from "@algolia/autocomplete-js";
import { createQuerySuggestionsPlugin } from "@algolia/autocomplete-plugin-query-suggestions";

const appId = "latency";
const apiKey = "6be0576ff61c053d5f9a3225e2a90f76";
const searchClient = algoliasearch(appId, apiKey);

const querySuggestionsPlugin = createQuerySuggestionsPlugin({
  // ...
  transformSource({ source }) {
    function createUrl(item) {
      const urlParams = new URLSearchParams();
      urlParams.set("q", item.query);
      if (item.__autocomplete_qsCategory) {
        urlParams.set("category", item.__autocomplete_qsCategory);
      }

      return `/search?${urlParams.toString()}`;
    }

    return {
      ...source,
      getItemUrl({ item }) {
        return createUrl(item);
      },
      templates: {
        item(params) {
          const { item, html } = params;

          return html`<a class="aa-ItemLink" href="${createUrl(item)}">
            ${source.templates.item(params).props.children}
          </a>`;
        },
      },
    };
  },
});

autocomplete({
  container: "#autocomplete",
  plugins: [querySuggestionsPlugin],
  openOnFocus: true,
});
```

```jsx JSX theme={"system"}
/** @jsx h */
import { h } from "preact";
import { autocomplete } from "@algolia/autocomplete-js";
import { createQuerySuggestionsPlugin } from "@algolia/autocomplete-plugin-query-suggestions";

const appId = "latency";
const apiKey = "6be0576ff61c053d5f9a3225e2a90f76";
const searchClient = algoliasearch(appId, apiKey);

const querySuggestionsPlugin = createQuerySuggestionsPlugin({
  // ...
  transformSource({ source }) {
    function createUrl(item) {
      const urlParams = new URLSearchParams();
      urlParams.set("q", item.query);
      if (item.__autocomplete_qsCategory) {
        urlParams.set("category", item.__autocomplete_qsCategory);
      }

      return `/search?${urlParams.toString()}`;
    }

    return {
      ...source,
      getItemUrl({ item }) {
        return createUrl(item);
      },
      templates: {
        item(params) {
          const { item } = params;

          return (
            <a className="aa-ItemLink" href={createUrl(item)}>
              {source.templates.item(params).props.children}
            </a>
          );
        },
      },
    };
  },
});

autocomplete({
  container: "#autocomplete",
  plugins: [querySuggestionsPlugin],
  openOnFocus: true,
});
```

## Customize Query Suggestions

The [`createQuerySuggestionsPlugin`](https://www.algolia.com/doc/ui-libraries/autocomplete/api-reference/autocomplete-plugin-query-suggestions/createQuerySuggestionsPlugin) 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`](https://www.algolia.com/doc/ui-libraries/autocomplete/core-concepts/templates) or other [source](https://www.algolia.com/doc/ui-libraries/autocomplete/core-concepts/sources.md) configuration options, you can use [`transformSource`](https://www.algolia.com/doc/ui-libraries/autocomplete/api-reference/autocomplete-plugin-query-suggestions/createQuerySuggestionsPlugin#param-transform-source).
The function includes the original `source`.
Return it 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`](https://www.algolia.com/doc/ui-libraries/autocomplete/core-concepts/sources#param-get-item-url) and the [`item`](https://www.algolia.com/doc/ui-libraries/autocomplete/core-concepts/templates#param-templates-item) template.

```js JavaScript theme={"system"}
const querySuggestionsPlugin = createQuerySuggestionsPlugin({
  // ...
  transformSource({ source }) {
    return {
      ...source,
      getItemUrl({ item }) {
        return `/search?q=${item.query}`;
      },
      templates: {
        item(params) {
          const { item, html } = params;

          return html`<a class="aa-ItemLink" href="/search?q=${item.query}">
            ${source.templates.item(params).props.children}
          </a>`;
        },
      },
    };
  },
});
```

```jsx JSX theme={"system"}
const querySuggestionsPlugin = createQuerySuggestionsPlugin({
  // ...
  transformSource({ source }) {
    return {
      ...source,
      getItemUrl({ item }) {
        return `/search?q=${item.query}`;
      },
      templates: {
        item(params) {
          const { item } = params;

          return (
            <a className="aa-ItemLink" href={`/search?q=${item.query}`}>
              {source.templates.item(params).props.children}
            </a>
          );
        },
      },
    };
  },
});
```

If you use Autocomplete on the same page as your main search, you may want to avoid a full page reload when a user selects an item.
To do this, update your search query state on selection with [`onSelect`](https://www.algolia.com/doc/ui-libraries/autocomplete/core-concepts/sources#param-on-select):

```js JavaScript icon=code theme={"system"}
const querySuggestionsPlugin = createQuerySuggestionsPlugin({
  // ...
  transformSource({ source }) {
    return {
      ...source,
      onSelect({ item }) {
        // Assuming the `setSearchState` function updates the search page state.
        setSearchState({ query: item.query });
      },
    };
  },
});
```

## Next steps

This tutorial focuses on adding Query Suggestions to an autocomplete menu.
Many autocomplete menus also include recent searches and possibly other items.

* [Recent searches](https://www.algolia.com/doc/ui-libraries/autocomplete/guides/adding-recent-searches.md)
* [Multiple results types](https://www.algolia.com/doc/ui-libraries/autocomplete/guides/including-multiple-result-types.md)
* [Static sources](https://www.algolia.com/doc/ui-libraries/autocomplete/core-concepts/sources.md#static-sources)
