Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

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.

...

  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. LastmodifiedPublishedDate, DateCreatedReleasedDate

  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:

...

info
Code Block
languagec#
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>();
        }
    }
}

Note

Add your custom fields to Sitefinity Settings

After you have registered the configuration navigate to Advanced settings → DatetimeField section and add :

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

  2. Add the DateTime fields you wish to alter e.

...

  1. g. PublishedDate

Info

Setup search service

In order to

alter

modify all the

type to be properly mapped you need to modify all DateTime fields to have the proper type

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
languagec#
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 Hawksearch

After 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 :

Image Added

...