Switch Solr indexes Using SwitchOnRebuildSolrSearchIndex

This Blog Represents the steps to Switch the Solr indexes on Rebuilding the core using the SwitchOnRebuildSolrSearchIndex class which inherits from the SolrSearchIndex class .

This is very helpful in the production environments when we will have 2 cores in which one acts as a primary which will show the content on the front end , and secondary one for rebuilding the index from sitecore and when the secondary index is rebuilded the SwitchOnRebuildSolrSearchIndex class itself switches the indexes .

Note :- The switching of indexes will only happen on the full rebuild of the index.

Now let`s see how it works.

1. From the solr server copy the core testconnect_web_index in my case and give the name testconnect_web_index_sec , this testconnect_web_index_sec will act as a secondary core which will be used for rebuilding the indexes.

2. Go to the testconnect_web_index_sec/core.properties file and set the name=testconnect_web_index_sec .

3. Restart the solr Service.

4. In your configuration File , change the class SolrSearchIndex To SwitchOnRebuildSolrSearchIndex like this ,

<index id="sitecore_web_index" type="Sitecore.ContentSearch.SolrProvider.SwitchOnRebuildSolrSearchIndex, Sitecore.ContentSearch.SolrProvider"> 

and also add a parameter 
<param desc="rebuildcore">testconnect_web_index_sec</param> 

so now your config will look like ,

<?xml version="1.0" encoding="utf-8"?>
<configuration xmlns:patch="http://www.sitecore.net/xmlconfig/" xmlns:role="http://www.sitecore.net/xmlconfig/role/" xmlns:search="http://www.sitecore.net/xmlconfig/search/">
  <sitecore role:require="Standalone or ContentDelivery or ContentManagement" search:require="solr">
    <contentSearch>
      <configuration type="Sitecore.ContentSearch.ContentSearchConfiguration, Sitecore.ContentSearch">
        <indexes hint="list:AddIndex">
          <index id="sitecore_web_index" type="Sitecore.ContentSearch.SolrProvider.SwitchOnRebuildSolrSearchIndex, Sitecore.ContentSearch.SolrProvider">
            <param desc="name">$(id)</param>
            <param desc="core">testconnect_web_index</param>
<param desc="rebuildcore">testconnect_web_index_sec</param>
            <param desc="propertyStore" ref="contentSearch/indexConfigurations/databasePropertyStore" param1="$(id)" />
            <configuration ref="contentSearch/indexConfigurations/defaultSolrIndexConfiguration" />
            <strategies hint="list:AddStrategy">
              <strategy ref="contentSearch/indexConfigurations/indexUpdateStrategies/onPublishEndAsyncSingleInstance" />
            </strategies>
            <locations hint="list:AddCrawler">
              <crawler type="Sitecore.ContentSearch.SitecoreItemCrawler, Sitecore.ContentSearch">
                <Database>web</Database>
                <Root>/sitecore</Root>
              </crawler>
            </locations>
            <enableItemLanguageFallback>false</enableItemLanguageFallback>
            <enableFieldLanguageFallback>false</enableFieldLanguageFallback>
          </index>
        </indexes>
      </configuration>
    </contentSearch>
  </sitecore>
</configuration>

5. Check both of them in the solr by running the url
https://localhost:8721/solr/testconnect_web_index/select?q=*:*
https://localhost:8721/solr/testconnect_web_index_sec/select?q=*:*

Initially both will return the same no of document in the results.

6. So now in your content tree suppose you created a few items and published it into the web DB , Then only the testconnect_web_index will get updated and not the testconnect_web_index_sec .
But during the full rebuild of the index , only testconnect_web_index_sec  will get updated and after the full rebuild the switching of indexes takes place .

You can verify that from the Corename/Core.properties file , and you can see the swaping of the name attribute between both the indexes . The testconnect_web_index will have the name=testconnect_web_index_sec and vice-versa.
During the Full Rebuild of the Index only the testconnect_web_index_sec will be affected and will have 0 document when you check the result from https://localhost:8721/solr/testconnect_web_index_sec/select?q=*:* during the rebuild of the indexes.

Comments