Speed up your product imports

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;

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

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s