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

# Build a Query Suggestions UI

> Use the Autocomplete widget to show Query Suggestions and recent searches.

export const Index = () => <Tooltip tip="An Algolia index is a searchable dataset that consists of records and configuration settings. These settings define how the records are searched and ranked.">
    index
  </Tooltip>;

export const customLabel_0 = undefined

<div className="not-prose algolia-flavor-switcher">
  <div className="afs-dropdown">
    <div className="afs-trigger" role="button" tabIndex="0" aria-haspopup="listbox">
      <span className="afs-current">JavaScript</span>

      <svg className="afs-chevron lucide lucide-chevron-down" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2" strokeLinecap="round" strokeLinejoin="round" aria-hidden="true">
        <path d="m6 9 6 6 6-6" />
      </svg>
    </div>

    <ul className="afs-menu" role="listbox">
      <li role="option" aria-selected="true"><a className="afs-option is-current" href="/doc/guides/building-search-ui/ui-and-ux-patterns/autocomplete/query-suggestions/js"><span className="afs-option-name">JavaScript</span><span className="afs-option-lib">InstantSearch.js</span></a></li>
      <li role="option" aria-selected="false"><a className="afs-option" href="/doc/guides/building-search-ui/ui-and-ux-patterns/autocomplete/query-suggestions/react"><span className="afs-option-name">React</span><span className="afs-option-lib">React InstantSearch</span></a></li>
    </ul>
  </div>
</div>

Use [Query Suggestions](/doc/guides/building-search-ui/ui-and-ux-patterns/query-suggestions/js) to help users find popular searches as they type.
Query Suggestions creates an <Index /> from popular searches.
You can use this index to show search suggestions.

After you configure the Query Suggestions index, use the [`autocomplete`](/doc/api-reference/widgets/autocomplete/js) widget to show suggestions.
When users select a suggestion, the widget navigates to the suggestion URL or refines the search query, depending on your configuration.

<Columns>
  <Card title="Open CodeSandbox" icon="codesandbox" href="https://codesandbox.io/s/github/algolia/instantsearch/tree/master/examples/js/query-suggestions">
    Run and edit the Query Suggestions UI example in CodeSandbox.
  </Card>

  <Card title="Explore source code" icon="github" href="https://github.com/algolia/instantsearch/tree/master/examples/js/query-suggestions">
    Browse the source code for the Query Suggestions UI example on GitHub.
  </Card>
</Columns>

<Callout icon="flask-conical" color="#14b8a6">
  This widget is **{customLabel_0 || "experimental"}** and is subject to change in minor versions.
</Callout>

## Add Query Suggestions and recent searches

The widget displays suggestions from a [Query Suggestions index](/doc/guides/building-search-ui/ui-and-ux-patterns/query-suggestions/js) and recent searches stored in the browser's local storage.
Enable them with [`showQuerySuggestions`](/doc/api-reference/widgets/autocomplete/js/#param-showquerysuggestions) and [`showRecent`](/doc/api-reference/widgets/autocomplete/js/#param-showrecent):

```js JavaScript icon=code theme={"system"}
EXPERIMENTAL_autocomplete({
  container: "#autocomplete",
  placeholder: "Search for products",
  showRecent: true,
  showQuerySuggestions: {
    indexName: "instant_search_demo_query_suggestions",
    getURL: (item) => `/search?q=${item.query}`,
  },
  indices: [
    {
      indexName: "instant_search",
      // ...
    },
  ],
});
```

## Refine results with suggestions

To keep users on the same page, set up a multi-index search experience that uses the selected suggestion to search the main index.

The [`autocomplete`](/doc/api-reference/widgets/autocomplete/js) widget targets the index that contains the suggestions.
The other widgets target the main index that contains your records.
For more information, see [Multi-index search](/doc/guides/building-search-ui/ui-and-ux-patterns/multi-index-search/js).

```js JavaScript icon=code expandable theme={"system"}
// ...

const searchClient = algoliasearch("ALGOLIA_APPLICATION_ID", "ALGOLIA_SEARCH_API_KEY");

const search = instantsearch({
  indexName: "instant_search",
  searchClient,
});

search.addWidgets([
  EXPERIMENTAL_autocomplete({
    container: '#autocomplete',
    showQuerySuggestions: {
      indexName: 'instant_search_demo_query_suggestions',
    },
  }),
  hits({
    container: "#hits",
    templates: {
      // ...
    },
  }),
]);

search.start();
```

## Customize recent searches

Pass an object to `showRecent` to customize how each recent search renders:

```js JavaScript icon=code theme={"system"}
EXPERIMENTAL_autocomplete({
  container: "#autocomplete",
  showRecent: {
    templates: {
      item: ({ item, onSelect }, { html }) =>
        html`<div onClick=${onSelect}>${item.query}</div>`,
    },
  },
  // ...
});
```

## Customize suggestions

Pass an object to `showQuerySuggestions` to customize each suggestion and apply [search parameters](/doc/api-reference/search-api-parameters) to the suggestions query:

```js JavaScript icon=code theme={"system"}
EXPERIMENTAL_autocomplete({
  container: "#autocomplete",
  showQuerySuggestions: {
    indexName: "instant_search_demo_query_suggestions",
    searchParameters: { hitsPerPage: 5 },
    templates: {
      item: ({ item, onSelect }, { html, components }) =>
        html`<div onClick=${onSelect}>
          ${components.ReverseHighlight({ hit: item, attribute: "query" })}
        </div>`,
    },
  },
  // ...
});
```

Use `ReverseHighlight` to emphasize the text that doesn't match the query.
This is a common style for Query Suggestions.

The widget has no built-in category grouping for suggestions and there's no `categoryAttribute`.
To show a category next to a suggestion, render the category in the suggestion's `item` template.

## See also

* [Autocomplete](/doc/guides/building-search-ui/ui-and-ux-patterns/autocomplete/js) guide for the widget basics.
