Skip to main content

Sitecore

How to Connect to a Sitecore-Managed Azure Search Index with Azure Search API

Ai Featured Image 1600x600

Creating a search context to perform your search queries should be easy right? With Sitecore’s Content Search API, it is as easy as passing in the literal name of the index as a parameter into a ContentSearchManager method. Connecting to a Sitecore-Managed Azure Search Index to perform queries using Azure Search API adds some complexity to this, though. Why? When you perform an index rebuild from the Sitecore Control Panel, Azure Search will reindex in a rebuild index. Then it will swap the rebuilt index and the currently active index in order to make the rebuilt index the newly active index.

That’s a mouthful, so I will provide an example. Suppose your index name is “custom-web-index” and Azure Search states that “custom-web-index” is the currently active index being used in your CD environment. When you perform an index rebuild, Azure Search may do so in a rebuild index called “custom-web-index-135”. Once the rebuild is complete, Azure Search will set “custom-web-index-135” as the currently active index to be used in your CD environment. When you try to access your index using the Azure Search API, there is no way for you to guess the number that will be appended to the name of your index (“custom-web-index-xxx”).

The Code

Below is a code snippet showing how to connect to either “custom_web_index” or “custom_master_index” depending on the context in which the search index is being requested.

public ISearchIndexClient GetSitecoreSearchIndexClient()
{
     var connectionStringDictionary = ConnectionStringParser.Parse(Sitecore.Configuration.Settings.GetConnectionString("cloud.search"));
     var searchServiceUri = new Uri(connectionStringDictionary["serviceUrl"]);
     var searchServiceName = searchServiceUri.Host.Split('.').First();
     var apiKey = connectionStringDictionary["apiKey"];
     return new SearchIndexClient(searchServiceName, GetIndexName(), new SearchCredentials(apiKey));
}

private string GetIndexName()
{
     // use reflection to get the current search index name from sitecore
     var searchCloudIndexNamePropertyInfo =
        typeof(CloudSearchProviderIndex).GetProperty(SearchCloudIndexNamePropertyName,
        BindingFlags.Instance | BindingFlags.NonPublic);

     return searchCloudIndexNamePropertyInfo
        .GetValue((CloudSearchProviderIndex)RetrieveSearchContext().Index).ToString();
}

public IProviderSearchContext RetrieveSearchContext()
{
     return ContentSearchManager.GetIndex($"custom_{Context.Database.Name}_index").CreateSearchContext();
}

Conclusion

To see which index is the currently active index in your Azure Search Service, you can view the index-catalog in the Azure Portal. It will list which index is active at that point in time. Sitecore’s Content Search API saves you the pain of connecting to a search index in Azure Search. However, if you need to use the Azure Search API to perform your queries, you will need the code above to be able to connect to your Sitecore-Managed Azure Search Index.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Zach Gay, Senior Technical Consultant, Perficient's Sitecore practice

More from this Author

Categories
Follow Us
TwitterLinkedinFacebookYoutubeInstagram