Insights

Speed up your product imports

Senior Developer
Valtech Netherlands

December 12, 2018

When you use Episerver Commerce, your products will often be imported from an external source. When you use Episerver Find and there are a lot of changes, the import process might take a while and a lot of requests to Episerver Find will be made.

So usually you would turn of the Episerver Find events when starting the import like so:

EventedIndexingSettings.Instance.EventedIndexingEnabled = false;
EventedIndexingSettings.Instance.ScheduledPageQueueEnabled = false;

 

The problem with this is that your Find index will not be updated and you would need to re-index your site after the import has completed.

You can use the search client to update your index though after all products have been imported.

You keep track of objects that have been changed, added or deleted in an IEnumerable and tell the search client to index or remove them

private static void AddToIndex(IEnumerable itemsToAdd, IClient searchClient)

{

    searchClient.Index(objectsToIndex: itemsToAdd);

}




private static void RemoveFromIndex(IEnumerable itemsToRemove, IClient searchClient)

{

    RemoveFromIndex(itemsToRemove.Select(p => p.ContentLink), searchClient: searchClient);

}




private static void RemoveFromIndex(IEnumerable itemsToRemove, IClient searchClient)

{

    FilterBuilder deleteFilterBuilder =

            new FilterBuilder(client: searchClient);




    deleteFilterBuilder = itemsToRemove.Aggregate(

            seed: deleteFilterBuilder,

            func: (current, contentLink) =&gt; current.Or(x =&gt;   <span data-mce-type="bookmark" id="mce_SELREST_start" data-mce-style="overflow:hidden;line-height:0" style="overflow:hidden;line-height:0;">&#65279;</span>x.ContentLink.Match(contentLink)));




    searchClient.Delete(filter: deleteFilterBuilder);

}

 

Depending on the amount of products you might need to use batches for the add and delete.

After you have done this you need to enable the events again.

You can find the complete sample here.

 

Original article posted on EpiServer Deleopment: Speed up your product imports 

 

Meet The Challenges Of Today's Digital Economy

Ready to take that first step and rise to your digital potential? Contact Valtech today.
Talk to Us