Gravatar for vikrant.punwatkar@perficient.com

Question by vikrantpunwatkar, Dec 9, 2016 10:00 AM

Redirect to detail page rather search result page

Hi,

I am using OOTB renderings from Coveo for Sitecore 80 3.0 (1084). In one of the requirement, I need to redirect to detail page if searched term exactly matches to one of the field else show search result page. Searched term = 'X', Indexed field= 'Y' if ("X' exactly matches with 'Y'.value) then redirect to Y' else Show search result page.

Being new, I am not sure about the appropriate way of implementation. Can anyone suggest for this customization?

Regards, Vikrant

Gravatar for flguillemette@coveo.com

Comment by François Lachance-Guillemette, Dec 9, 2016 11:46 AM

Are you trying to have this behavior on a Coveo Searchbox rendering or on a Search Interface rendering?

Gravatar for vikrant.punwatkar@perficient.com

Comment by vikrantpunwatkar, Dec 12, 2016 4:27 AM

I was thinking of the one that would take lesser time as this is urgent. But I would prefer this change on Coveo Searchbox rendering.

Thanks, Vikrant

1 Reply
Gravatar for flguillemette@coveo.com

Answer by François Lachance-Guillemette, Dec 12, 2016 10:02 AM

If I understand correctly, if the searched term exactly matches a field in any of the documents, you need to redirect the user to it?

I think you could hook yourself to an event from the JavaScript Search Framework. I was curious and decided to test it myself.

I found out the beforeRedirect event that you can hook to cancel it and instead implement your own logic. Here is an example that redirects to the result if it is the only one with a title matching the current text in the searchbox.

searchBoxElement.on(Coveo.StandaloneSearchInterfaceEvents.beforeRedirect, function(e, data) {
    // Cancel the current redirection
    data.cancel = true;

    var queryBuilder = new Coveo.QueryBuilder();

    var currentQuery = Coveo.state(searchBoxElement[0], "q");

    // Add the expression to the query matching the title.
    queryBuilder.advancedExpression.add('<%= Model.ToCoveoFieldName("title") %>=="' + currentQuery + '"');

    // Execute the query and wait for the results.
    Coveo.SearchEndpoint.endpoints["default"].search(queryBuilder.build()).then(function(results) {
        if (results.totalCount == 1) {
            window.location.href = results.results[0].ClickUri;
        } else {
            var standaloneSearchInterface = Coveo.get(searchBoxElement[0], Coveo.SearchInterface);
            standaloneSearchInterface.redirectToSearchPage(data.searchPageUri);
        }
    });
});

Change the conditions in the queryBuilder and you're good to go!

Hope this helps :)

FLG

Gravatar for vikrant.punwatkar@perficient.com

Comment by vikrantpunwatkar, Dec 14, 2016 7:15 AM

Thanks a lot FLG. Just went through your code and will surely try this. Do you have any samples of customization. How should I consume this code so that it does not get lost when I upgrade my Coveo module?

Thanks

Gravatar for flguillemette@coveo.com

Comment by François Lachance-Guillemette, Dec 14, 2016 7:48 AM

Our recommended way to customize component is to duplicate the component and apply your customizations in it.

This way, when updating, you won't lose your custom changes. However, this requires you to implement new features from the default component. They are usually in the upgrade guide, so no worries about that.

Gravatar for vikrant.punwatkar@perficient.com

Comment by vikrantpunwatkar, Dec 22, 2016 6:06 AM

While trying this solution, I observed that beforeRedirect is not firing. My lib version is 0.9.1146.26 and as per below link, this event is not available in my version. https://answers.coveo.com/questions/4311/beforeredirect-is-not-firing Can you please help me for an alternative?

Thank you.

Gravatar for flguillemette@coveo.com

Comment by François Lachance-Guillemette, Dec 22, 2016 9:22 AM

I'm afraid I don't see other options. Since you can't stop the page from redirecting when the user enters a query, there is not much we can do to redirect a query directly in the search box.

Maybe you could try other alternatives to achieve similar behavior. With the OmniboxResultList component, you could show results based on the current expression typed in the search box and the field. Then the user could click on that result and you could redirect directly to the page you need.

You could also trigger a query and add those result to the Omnibox using PopulateOmniboxObject. Results that match the field could be returned as results.

Gravatar for vikrant.punwatkar@perficient.com

Comment by vikrantpunwatkar, Dec 27, 2016 4:16 AM

I am using OmniBox component and it is populating result list for the mentioned field. But I need to add a feature to redirect if searched term exactly matches a field value, keeping all things as it is.

Can this be achieved on any of the events on result page?

Gravatar for vikrant.punwatkar@perficient.com

Comment by vikrantpunwatkar, Jan 18, 2017 10:14 AM

Hi, Do you have anything to share?

Gravatar for flguillemette@coveo.com

Comment by François Lachance-Guillemette, Jan 18, 2017 10:40 AM

Can this be achieved on any of the events on result page?

No, since it can only be easily achieved with beforeRedirect.

At this point, you could try to implement a custom "keyup" event on the searchbox, see if the user pressed "enter" and stop the event from firing to implement your own implementation. You can see how it is implemented in the more recent versions in the coveo-search-ui repository. You will need to experiment yourself as this is a very specific use case that requires custom code since we don't offer anything in this version for this.

Ask a question