All catalogs are indexed in Episerver Find, while working on a website that uses the Find index to retrieve entities you sometimes need to re-index a specific catalog to quickly test new features. This can be time consuming because by default all catalogs are being indexed, and you cannot specify a specific catalog or how many items to be indexed out of the box.
This can be done by overwriting the CommerceReIndexInformation
and removing all other implementations of IReindexInformation
from the IOC container.
CommerceReIndexInformation
is responsible for indexing catalogs and returning the ContentReferences
to be indexed.
Below is an example that passes the first 50 ContentReferences
to the indexer of the catalog with the name ‘Test’. Please pay attentention that from the 50 supplied ContentReferences
only a few may be indexed because the ContentReferences
can be of any type; categories, products and or variants. And in the end your CatalogContentClientConventions
determine what get’s indexed and what not.
internal class CatalogConnectCommerceReIndexInformation : CommerceReIndexInformation
{
private readonly CatalogContentClientConventions _catalogContentClientConventions;
private readonly IContentLoader _contentLoader;
private readonly LanguageSelectorFactory _languageSelectorFactory;
[Obsolete("Use the constructor with CatalogContentClientConventions instead. Will remain at least until November 2016.")]
public CatalogConnectCommerceReIndexInformation(ReferenceConverter referenceConverter, IContentLoader contentLoader, LanguageSelectorFactory langugSelectorFactory)
: base(referenceConverter, contentLoader, langugSelectorFactory)
{
}
public CatalogConnectCommerceReIndexInformation(ReferenceConverter referenceConverter, IContentLoader contentLoader, LanguageSelectorFactory langugSelectorFactory, CatalogContentClientConventions catalogContentClientConventions)
: base(referenceConverter, contentLoader, langugSelectorFactory, catalogContentClientConventions)
{
_catalogContentClientConventions = catalogContentClientConventions;
_contentLoader = contentLoader;
_languageSelectorFactory = langugSelectorFactory;
}
/// <summary>
/// Return the targets to index.
/// </summary>
public override IEnumerable ReindexTargets
{
get
{
if (_catalogContentClientConventions.AllowIndexingCatalogContent)
{
foreach (var catalog in GetCatalogs())
{
yield return new ReindexTarget
{
ContentLinks = GetContentToIndex(catalog).Take(50), // Only supply the first 50 content references of the catalog.
Languages = GetLanguagesToIndex(catalog),
SiteDefinition = SiteDefinition.Empty
};
}
}
}
}
protected override IEnumerable GetCatalogs()
{
return _contentLoader.GetChildren(Root, _languageSelectorFactory.AutoDetect(true)).Where(x => x.Name == "Test"); // Only index the supplied catalog, this makes sure only the catalog with the supplied name gets indexed.
}
}
See the code below to register your custom IReindexInformation
implementation and remove all other implementations of IReindexInformation
so you have full control over the indexing process.
[ModuleDependency(typeof(EPiServer.Commerce.Initialization.InitializationModule))]
[InitializableModule]
public class ContentProviderInitialization : IConfigurableModule
{
public void Initialize(InitializationEngine context)
{
}
public void Uninitialize(InitializationEngine context)
{
}
public void ConfigureContainer(ServiceConfigurationContext context)
{
// Enable this to forcefully only index a specific catalog and a number of desired items.
context.StructureMap().Model.EjectAndRemove(typeof(IReindexInformation));
context.StructureMap().Configure(c =>
{
c.For().Use().Singleton();
});
}
}
By using the class above and the scheduled task ‘EPiServer Find Content Indexing Job’ you have more control over the Find indexing process of Episerver.