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

Version 1 Current »

To extend the types of the supported properties decorated with [IncludeInHawksearch], you should create a “mapper” class and implement IOutputFieldValueMapper interface:

List<object> Map(object source, PropertyInfo property);
bool CanMap(PropertyInfo property);
  • Map is a method that receives the source object containing the property and that property’s general info, then tries to return a mapped value.

  • CanMap is a method that receives the property’s general info, then decides whether or not this “mapper” is suitable to handle the requested type.

Afterwards, register the mapper as a viable implementation for the IOutputFieldValueMapper service in your DI container. Using this approach, you could index more complex types such as Optimizely blocks.

Example (of the mapper responsible for handling Optimizely’s complex data)

Creation

public class OptiOutputFieldValueMapper : IOutputFieldValueMapper
{
    private static readonly Type[] ValidTypes =
    { 
        typeof(ContentReference), typeof(XhtmlString)
    };

    public virtual List<object> Map(object source, PropertyInfo property)
    {
        var value = property.GetValue(source);

        switch (value)
        {
            case null:
                return new List<object> { string.Empty };

            case ContentReference contentLink:
                return new List<object> { contentLink.ToString() };

            case XhtmlString xhtmlString:
                return new List<object> { Regex.Replace(xhtmlString.ToHtmlString(), "<.*?>", string.Empty) };

            default:
                throw new NotSupportedException($"The decorated property {property.Name} of type " +
                                                $"{property.PropertyType.Name} is not supported.");
        }
    }

    public virtual bool CanMap(PropertyInfo property)
    {
        return ValidTypes.Contains(property.PropertyType);
    }
}

Registration

 services.AddSingleton<IOutputFieldValueMapper, OptiOutputFieldValueMapper>();

  • No labels