How to index DateTime fields
Goal
The aim of this article is to demonstrate how to index DateTime fields so that their type is properly parsed passed to the Hawksearch API.
Prerequisite
Configured Connector - Configure Hawksearch
Â
Overview
This documentation explains how to index fields of type DateTime in Hawksearch. The Hawksearch DateTime format differs from Sitefinity's default DateTime format, so when the field is created in Hawksearch it’s type is String. The solution below explains how to alter the DateTime type so that it is properly mapped in Hawksearch.
Add fields for indexing
In order for these fields to be indexed you first need to add them to index.
Navigate to Administration → Search indexes (your-site-domain/Sitefinity/Administration/Search)
Open the index for editing
Under Advanced → Additional fields for indexing add the fields e.g. PublishedDate, ReleasedDate
Save the changes
Add the fields to a Content item
In order for these fields to be indexed you also need to add them as a custom fields to a e.g. News item
Create a News item
On the top right side click on the Settings icon and select the Custom Fields
Add a custom field of type Date and Time and with the name you have added in the Advanced settings of the Index e.g. PublishedDate
Press Continue and Done
Save Changes
Â
Create configuration
In order to alter the type of particular fields we need to store them in a configuration and then use that configuration in a custom search service. Below you will find the ready to use configuration:
Â
using System.Configuration;
using Telerik.Sitefinity.Configuration;
using Telerik.Sitefinity.Localization;
namespace SitefinityWebApp
{
[ObjectInfo(Title = "Datetime fields Title", Description = "Datetime fields Description")]
public class DatetimeFieldsConfig : ConfigSection
{
[ConfigurationProperty("DatetimeFields")]
public ConfigElementDictionary<string, DatetimeField> DatetimeFields
{
get { return (ConfigElementDictionary<string, DatetimeField>)this["DatetimeFields"]; }
}
}
public class DatetimeField : ConfigElement
{
public DatetimeField(ConfigElement parent) : base(parent)
{
}
[ConfigurationProperty("Name", IsRequired = true, IsKey = true)]
[ObjectInfo(Title = "Name", Name = "Name", Description = "Field name.")]
public string Name
{
get { return (string)this["Name"]; }
set { this["Name"] = value; }
}
}
}
Â
Register configuration
In order for your configuration to appear in the Advanced settings of your Sitefinity instance you need to register it at application start in your Global.asax file. Please refer to the code below:
Â
using System;
using SitefinityWebApp;
using Telerik.Sitefinity.Abstractions;
using Telerik.Sitefinity.Configuration;
namespace Hawksearch122
{
public class Global : System.Web.HttpApplication
{
protected void Application_Start(object sender, EventArgs e)
{
Bootstrapper.Bootstrapped += Bootstrapper_Bootstrapped;
}
private void Bootstrapper_Bootstrapped(object sender, EventArgs e)
{
Config.RegisterSection<DatetimeFieldsConfig>();
}
}
}
Â
Add your custom fields to Sitefinity Settings
After you have registered the configuration :
Navigate to Administration → Settings → Advanced → DateTimeFields (your-site-domain/Sitefinity/Administration/Settings/Advanced)
Add the DateTime fields you wish to alter e.g. PublishedDate
Â
Setup search service
In order to modify all the DateTime fields to the index you need to create a custom search service which inherits the HawkseachService class and overrides the CreateIndex , which also implement the ModifyDatetimeFields method . Please refer to the code snippet below.
Â
using System;
using System.Collections.Generic;
using System.Linq;
using Hawksearch.Search;
using Telerik.Sitefinity.Configuration;
using Telerik.Sitefinity.Services.Search;
namespace SitefinityWebApp
{
public class CustomSearchService : HawksearchService
{
public override void CreateIndex(string sitefinityIndexName, IEnumerable<IFieldDefinition> fieldDefinitions)
{
var definitionList = new List<IFieldDefinition>(fieldDefinitions);
this.ModifyDatetimeFields(definitionList);
base.CreateIndex(sitefinityIndexName, definitionList);
}
private void ModifyDatetimeFields(List<IFieldDefinition> definitionList)
{
var configManager = ConfigManager.GetManager();
var section = configManager.GetSection<DatetimeFieldsConfig>();
if (section != null)
{
var dateTimeFields = section.DatetimeFields.Keys;
foreach (var field in dateTimeFields)
{
var dateTimeField = definitionList.FirstOrDefault(f => f.Name == field);
if (dateTimeField != null)
{
definitionList.Remove(dateTimeField);
var dateTimeFieldDefinition = Telerik.Sitefinity.Abstractions.ObjectFactory.Resolve<IFieldDefinition>();
dateTimeFieldDefinition.Name = dateTimeField.Name;
dateTimeFieldDefinition.Type = typeof(DateTime);
definitionList.Add(dateTimeFieldDefinition);
}
}
}
}
}
}
Register custom search service
In order to use you custom search service instead of the built-in one please refer to this documentation:
https://bridgeline.atlassian.net/wiki/spaces/CON/pages/3468466897
Â
Â