You can trigger a redirect from AutoComplete using the Redirect URL plugin. The Redirect URL plugin lets you redirect your Autocomplete results to a specified URL. For guidance on how to use this plugin please see our guide on Redirecting searches to a URL.
Using the redirect plugin is the recommend way to setup redirects with AutoComplete however you can also use the transformResponse to trigger redirects which we will cover below.
Here's a CodeSandBox example of how you can trigger a redirect directly in AutoComplete. A redirect rule was created for this index with:
- Condition: Query is
algolia - Consequence: Return Custom Data
{
"redirect": "https://www.algolia.com/doc/"
}
To see how this works in the live environment simply type the query "algolia".
It retrieves the redirect user data using getAlgoliaResults.transformResponse() and sets its value in the context. This can happen during either onStateChange() or onSubmit(). The key difference between the two is that onStateChange() triggers the redirect as soon as the user types the last character that satisfies the query condition, whereas onSubmit() only triggers the redirect when the user hits Enter with the complete query condition entered in the search box.