2

I am adding custom fields in solr schema in Sitecore 10.3. But it will be for specific index.

How can i get index name in the class which is inherited from IPopulateHelperFactory refer this link to add custom code

1 Answer 1

2

Index name is not passed OOTB to SchemaPopulateHelper class.

However there is a workaround using Sitecore Switcher class.

  • First create a class that inherits from mentioned above Switcher class with PopulateManagedSchemaArgs type. This class does not need any method. It should be like:
public class PopulateManagedSchemaArgsSwitcher 
    : Switcher<Sitecore.ContentSearch.SolrProvider.Pipelines.PopulateSolrSchema.PopulateManagedSchemaArgs>
{
}
  • Next create 2 classes implementing PopulateManagedSchemaProcessor, one that will enter the switcher with current PopulateManagedSchemaArgs, and one that will exit the switcher:
public class EnterPopulateManagedSchemaArgsSwitcher : PopulateManagedSchemaProcessor
{
    public override void Process(PopulateManagedSchemaArgs args)
    {
        PopulateManagedSchemaArgsSwitcher.Enter(args);
    }
}

public class ExitPopulateManagedSchemaArgsSwitcher : PopulateManagedSchemaProcessor
{
    public override void Process(PopulateManagedSchemaArgs args)
    {
        PopulateManagedSchemaArgsSwitcher.Exit();
    }
}
  • Then create your CustomPopulateHelper class that will override default SchemaPopulateHelper class, and in the GetAllFields method it will read index and core names from the switcher:
public class CustomPopulateHelper : SchemaPopulateHelper
{
    public CustomPopulateHelper(SolrSchema solrSchema) : base(solrSchema)
    {
    }

    public override IEnumerable<XElement> GetAllFields()
    {
        var args = PopulateManagedSchemaArgsSwitcher.CurrentValue;
        var indexName = args?.Index?.Name;
        var cores = args?.Cores?.ToList();

        var baseFields = base.GetAllFields().ToList();

        if (indexName == "my_custom_index")
        {
            baseFields.Add(CreateField("my_custom_field", "text_general", false, true, false, false, false, false, false, false));
        }

        return baseFields;
    }
}

From what I can see, there is always single core in args.Cores and that core name is equal to args.Index.Name. I'm sure there may be specific case when it will be different, but I'm not sure when and why.

  • Now it's time for factory, that will create your CustomPopulateHelper instead of default one:
public class CustomPopulateHelperFactory : IPopulateHelperFactory
{
    public ISchemaPopulateHelper GetPopulateHelper(SolrSchema solrSchema)
    {
        return new CustomPopulateHelper(solrSchema);
    }
}

And the last thing is config file which binds everything together:

<?xml version="1.0" encoding="utf-8" ?>
<configuration xmlns:patch="http://www.sitecore.net/xmlconfig/" xmlns:role="http://www.sitecore.net/xmlconfig/role/" xmlns:search="http://www.sitecore.net/xmlconfig/search/">
  <sitecore role:require="Standalone or ContentManagement or ContentDelivery" search:require="solr">
    <pipelines>
      <contentSearch.PopulateSolrSchema>
        <processor type="Sitecore.ContentSearch.SolrProvider.Pipelines.PopulateSolrSchema.ResolveCoreNames, Sitecore.ContentSearch.SolrProvider" />
          <processor 
            type="MyAssembly.MyNamespace.EnterPopulateManagedSchemaArgsSwitcher, MyAssembly"
            patch:before="processor[@type='Sitecore.ContentSearch.SolrProvider.Pipelines.PopulateSolrSchema.PopulateFields, Sitecore.ContentSearch.SolrProvider']">
          </processor>
        <processor type="Sitecore.ContentSearch.SolrProvider.Pipelines.PopulateSolrSchema.PopulateFields, Sitecore.ContentSearch.SolrProvider">
            <param 
                type="MyAssembly.MyNamespace.CustomPopulateHelperFactory, MyAssembly" 
                patch:instead="*[@type='Sitecore.ContentSearch.SolrProvider.Factories.DefaultPopulateHelperFactory']"/>
        </processor>
        <processor
            type="MyAssembly.MyNamespace.ExitPopulateManagedSchemaArgsSwitcher, MyAssembly"
            patch:after="processor[@type='Sitecore.ContentSearch.SolrProvider.Pipelines.PopulateSolrSchema.PopulateFields, Sitecore.ContentSearch.SolrProvider']">
        </processor>
      </contentSearch.PopulateSolrSchema>
    </pipelines>
  </sitecore>
</configuration>
  • And that's it, your custom field will be added to the managed-schema of the index:

enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.