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.
...
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. LastmodifiedPublishedDate, DateCreatedReleasedDate
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:
...
Code Block | ||
---|---|---|
| ||
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>(); } } } |
Infonote |
---|
Add your custom fields to Sitefinity SettingsAfter you have registered the configuration navigate to Advanced settings → DatetimeField section and add :
|
...
|
Info |
---|
Setup search serviceIn 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 |
. |
Code Block | ||
---|---|---|
| ||
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); } } } } } } |
Note |
---|
Once you implement the code in Visual Studio , build your solution and you will also have to reindex the index you are using from Administrator → Search Indexes → Action → Reindex |
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
Info |
---|
Check field type in HawksearchAfter you have indexed please open your Hawksearch dashboard and check the type of the field you have created. You can find them under Workbench → Data Configuration → Fields It should look like this : |