Add custom fields to a Solr schema

Abstract

This topic describes how you use a custom helper class to add fields to a Solr schema.

You can create a custom helper class to add custom fields to a Solr schema.

To add custom fields:

  1. Create a class that inherits SchemaPopulateHelper, and use this class to define the fields and the field types you want to add to the Solr schema:

    using System.Collections.Generic;
    using System.Linq;
    using System.Xml.Linq;
    using Sitecore.ContentSearch.SolrProvider.Pipelines.PopulateSolrSchema;
    using SolrNet.Schema;
    
    namespace Documentation.Examples
    {
        public class CustomPopulateHelper : SchemaPopulateHelper
        {
            public CustomPopulateHelper(SolrSchema schema) : base(schema)
            {
    
            }
            
            public override IEnumerable<XElement> GetAllFields()
            {
                return base.GetAllFields().Union(GetAddCustomFields());
            }
    
            public override IEnumerable<XElement> GetAllFieldTypes()
            {
                return base.GetAllFieldTypes().Union(GetAddCustomFieldTypes());
            }
    
            private IEnumerable<XElement> GetAddCustomFields()
            {            
                yield return CreateField("*_t_pl", 
                    "text_general", 
                    isDynamic: true, 
                    required: false, 
                    indexed: true, 
                    stored: true, 
                    multiValued: false, 
                    omitNorms: false, 
                    termOffsets: false, 
                    termPositions: false, 
                    termVectors: false);
           }
    
            private IEnumerable<XElement> GetAddCustomFieldTypes()
            {
                yield return CreateFieldType("randomType", "solr.RandomSortField",
                    new Dictionary<string, string>
                    {
                        { "indexed", "true" }
                    });
            }
        }
    }
    • Override the GetAllFields method to add custom fields. It is important that you call the base method and combine the results with the results of the custom method.

    • Override the GetAllFieldTypes method to add custom field types. It is important that you call the base method and combine the results with the results of the custom method.

    • Use the CreateField method to add new field and attributes. The Solr documentation has descriptions of all parameters, except for these two:

      Parameter

      Type

      Description

      Default value

      defaultName

      string

      Specify a default value for the field

      null

      isDynamic

      bool

      Specify whether the field is a dynamic field

      false

    • Use the CreateFieldType method to add new field type and attributes. The Solr documementation has descriptions of all parameters.

  2. Create a PopulateHelper factory class that inherits from IPopulateHelperFactory, implements the GetPopulateHelper method, and returns your custom class:

    using Sitecore.ContentSearch.SolrProvider.Abstractions;
    using Sitecore.ContentSearch.SolrProvider.Pipelines.PopulateSolrSchema;
    using SolrNet.Schema;
    
    namespace Documentation.Examples
    {
        public class CustomPopulateHelperFactory : IPopulateHelperFactory
        {
            public ISchemaPopulateHelper GetPopulateHelper(SolrSchema solrSchema)
            {
                return new CustomPopulateHelper(solrSchema);
            }
        }
    }
    
  3. Create a configuration patch file that overrides the Sitecore class factory with the one you created:

    <?xml version="1.0" encoding="utf-8" ?>
    <configuration xmlns:patch="http://www.sitecore.net/xmlconfig/">
      <sitecore>
        <pipelines>
          <contentSearch.PopulateSolrSchema>
            <processor type="Sitecore.ContentSearch.SolrProvider.Pipelines.PopulateSolrSchema.PopulateFields, Sitecore.ContentSearch.SolrProvider">
           <param type="Documentation.Examples.CustomPopulateHelperFactory, Documentation.Examples" patch:instead="*[@type='Sitecore.ContentSearch.SolrProvider.Factories.DefaultPopulateHelperFactory']"/>
         </processor>
          </contentSearch.PopulateSolrSchema>
        </pipelines>
      </sitecore>
    </configuration>