In order to create a new Pipe, you have to create a new class that implements:
IPipe<ICategoryIndexingCommand<T>>, where T : NodeContent
if you want to add a pipe for further processing of catalog categories.
IPipe<IEntryIndexingCommand<T>>, where T : EntryContentBase
if you want to add a pipe for further processing of catalog entries.
IPipe<IPageIndexingCommand<T>> where T : PageData
if you want to add a pipe for further processing of site pages.
You will have to implement 2 properties and a method
int Order { get; } string Name { get; } void Execute(T command);
Order is a property which determines the priority of a pipe execution inside a pipeline.
Name is a property depicting the name of the pipe.
Execute is a method which holds the inner workings of the pipe. Generic parameter T is of type IPipeCommand. This is a base interface which the mentioned
ICategoryIndexingCommand
,IEntryIndexingCommand
andIPageIndexingCommand
inherit from.
Afterwards, you have to register your pipe in order for it to be picked up and executed. Use one of the built-in extension methods:
ExtendCategoryIndexing<>()
container.AddHawksearchCategoryIndexing<NodeContent>() .ExtendCategoryIndexing<NodeContent, CustomCategoryIndexingPipe>();
ExtendProductIndexing<>()
container.AddHawksearchProductIndexing<GenericProduct, GenericVariant>() .ExtendProductIndexing<GenericProduct, EntriesImagePipe<GenericProduct>>() .ExtendProductIndexing<GenericProduct, GenericProductAttributesPipe>();
ExtendVariantIndexing<>()
container.AddHawksearchVariantIndexing<GenericVariant>() .ExtendVariantIndexing<GenericVariant, EntriesImagePipe<GenericVariant>>() .ExtendVariantIndexing<GenericVariant, GenericVariantAttributesPipe>() .ExtendVariantIndexing<GenericVariant, GenericVariantShippingPipe>();
ExtendBundleIndexing<>()
container.AddHawksearchBundleIndexing<GenericBundle, GenericVariant>() .ExtendBundleIndexing<GenericBundle, CustomBundleIndexingPipe>();
ExtendPackageIndexing<>()
container.AddHawksearchPackageIndexing<GenericPackage, GenericVariant>() .ExtendPackageIndexing<GenericPackage, CustomPackageIndexingPipe>();
ExtendPageIndexing<>()
container.AddHawksearchPageIndexing<LocationItemPage>() .ExtendPageIndexing<LocationItemPage, FoundationPageAttributesPipe<LocationItemPage>>() .ExtendPageIndexing<LocationItemPage, LocationItemPageAttributesPipe>(); container.AddHawksearchPageIndexing<StandardPage.StandardPage>() .ExtendPageIndexing<StandardPage.StandardPage, FoundationPageAttributesPipe<StandardPage.StandardPage>>() .ExtendPageIndexing<StandardPage.StandardPage, StandardPageAttributesPipe>();
Examples
New pipe for adding shipping information to a variant
Creation
public class GenericVariantShippingPipe : IPipe<IEntryIndexingCommand<GenericVariant>> { public int Order => 410; public string Name => "Variant Shipping Attributes"; public void Execute(IEntryIndexingCommand<GenericVariant> command) { foreach (var entry in command.Entries) { var productItem = entry.OutputEntry; var variant = entry.InputEntry; productItem.Attributes[nameof(variant.MinQuantity)] = new List<decimal> { variant.MinQuantity ?? decimal.Zero }; productItem.Attributes[nameof(variant.MaxQuantity)] = new List<decimal> { variant.MaxQuantity ?? decimal.Zero }; productItem.Attributes[nameof(variant.Weight)] = new List<double> { variant.Weight }; } } }
Registration
container.AddHawksearchVariantIndexing<GenericVariant>() .ExtendVariantIndexing<GenericVariant, GenericVariantShippingPipe>();
New pipe for adding custom attributes to a certain page type (LocationItemPage from Foundation)
Creation
public class LocationItemPageAttributesPipe : IPipe<IPageIndexingCommand<LocationItemPage>> { public int Order => 360; public string Name => "Location Page Attributes"; private readonly IUrlResolver _urlResolver; public LocationItemPageAttributesPipe(IUrlResolver urlResolver) { _urlResolver = urlResolver; } public void Execute(IPageIndexingCommand<LocationItemPage> command) { foreach (var entry in command.Entries) { var contentItem = entry.OutputEntry; var page = entry.InputEntry; contentItem.Attributes["ImageUrl"] = new List<string> { _urlResolver.GetUrl(page.Image) }; contentItem.Attributes["Description"] = new List<string> { page.MainIntro }; } } }
Registration
container.AddHawksearchPageIndexing<LocationItemPage>() .ExtendPageIndexing<LocationItemPage, LocationItemPageAttributesPipe>();
New pipe for indexing an entry’s images - a general example for all entries
Creation
public class EntriesImagePipe<T> : IPipe<IEntryIndexingCommand<T>> where T : EntryContentBase { public int Order => 350; public string Name => "Images lookup"; private readonly IUrlResolver _urlResolver; private readonly IContentLoader _contentLoader; private readonly ILogger _logger = LogManager.GetLogger(); public EntriesImagePipe(IUrlResolver urlResolver, IContentLoader contentLoader) { _urlResolver = urlResolver; _contentLoader = contentLoader; } public void Execute(IEntryIndexingCommand<T> command) { var entriesWithError = new List<ContentToIndex<T, HawksearchProductItem>>(); foreach (var entry in command.Entries) { if (!TryProcessOne(command.Context, entry)) { entriesWithError.Add(entry); } } if (entriesWithError.Any()) { command.Entries = command.Entries .Except(entriesWithError) .ToList(); } } private bool TryProcessOne(IIndexingContext context, ContentToIndex<T, HawksearchProductItem> entry) { var inputEntry = entry.InputEntry; try { foreach (var commerceMedia in inputEntry.CommerceMediaCollection) { if (_contentLoader.TryGet<ImageMediaData>(commerceMedia.AssetLink, out var imageMediaData)) { entry.OutputEntry.ImageUrl.Add(_urlResolver.GetUrl(imageMediaData)); if (!string.IsNullOrWhiteSpace(imageMediaData.AltText)) { entry.OutputEntry.ImageAlt.Add(imageMediaData.AltText); } break; } } return true; } catch (Exception ex) { string message = $"Error occurred while processing: `{inputEntry.DisplayName}`, {inputEntry.Code}."; context.AddDetailsToReport(message); _logger.Error(message, ex); return false; } } }
Registration
container.AddHawksearchProductIndexing<GenericProduct, GenericVariant>() .ExtendProductIndexing<GenericProduct, EntriesImagePipe<GenericProduct>>(); container.AddHawksearchVariantIndexing<GenericVariant>() .ExtendVariantIndexing<GenericVariant, EntriesImagePipe<GenericVariant>>();