Azure Switch On Rebuild Bug – Index name not always updating on CD Servers

azure-searchI have recently been assisting a client with moving their Sitecore 8.2 update 7 Site to Azure. As part of this we decided to use Azure search for our custom search indexes – available in 8.2 u7 and 9.0.2 and later . We ran into an issue when we enabled Switch On Rebuild as we found that on rebuild the index would sometimes return no results. This resulted in missing data on the pages powered by these custom indexes.

TL;DR

This is a bug with Azure Switch On Rebuild, you can request SC Hotfix-294596 from Sitecore Support.

Why did this happen?

I didn’t previously know this until I started investigating this further and asked on SSE but if you have Switch On Rebuild enabled then on rebuild of the index Sitecore creates a new index with a -secondary suffix and switches to that once the index has been rebuilt. It stores a reference to the Primary index name (the one in use) in the properties table of the Core database.

e.g Sitecore should change the name from ‘my-custom-index’ to ‘ my-custom-index-secondary’ and back again each time it is rebuilt.

Sitecore uses the index Id from your config along with the server name and url of the site to create a key in the core database to store the Primary and Secondary index names like so (sensitive information blurred):

core-db-primary-index

From further testing we were seeing that the value of the Primary index in the Core database is only updated on the CA server and not the CD Servers so after some further investigation and head-scratching I implemented a short term work-around with an custom admin page to allow us to manually update the index names in the with code similar to below:

var database = Sitecore.Configuration.Factory.GetDatabase(“core”);

var indexName = “my-custom-index”‘;
SwitchPrimaryIndex(indexName, “cd1-server”, true);

Following this I raised this issue with Sitecore Support for investigation.

Resolving the Issue Properly

After some further investigation Sitecore Support confirmed this is an bug with Sitecore XP 8.2 rev. 180406 (Update-7) – bug report #254072 and provide a hotfix: SC Hotfix-294596.

This is also an issue in 9.0.2 and likely any version below 9.1, this is resolved in Sitecore 9.1 Initial release.

The only file included in the hotfix is an updated version of the Sitecore.ContentSearch.Azure.dll, there have been a number of changes here.

The Hotfix addresses this issue by changing how the Primary and Secondary index names are stored. Instead of being stored in the properties table of the Core database these values are now stored in their own Azure Index called index-catalog. Here is a screenshot of how this looks in the Azure Portal:

index-catalog-new

In addition to this the Hotfix also renames the index using an random number instead of -secondary.  If you also have this issue you can request the Hotfix from support.

Thanks for Gert Gullentops for his help with this on SSE and Slack. Hopefully this will help others too.