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 -

Registered custom search service -

Sitefinity 13.3.7600 or higher

Set Site-Specific Settings

  1. Open Administration → Settings → Advanced (your-site-domain/Sitefinity/Administration/Settings/Advanced

  2. Open the Hawksearch tab.

  3. Copy the HawkSearchApiKey property path

     

  4. Copy the HawkSearchClientId property path

     

  5. Open the SiteSettings → SiteSpecificProperties tab in Advanced Settings

  6. Click Create new.

  7. In Path, enter the copied value.

  8. Save your changes.

  9. Restart your application.

  10. Go back to the Hawksearch tab.

  11. Select the desired Site that you want to have site-specific configurations.

  12. Click the Break Inheritance button

  13. Add values for the exposed fields

  14. Click Save Changes.

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; } } }

Â