Add custom fields to a Solr schema
You can create a custom helper class to add custom fields to a Solr schema.
To add custom fields:
-
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:RequestResponsec#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() { var fieldType = CreateFieldType("custom_text", "solr.TextField", new Dictionary<string, string> { { "positionIncrementGap", "100" }, { "multiValued", "false" }, }); var indexAnalyzer = new XElement("indexAnalyzer"); indexAnalyzer.Add(new XElement("tokenizer", new XElement("class", "solr.StandardTokenizerFactory"))); indexAnalyzer.Add(new XElement("filters", new XElement("class", "solr.StopFilterFactory"), new XElement("ignoreCase", "true"), new XElement("words", "stopwords.txt"))); indexAnalyzer.Add(new XElement("filters", new XElement("class", "solr.LowerCaseFilterFactory"))); fieldType.Add(indexAnalyzer); var queryAnalyzer = new XElement("queryAnalyzer"); queryAnalyzer.Add(new XElement("tokenizer", new XElement("class", "solr.StandardTokenizerFactory"))); queryAnalyzer.Add(new XElement("filters", new XElement("class", "solr.StopFilterFactory"), new XElement("ignoreCase", "true"), new XElement("words", "stopwords.txt"))); queryAnalyzer.Add(new XElement("filters", new XElement("class", "solr.SynonymFilterFactory"), new XElement("synonyms", "synonyms.txt"), new XElement("ignoreCase", "true"), new XElement("expand", "true"))); queryAnalyzer.Add(new XElement("filters", new XElement("class", "solr.LowerCaseFilterFactory"))); fieldType.Add(queryAnalyzer); yield return fieldType; } } }
-
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.
-
-
Create a
PopulateHelper
factory class that inherits fromIPopulateHelperFactory
, implements theGetPopulateHelper
method, and returns your custom class:RequestResponsec#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); } } }
-
Create a configuration patch file that overrides the Sitecore class factory with the one you created:
RequestResponsexml<?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>