Sending user to result when only one result is found in instantsearch

My employer has a client that is utilizing instantsearch as part of their ecommerce solution. One detail that they’re wanting is if only one result is returned for a query, is it possible to just automatically send the user to that result, instead of leaving them viewing the algoliaHits listing.

Two or more hits would still retain current default behavior. Not seeing anything similar to this type of request at hits | InstantSearch.js | Algolia or similar, but I may be looking at the wrong place.

Any thoughts or ideas on this one?

Hi Michael,

Let me know if this is what you were looking for! Redirects in InstantSearch.js | Algolia

Will review and see what I can make of this. Thanks long.chen

Thank you for the patience here. Looking over that link, it appears that that is more for when we’d be able to anticipate what is getting entered in, which isn’t quite what we are aiming for here.

Perhaps worded a bit differently, is there a way to intercept the incoming results, and then check the amount of hits were returned? If only 1 hit found, tap into perhaps an anonymous function to redirect the user via window.location with the destination being that one result permalink?

I may be managing to get away with enough by way of the transformItems callback as part of the hits widget. I check if the items length is equal to 1, and if yes, set window.location to the saved permalink, else just return the items untouched.

1 Like

That sounds like a great workaround!
User query → Algolia → return [hits] →
if (hits.len ==1) {
window.location.href = hits[0].href