Use different Hawksearch engines in Sitefinity multisite
In this article you will find:
Goal
With the release of Sitefinity 13.3 and site-specific settings, you can use different engines for different sites in a multi-site environment.
This article will provide information and a sample with which you can achieve this.
In this example, the engines will differ only for their HawkSearchApiKey and HawkSearchClientId, and we will set site-specific values for these two properties.
Prerequisite
Installed Connector - Installing the Connector
Registered custom search service - Register custom search service
Sitefinity 13.3.7600 or higher
Set Site-Specific Settings
Open Administration → Settings → Advanced (your-site-domain/Sitefinity/Administration/Settings/Advanced
Open the Hawksearch tab.
Copy the HawkSearchApiKey property path
Â
Copy the HawkSearchClientId property path
Â
Open the SiteSettings → SiteSpecificProperties tab in Advanced Settings
Click Create new.
In Path, enter the copied value.
Save your changes.
Restart your application.
Go back to the Hawksearch tab.
Select the desired Site that you want to have site-specific configurations.
Click the Break Inheritance button
Add values for the exposed fields
Click Save Changes.
Link to official site-specific settings documentation - https://www.progress.com/documentation/sitefinity-cms/site-specific-settings
Extend the Hawksearch Service
In order to use the site-specific settings we need to override InitializeClient method of the Hawksearch service and use the  site-specific API. As we do not have access to the Context in RemoveDocuments method we should override it and get the current site by the index name.
Â
using System.Collections.Generic;
using System.Linq;
using Hawksearch.Configuration;
using Hawksearch.SDK;
using Hawksearch.Search;
using Telerik.Sitefinity.Configuration;
using Telerik.Sitefinity.Multisite;
using Telerik.Sitefinity.Publishing;
using Telerik.Sitefinity.Publishing.Configuration;
using Telerik.Sitefinity.Publishing.Model;
using Telerik.Sitefinity.Services;
using Telerik.Sitefinity.Services.Search.Data;
namespace SitefinityWebApp.Custom
{
public class CustomSearchService : HawksearchService
{
private ISite site;
public override void DeleteIndex(string name)
{
this.site = SystemManager.CurrentContext.CurrentSite;
base.DeleteIndex(name);
}
public override void RemoveDocuments(string indexName, IEnumerable<IDocument> documents)
{
this.site = this.GetSiteFromIndex(indexName);
base.RemoveDocuments(indexName, documents);
}
protected override IHawksearchClient InitializeClient()
{
using (new SiteRegion(this.site))
{
this.HawkConfig = Config.Get<HawkSearchConfig>();
var enableIndexTraceLog = this.HawkConfig.EnableIndexTraceLog;
var enableSearchTraceLog = this.HawkConfig.EnableSearchTraceLog;
var client = new HawksearchClient(this.HawkConfig.HawkSearchBaseAPI,
this.HawkConfig.HawkSearchIndex,
this.HawkConfig.HierarchyUrl,
this.HawkConfig.HawkSearchSearchingAPI,
this.HawkConfig.AutocompleteUrl,
this.HawkConfig.HawkSearchClientId,
this.HawkConfig.HawkSearchApiKey,
enableIndexTraceLog,
enableSearchTraceLog,
this.RequestLog,
this.ResponseLog);
return client;
}
}
private ISite GetSiteFromIndex(string indexName)
{
var manager = PublishingManager.GetManager(PublishingConfig.SearchProviderName);
var query = manager.GetPipeSettings<SearchIndexPipeSettings>();
var pipeSettings = query.Where(p => p.IsActive == true && p.IsInbound == false).ToList();
var sites = SystemManager.CurrentContext.GetSites();
var pointLinks = manager.Provider.GetSiteItemLinks().ToList();
foreach (var site in sites)
{
var links = pointLinks.Where(l => l.SiteId == site.Id).Select(l => l.ItemId).ToList();
var indexPipeSettings = pipeSettings.FirstOrDefault(p => (p.PublishingPoint.IsSharedWithAllSites || links.Contains(p.PublishingPoint.Id)) && p.CatalogName == indexName);
if (indexPipeSettings != null)
{
return site;
}
}
return SystemManager.CurrentContext.CurrentSite;
}
}
}
Â