Customizing field indexing

All the data types marked with [SearchableType] attribute are processed by an internal class DataTypeDocumentSource, however one can extend how certain fields are processed to. For example: change the way the facet fields values are extracted, or replacing reference values with searchable text. To do that one can register an implementation of the IDataFieldProcessorProvider interface in a startup handler.

In the following example we will customize search indexing for the Composite.Community.Blog package.

The startup handler:

    [ApplicationStartup]
    public class Startup
    {
        public static void ConfigureServices(IServiceCollection collection)
        {
            collection.AddSingleton<IDataFieldProcessorProvider>(new BlogDataFieldProcessorProvider());
        }
        public static void OnBeforeInitialize() {}
        public static void OnInitialized(){}
    }

Implementation of IDataFieldProcessorProvider that would return custom processors for the “Tags” and “Teaser” fields:

internal class BlogDataFieldProcessorProvider: IDataFieldProcessorProvider
{
  public IDataFieldProcessor GetDataFieldProcessor(PropertyInfo dataTypeProperty)
  {
	  if (dataTypeProperty.DeclaringType.FullName == "Composite.Community.Blog.Entries")
	  {
		  if (dataTypeProperty.Name == "Tags")
		  {
			  return new TagsDataFieldProcessor();
		  }
		  if (dataTypeProperty.Name == "Teaser")
		  {
			  return new TeaserDataFieldProcessor();
		  }
	  }
	  return null;
  }
}

For the “Tags” field, we will split the value into multiple values and mark that the facet is multivalued:

internal class TagsDataFieldProcessor : DefaultDataFieldProcessor
{
	public override string[] GetFacetValues(object value)
	{
		var str = (string) value;
		return str.Split(',');
	}
	public override DocumentFieldFacet GetDocumentFieldFacet(PropertyInfo propertyInfo)
	{
		var result = base.GetDocumentFieldFacet(propertyInfo);
		result.FacetType = FacetType.MultipleValues;
		return result;
	}
}

For the “Teaser” field, we want the text to appear in the already existing “Description” column of the search results:

internal class TeaserDataFieldProcessor : DefaultDataFieldProcessor
{
	public override string GetDocumentFieldName(PropertyInfo pi)
	{
		return DocumentFieldNames.Description;
	}
}