Add related data to the Hawksearch index
In this article you will find:
Goal
This article provides information on how to push related data into a document for indexing.
Prerequisite
Configure Connector - Configure Hawksearch
Setup related data
Open the backend of your Sitefinity instance
Open the content → news page (your-site-domain/Sitefinity/adminapp/content/newsitems)
Click the gears icon (settings) in the upper right corner
Open the custom fields page and add a new field
5. Press Continue, then Done and save the changes
6. Open Content Types → Events and create an event
7. Add the newly created event to a news item
Field Setup
To create the related data field in Hawksearch, you must first add it to the 'Additional fields for indexing' section found within the 'Search Index Properties' settings for the index in the Sitefinity Administration. This step is essential for the field to be recognized and utilized within the Hawksearch system.
Setup search service
In order to add the related data to the index you need to create a custom search service which inherits the HawkseachService class and overrides the CreateIndex and UpdateIndex methods. Please refer to the code snippet below.
Â
using System;
using System.Collections.Generic;
using System.Linq;
using Hawksearch.Search;
using Telerik.Sitefinity.Services.Search.Data;
using Telerik.Sitefinity.Data.ContentLinks;
using Telerik.Sitefinity.Modules.Events;
using Telerik.Sitefinity.Services.Search.Model;
using Telerik.Sitefinity.Services.Search.Publishing;
using Telerik.Sitefinity.Services.Search;
namespace SitefinityWebApp
{
public class CustomSearchService : HawksearchService
{
private const string NewsItemContentType = "Telerik.Sitefinity.News.Model.NewsItem";
public override void CreateIndex(string sitefinityIndexName, IEnumerable<IFieldDefinition> fieldDefinitions)
{
var definitions = fieldDefinitions.ToList();
var parentTitleFieldDefinition = Telerik.Sitefinity.Abstractions.ObjectFactory.Resolve<IFieldDefinition>();
parentTitleFieldDefinition.Name = "event";
parentTitleFieldDefinition.Type = typeof(string);
definitions.Add(parentTitleFieldDefinition);
base.CreateIndex(sitefinityIndexName, definitions);
}
public override void UpdateIndex(string name, IEnumerable<IDocument> documents)
{
var documentList = new List<IDocument>(documents);
var document = documents.FirstOrDefault();
if (document != null)
{
var contentTypeField = document.Fields.FirstOrDefault(f => f.Name == "ContentType");
if (contentTypeField != null)
{
var contentType = contentTypeField.Value.ToString();
if (string.Equals(contentType, NewsItemContentType, StringComparison.InvariantCultureIgnoreCase))
{
var eventsManager = EventsManager.GetManager();
var contentLinksManager = ContentLinksManager.GetManager();
var newsItemsContentLinks = contentLinksManager.GetContentLinks().Where(cl => cl.ParentItemType == NewsItemContentType).ToList();
var eventIds = newsItemsContentLinks.Select(cl => cl.ChildItemId).ToList();
var events = eventsManager.GetEvents().Where(e => eventIds.Contains(e.Id));
documentList = new List<IDocument>();
foreach (var doc in documents)
{
var fields = new List<IField>();
fields.AddRange(doc.Fields);
var documentIdField = doc.Fields.FirstOrDefault(f => f.Name == "OriginalItemId");
if (documentIdField != null)
{
var id = Guid.Parse(documentIdField.Value.ToString());
var newsArticleContentLink = newsItemsContentLinks.FirstOrDefault(n => n.ParentItemId == id);
if (newsArticleContentLink != null)
{
var relatedEvent = events.FirstOrDefault(e => e.Id == newsArticleContentLink.ChildItemId);
if (relatedEvent != null)
{
var relatedEventField = new Field
{
Name = "event",
Value = relatedEvent.Title
};
fields.Add(relatedEventField);
}
}
}
var modifiedDocument = new Document(fields, document.IdentityField.Name);
documentList.Add(modifiedDocument);
}
}
}
base.UpdateIndex(name, documentList);
}
}
}
}
Register Custom Search Service
In order to use your custom search service instead of the built-in one you need to register it in the backend.