Skip to content

Instantly share code, notes, and snippets.

@fluxdigital
Created May 14, 2019 21:00
Show Gist options
  • Save fluxdigital/3e207c282ddaaf0b63665f654cf4d0be to your computer and use it in GitHub Desktop.
Save fluxdigital/3e207c282ddaaf0b63665f654cf4d0be to your computer and use it in GitHub Desktop.
Provides Functions to allow fix azure index bug with index rebuild
protected void SwitchPrimaryIndex(string indexName, string machineName, bool isCd)
{
//get key
var indexKey = GetIndexKey(indexName, machineName, isCd);
//get current primary key name
var currentPrimaryIndexName = database.Properties[indexKey];
//default to this as new index name
var newPrimaryIndexName = currentPrimaryIndexName;
//get the new index name by adding/removing 'secondary'
newPrimaryIndexName = ToggleIndexName(currentPrimaryIndexName, newPrimaryIndexName);
//update the index name to the new one
Database.Properties[indexKey] = newPrimaryIndexName;
}
protected string GetIndexKey(string indexName, string machineName, bool isCd)
{
var iisSiteName = System.Web.Hosting.HostingEnvironment.ApplicationHost.GetSiteName();
if (isCd)
{
iisSiteName = iisSiteName.ToLower().Replace("master", "");
}
return string.Format("{0}_{1}-{2}_CLOUD_SEARCH_PRIMARY_INDEX_NAME", indexName, machineName, iisSiteName).ToUpper();
}
protected string ToggleIndexName(string currentPrimaryIndexName, string newDdbPrimaryIndexName)
{
if (currentPrimaryIndexName.ToLower().Contains("secondary"))
{
newDdbPrimaryIndexName = newDdbPrimaryIndexName.ToLower().Replace("-secondary", "");
}
else
{
newDdbPrimaryIndexName = string.Format("{0}-secondary", newDdbPrimaryIndexName);
}
return newDdbPrimaryIndexName;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment