Sitecore 9.1: How to prefix or rename Azure Search indexes
Sitecore’s Solr provider has long had an out of the box way to support renaming Solr cores allowing reuse of a Solr server instance, but nothing similar exists for Azure Search.
Why would I want to rename or prefix an index?
I can think of a few reasons:
- Cost saving — For non-production environments, you may want to share a single Azure Search service between multiple environments to save on cost. Azure Search is one of the most expensive components in a Sitecore environment, so minimizing instances would have a big impact.
- Flexibility — Developers may want to test out their changes to indexes without having to impact any other environments that may be using a shared Azure Search instance.
- Time saving — It takes time to provision new instances of Azure Search, especially if you don’t hold the keys to the Azure subscription. If you have an existing instance you can use, then it will save time to simply reuse the instance and add a new environment to it.
- Blue/green deployments — This is pretty much a combination of the previous points, but it would be nice for staging slots to be able to share the Azure Search instance but have their own dedicated indexes while staging slots are active.
Implementations for previous versions of Sitecore
I’ll first note that this implementation is for Sitecore 9.1.0 only as the APIs have changed quite a lot since Sitecore 9.0.x. If you’re looking for an implementation that works in Sitecore 9.0.x then check out this article.
What’s changed in Sitecore 9.1.0?
In previous versions the current index name was stored in the database Properties table. This was changed in Sitecore 9.1.0 (and there is also a patch available from Support for 9.0.2) to use an index catalog in the Azure Search instance itself. This was due to issues with the database based implementation not picking up index name changes while the Azure Search Switch on Rebuild is enabled.
Ok let’s get to it…
This solution does require custom code.
- First, choose a project in your solution to put the code in. If you’re using Helix I’d suggest a Foundation project as once you’ve configured this correctly it’s unlikely to change until you upgrade to a newer version of Sitecore.
- Add the following code to your project
3. Add the following config and modify as necessary for any custom indexes you have
After deploying the above and kicking off a rebuild of sitecore-master-index I can see this in my instance:
Further info
I haven’t fully tested that this works in all situations and as you may have noticed I’ve disabled the index switch on rebuild functionality as I don’t need it for scenarios where I would need to be prefixing index names.
Note that I’ve included changing the web index strategies. This isn’t necessary for this example, but I’m including it as I think it’s an important thing to configure.
I wouldn’t recommend using this code in Production as if you do have any issues then Sitecore Support will be well within their rights to refuse to support it.
I think that this solution will probably not work in future versions of Sitecore 9.x as I believe that this area of Sitecore’s code is in flux at time of writing. However I’ll endeavor to update this post once new versions of Sitecore are released.