---
title: "Switch Lucene indexes"
slug: "switch-lucene-indexes"
abstract: "Describes how to switch Lucene indexes after rebuilding."
---
# Switch Lucene indexes

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](https://doc.sitecore.com/xp/en/developers/91/platform-administration-and-architecture/the-index-property-store.html "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`:

    ```xml
    <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.