Extending Data Indexing in Hawksearch V4
In this article you will find:
Goal
This article provides information on how to extend the indexing logic for Hawksearch V4
Prerequisite
Installed Connector - Installing the Connector
Â
Make sure you have created at least one content item (e.g. News)
Open Administration → Search indexes and create a new index
CustomTestIndex
Open it and make sure that News items or the content item you have created is selected
Create a page and add the
Hawksearch box
andHawksearch results
widgetsSelect from the widget designer of both widgets to search in your
CustomTestIndex
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.
Please refer to this documentation - Register custom search service
Â
Setup search service
In order to add external data to the index you need to create a custom search service class which inherits the HawkseachService class and overrides the CreateIndex and UpdateIndex methods. Please refer to the code snippet below.
Add External Data to the Index
Add additional Fields to the index
using Hawksearch.Search;
using System.Collections.Generic;
using System.Linq;
using Telerik.Sitefinity.Services.Search;
namespace SitefinityWebApp.Search
{
public class CustomSearchService : HawksearchService
{
public override void CreateIndex(string sitefinityIndexName, IEnumerable<IFieldDefinition> fieldDefinitions)
{
var newFieldDefinition = Telerik.Sitefinity.Abstractions.ObjectFactory.Resolve<IFieldDefinition>();
newFieldDefinition.Name = "your-custom-field-name";
newFieldDefinition.Type = typeof(string);
var definitions = fieldDefinitions.ToList();
definitions.Add(newFieldDefinition);
base.CreateIndex(sitefinityIndexName, definitions);
}
}
}
2. Add additional Fields to search Document
using Hawksearch.Search;
using System.Collections.Generic;
using Telerik.Sitefinity.Services.Search.Data;
using Telerik.Sitefinity.Services.Search.Model;
using Telerik.Sitefinity.Services.Search.Publishing;
namespace SitefinityWebApp.Search
{
public class CustomSearchService : HawksearchService
{
public override void UpdateIndex(string name, IEnumerable<IDocument> documents)
{
var documentList = new List<IDocument>();
foreach (var document in documents)
{
var fields = new List<IField>();
fields.AddRange(document.Fields);
var newField = new Field
{
Name = "your-custom-field-name",
Value = "your-custom-field-value"
};
fields.Add(newField);
var modifiedDocument = new Document(fields, document.IdentityField.Name);
documentList.Add(modifiedDocument);
}
base.UpdateIndex(name, documentList);
}
}
}
Â