# Switch Lucene indexes

Abstract

Describes how to switch Lucene indexes after rebuilding.

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 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"
“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"
type="Sitecore.ContentSearch.LuceneProvider.SwitchOnRebuildLuceneIndex,
Sitecore.ContentSearch.LuceneProvider">
<param desc="name">$(id)</param> <param desc="folder">$(id)</param>
...


### Note

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.