Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to temporarily disable sitecore indexing while editing items

I am developing a Sitecore project that has several data import jobs running on daily basis. Every time a job is executed, it may update a large amount of Sitecore items (thousands) and I've noticed that all these editings trigger Solr index updates.

My concern is, I don't really sure if this is better or update everything at the end of the job is. So, I would love to try both options. Could anyone tell me how can I use code to temporarily disable Lucene/Solr indexing and enable it later when I finish editing all items?

like image 571
Harry Ninh Avatar asked Jan 07 '23 22:01

Harry Ninh


2 Answers

This is a common requirement, and you're right to have such concerns. In general it's considered good practice to disable indexing during big import jobs, then rebuild afterwards.

Assuming you're using Sitecore 7 or above, this is pretty much what you need:

IndexCustodian.PauseIndexing();
IndexCustodian.ResumeIndexing();

Here's a comprehensive article discussing this:

http://blog.krusen.dk/disable-indexing-temporarily-in-sitecore-7/

like image 89
Martin Davies Avatar answered May 12 '23 06:05

Martin Davies


In addition to @Martin answer, you can pass (silent=true) when you finish the editing of the item, Something like:

item.Editing.BeginEdit();
//Change fields values
item.Editing.EndEdit(true,true);

The second parameter in EndEdit() method force a silent update of the item, which means no Events/Indexing will be triggered on item save.

I feel this is safer than pausing indexing on the whole application level during import process, you just skip indexing of the items you are updating.

EDIT:

In case you need to rebuild the index for the updated items after the import process is done, you can use the following code, It will index the content tree starting from RootItemInTree and below:

var index = Sitecore.ContentSearch.ContentSearchManager.GetIndex("Your_Index_Name")
index.Refresh(new SitecoreIndexableItem(RootItemInTree));
like image 35
Ahmed Okour Avatar answered May 12 '23 07:05

Ahmed Okour