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.

  1. Navigate to Administration → Search indexes (your-site-domain/Sitefinity/Administration/Search)

  2. Open the index for editing

  3. Under Advanced → Additional fields for indexing add the fields e.g. PublishedDate, ReleasedDate

  4. 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

  1. Create a News item

  2. On the top right side click on the Settings icon and select the Custom Fields

  3. 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

  4. Press Continue and Done

  5. 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 :

  1. Navigate to Administration → Settings → Advanced → DateTimeFields (your-site-domain/Sitefinity/Administration/Settings/Advanced)

  2. 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:
Register custom search service

 

Â