Disable indexing temporarily in Sitecore 7 from code

Disable indexing temporarily in Sitecore 7 from code

Sitecore 7 introduced a lot of new things including the Sitecore.ContentSearch namespace containing new indexing and search APIs. These makes it easier to work with Lucene indexes as you now don’t actually need to know anything about Lucene. Though, as you might have noticed in the change logs for the following updates and later versions, it had a few bugs and missing features when the first version was released – Sitecore 7.0 rev130424 – including a few that I have run into lately.

Disable indexing

One of the missing features is being able to disable index rebuilding temporarily for example when doing a lot of item editing, e.g. a product import or something like that. Whenever you edit an item through your code it triggers an index update for at least sitecore_master_index and any other custom indexes that uses the syncMaster update strategy.

item["Title"] = "New Title";

// This will trigger an index update on
// indexes using the syncMaster update strategy

When updating a lot of items – for example when doing a product import – it can result in bad performance and wasted index updates if you are updating the same item more than once. You might also have some computed fields in your own index that is depending on all items existing, so indexing each item before all items have been imported will just be wasted resources in such a case. That is why the ability to pause indexing temporarily can be useful.

Before Sitecore 7

// This is how you would temporarily disable all 
// index rebuilding before Sitecore 7
Sitecore.Configuration.Settings.Indexing.Enabled = false;

// Do a lot of item updates

// Enable indexing again. Might be a good idea to also rebuild all indexes.
Sitecore.Configuration.Settings.Indexing.Enabled = true;

But the feature simply didn’t make it into the first release of Sitecore 7 and using the old method sadly doesn’t do anything. Here’s a list of a lot of the community wishes that actually made it into Sitecore 7, which also notes that the index pausing didn’t make the cut. Luckily the feature was added in 7.0 Update 1 (rev130810).

Sitecore 7.0 Update 1 (rev130818) and later

// How to pause indexing in Sitecore 7.0 Update-1 (rev130810) and later

// Do a lot of item updates

// Enable indexing again. Might be a good idea to also rebuild all indexes.

So if you have any projects running the initial release of Sitecore 7.0 you will have to update to at least Update 1 to be able to disable indexing temporarily from your code. But it is not such a bad thing, the updates include a lot of other bug fixes and improvements to the ContentSearch API.