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 a Condition of Query Is algolia
and a Consequence of Return Custom Data. To see how this works in the live environment simply type the query "algolia".
{
"redirect": "https://www.algolia.com/doc/"
}
It looks for the redirect user data using getAlgoliaResults.transformResponse()
and sets its value in the context.During onStateChange()
or onSubmit()
. The difference between the two being that onStateChange()
will trigger the redirect as soon as the user types the last character in the query Condition, while onSubmit() will only trigger the redirect when the user hits enter with the full query condition in the search box.