Switch Lucene indexes

Last updated Friday, February 26, 2016 in Sitecore Experience Platform for Developer, Administrator

You can set up Lucene to maintain two directories for an index. This addresses the fact that the index directory is reset (deleted) before a full index rebuild. This is only a problem for production environments, so you can reconfigure your custom index using the SwitchOnRebuildLuceneIndex class during testing and before moving to a production environment.

The SwitchOnRebuildLuceneIndex class inherits from the LuceneIndex class. It adds the capability of maintaining two directories for a particular index.

When you initialize a SwitchOnRebuildLuceneIndex object, it verifies the presence of a secondary folder. If the folder does not exist, it creates this folder. It appends _sec to the name of the baseline directory, and uses this as the name of folder it creates.

The index uses the IndexReader.LastModified(Directory) method to choose a primary directory based on the last modified date. It uses the most recent directory as the primary directory.


The primary directory is used for index read and update operations. The secondary directory is a fallback for read operations during a full index rebuild.

The Index Property Store contains information about which directory is primary and which one is secondary.

The SwitchOnRebuildLuceneIndex object writes the following entries to the CrawlingLog object when it initializes:

“Resolving directories from index meta data store”
"Resolving directories by last time modified"
"Primary directory last modified = '…'"
"Secondary directory last modified = '…'"
“ReadUpdateDirectory is set to …”
“FullRebuildDirectory is set to '{0}'”

When the full index rebuild completes, the primary and secondary directories are switched.

To set up Lucene to maintain two directories for an index:

  • Change the type reference on a particular search index to Sitecore.ContentSearch.LuceneProvider.SwitchOnRebuildLuceneIndex:
    <indexes hint="list:AddIndex">
       <index id="content_index"
           <param desc="name">$(id)</param>
           <param desc="folder">$(id)</param>


After you have adjusted the configuration file to make the search index use the SwitchOnRebuildLuceneIndex method, your website uses indexes from the primary directory. Each time you do a full index rebuild, this rebuild take place in the secondary directory. The secondary directory becomes the primary one after the rebuild.