Sitecore Item Boosting based on complex logic

Sitecore has a number of very useful, built-in, ways of boosting an item during indexing. You can boost items individually, set global rules or item specific rules. Most of the out of the box capabilities are well covered in the documentation available from Sitecore. I’ll show how to add a custom pipeline processor to go beyond what is offered by default.

One thing that is not mentioned explicitly in the documentation is that you can boost items based on field values, though with a few limitations. Some use cases for field value based logic are:

  • Apply boosting based on a field value
    • Its important to remember that the field values that will be compared are the raw field values. For example, a checked checkbox would have a value of “1”.
  • Apply boosting based on an empty field value
  • Apply boosting based on the presence of a field type

If those default options are not enough you essentially have two choices, either create custom rules, or create a custom processor in the indexing.resolveItemBoost pipeline. There are plenty of resources for building custom rules, including the Rules Engine Cookbook, which is still relevant as of Sitecore 9.1. Instead of a custom rule, a custom processor in the indexing.resolveItemBoost pipeline might be a better choice depending on your use case. The implementation of the custom processor will look like the following:

<configuration xmlns:patch="http://www.sitecore.net/xmlconfig/">
    <sitecore>
        <pipelines>
            <indexing.resolveItemBoost>
                <processor type="MyAssembly.Pipelines.ResolveBoost.ResolveItemBoost.ResolveDemoBoostFactor, MyAssembly"/>
            </indexing.resolveItemBoost>
        </pipelines>
        <settings>
            <setting name="MyAssembly.Pipelines.DemoBoostFactor" value="1.1"/>
        </settings>
    </sitecore>
</configuration>
using System;
using Sitecore.ContentSearch;
using Sitecore.ContentSearch.Pipelines.ResolveBoost.ResolveItemBoost;
using Sitecore.Data.Items;
using Sitecore.Diagnostics;

namespace MyAssembly.Pipelines.ResolveBoost.ResolveItemBoost
{
	public class ResolveDemoBoostFactor : BaseResolveItemBoostPipelineProcessor
	{
		public override void Process(ResolveItemBoostArgs args)
		{
			Assert.ArgumentNotNull(args, nameof(args));
			Assert.ArgumentNotNull(args.Indexable, "indexable");
			if (!(args.Indexable is SitecoreIndexableItem indexable))
				return;
			args.ResolvedBoost += ResolveItemBoost(indexable);
		}

		protected virtual float ResolveItemBoost(IIndexable indexable)
		{
			Assert.ArgumentNotNull(indexable, nameof(indexable));
			var item = (Item)(indexable as SitecoreIndexableItem);
			
			if (item != null)
			{
				//add your logic to determine whether or not the item should be boosted.
				var @double =
					Sitecore.Configuration.Settings.GetDoubleSetting("MyAssembly.Pipelines.DemoBoostFactor", 0);

				var @float = Convert.ToSingle(@double);

				return @float;
			}

			return 0.0f;
		}
	}
}

A few notes on the processor:

  • The args.ResolvedBoost property is a float. Sitecore.Configuration.Settings does not have a method for GetFloatSetting, which is why a conversion is required.
  • The baseline of args.ResolvedBoost is 0, all boost values that are resolved are combined in to this property. This includes the results of the other processors in the pipeline.
  • Depending on the complexity of the logic, this may add significant indexing time.

Happy searching!

Leave a Reply

Your email address will not be published. Required fields are marked *