Wednesday 15 January 2020

Coveo Custom search hub implementation

In a recent project I was working on, this project contain up to 7 microsites, were these microsites are sub trees of the content tree under the main site home item, there was a Coveo for Sitecore cloud 4 implementation, and as requested we wanted to use the same global search results, BUT when a user initiated search from any of these microsites we wanted the search hub to be specific to that search interface, in this post I will walk you through the custom implementation we did.




First of all and for those who don't know what is Search hub, following is the definition quoted from Coveo documentation:  


Search hub is a Search API query parameter whose value is a descriptive name of the search interface from which the query originates, allowing the creation of a query pipeline optimized for a specific search interface. It also makes it possible to create Usage Analytics Reports in which data is filtered or grouped according to this information.

The second thing I want you to is to review the last related blog post I wrote which will prepare you for the context of the issue we are trying to resolve, following is the URL for the prevous blog post: 

Override Global Coveo Search Box


You can edit the value of search hub in Sitecore through the data source of the Coveo Global Searchbox datasource as you can see in the following screenshot:



And if you use the developer tools to inspect the html of the Coveo global search box, you will see where is this value is being set, see the following screenshot:




From the above we got an idea of why we don't update "data-sc-search-hub" attribute using JavaScript based on the context of the sub-site, so we did, we looked for the razor file for the global search-box, you can find it in the following path:

..\Views\Coveo Hive\Search Boxes\Coveo Searchbox.cshtml

Then we used the following script to update the attribute based on the site context:


Now when you publish the file and reload the page you will see that search hub passed into Coveo API is changing, see the following screenshot:


Hope this will help someone! 


No comments:

Post a Comment