Skip to end of metadata
Go to start of metadata

You are viewing an old version of this page. View the current version.

Compare with Current View Page History

« Previous Version 2 Next »

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. Lastmodified, DateCreated

  4. Save the 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>();
        }
    }
}

After you have registered the configuration navigate to Advanced settings → DatetimeField section and add the DateTime fields you wish to alter.

Create custom search service

In order to alter the type to be properly mapped you need to modify all DateTime fields to have the proper type. Please refer to the code 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

  • No labels