2

I tried to index master, web indexes using the Azure Search service. Indexing works fine, but we have a requirement to have the index names unique in Azure as we are using single search services for different instances like (local, QA). Hence we would like name indexes with unique prefix names. Something like say for master index,

When we trigger index from QA instance --> QA_sitecore_master_index

When we trigger index from Local instance --> Local_sitecore_master_index

In one of the blogs it was mentioned, prior to Sitecore 9.1, we will not be able to handle this requirement using patch files.

For 9.0 they have asked to create a custom cloud search provider index, which did not work in my case.

Is there any recommended way of doing it for Sitecore 8.2 Update 7?

0

I was able to get some code working to do this on Sitecore 9.1 as per my blog, however I haven't investigated any other Sitecore versions.

General approach is to override the ICloudSearchProviderIndexName with your implementation and then patch in this implementation via config.

public class CloudSearchProviderIndex : Sitecore.ContentSearch.Azure.CloudSearchProviderIndex
{
    public CloudSearchProviderIndex(string name, string connectionStringName, string totalParallelServices, IIndexPropertyStore propertyStore)
      : base(name, connectionStringName, totalParallelServices, propertyStore, null, new ServiceCollectionClient(), new PrefixCloudSearchProviderIndexName(new FactoryWrapper()))
    {
    }
}
public class PrefixCloudSearchProviderIndexName : ICloudSearchProviderIndexName
{
    private readonly IFactoryWrapper factoryWrapper;
    private readonly string _prefix = Sitecore.Configuration.Settings.GetSetting("ContentSearch.Azure.IndexNamePrefix");

    public PrefixCloudSearchProviderIndexName(IFactoryWrapper wrapper)
    {
        factoryWrapper = wrapper;
    }

    public void Initialize(string connectionStringName) => Sitecore.Diagnostics.Log.Info($"Initializing PrefixCloudSearchProviderIndexName {_prefix}", this);

    public string GetIndexName(string sitecoreIndexName, CloudSearchProviderIndexName.OperationalType type)
    {
        var prefixName = $"{_prefix}-{sitecoreIndexName.Replace('_', '-')}";
        Sitecore.Diagnostics.Log.Info($"PrefixCloudSearchProviderIndexName {prefixName}", this);
        return prefixName;
    }

    public void SetIndexName(string sitecoreIndexName, CloudSearchProviderIndexName.OperationalType type, string serviceIndexName)
    {
        // No action needed
    }

    public string GenerateIndexName(string originalName) => string.Format("{0}-{1}", originalName, new Random().Next(1000)).Replace('_', '-');
}

Config:

<sitecore role:require="Standalone or ContentManagement or ContentDelivery" search:require="Azure">
<settings>
  <setting name="ContentSearch.Azure.SwitchOnRebuild" value="false"/>
  <setting name="ContentSearch.Azure.IndexNamePrefix" value="local"/>
</settings>
<contentSearch>
  <configuration>
    <indexes hint="list:AddIndex">
      <index id="sitecore_core_index">
        <patch:attribute name="type" value="MyCustomAssembly.CloudSearchProviderIndex, MyCustomAssembly"/>
      </index>
      <index id="sitecore_master_index">
        <patch:attribute name="type" value="MyCustomAssembly.CloudSearchProviderIndex, MyCustomAssembly"/>
      </index>
      <index id="sitecore_web_index">
        <patch:attribute name="type" value="MyCustomAssembly.CloudSearchProviderIndex, MyCustomAssembly"/>
        <strategies hint="list:AddStrategy" patch:instead="strategies">
          <strategy role:require="Standalone OR ContentManagement" ref="contentSearch/indexConfigurations/indexUpdateStrategies/onPublishEndAsync"  />
          <strategy role:require="ContentDelivery" ref="contentSearch/indexConfigurations/indexUpdateStrategies/manual"/>
        </strategies>
      </index>
      <index id="sitecore_marketing_asset_index_master">
        <patch:attribute name="type" value="MyCustomAssembly.CloudSearchProviderIndex, MyCustomAssembly"/>
      </index>
      <index id="sitecore_marketing_asset_index_web">
        <patch:attribute name="type" value="MyCustomAssembly.CloudSearchProviderIndex, MyCustomAssembly"/>
        <strategies hint="list:AddStrategy" patch:instead="strategies">
          <strategy role:require="Standalone OR ContentManagement" ref="contentSearch/indexConfigurations/indexUpdateStrategies/onPublishEndAsyncSingleInstance"  />
          <strategy role:require="ContentDelivery" ref="contentSearch/indexConfigurations/indexUpdateStrategies/manual"/>
        </strategies>
      </index>
      <index id="sitecore_marketingdefinitions_master">
        <patch:attribute name="type" value="MyCustomAssembly.CloudSearchProviderIndex, MyCustomAssembly"/>
      </index>
      <index id="sitecore_marketingdefinitions_web">
        <patch:attribute name="type" value="MyCustomAssembly.CloudSearchProviderIndex, MyCustomAssembly"/>
        <strategies hint="list:AddStrategy" patch:instead="strategies">
          <strategy role:require="Standalone OR ContentManagement" ref="contentSearch/indexConfigurations/indexUpdateStrategies/onPublishEndAsyncSingleInstance"  />
          <strategy role:require="ContentDelivery" ref="contentSearch/indexConfigurations/indexUpdateStrategies/manual"/>
        </strategies>
      </index>
      <index id="sitecore_fxm_master_index">
        <patch:attribute name="type" value="MyCustomAssembly.CloudSearchProviderIndex, MyCustomAssembly"/>
      </index>
      <index id="sitecore_fxm_web_index">
        <patch:attribute name="type" value="MyCustomAssembly.CloudSearchProviderIndex, MyCustomAssembly"/>
        <strategies hint="list:AddStrategy" patch:instead="strategies">
          <strategy role:require="Standalone OR ContentManagement" ref="contentSearch/indexConfigurations/indexUpdateStrategies/onPublishEndAsyncSingleInstance"  />
          <strategy role:require="ContentDelivery" ref="contentSearch/indexConfigurations/indexUpdateStrategies/manual"/>
        </strategies>
      </index>
      <!-- Optional - if you are using SXA -->
      <index id="sitecore_sxa_master_index">
        <patch:attribute name="type" value="MyCustomAssembly.CloudSearchProviderIndex, MyCustomAssembly"/>
      </index>
      <index id="sitecore_sxa_web_index">
        <patch:attribute name="type" value="MyCustomAssembly.CloudSearchProviderIndex, MyCustomAssembly"/>
        <strategies hint="list:AddStrategy" patch:instead="strategies">
          <strategy role:require="Standalone OR ContentManagement" ref="contentSearch/indexConfigurations/indexUpdateStrategies/onPublishEndAsync"  />
          <strategy role:require="ContentDelivery" ref="contentSearch/indexConfigurations/indexUpdateStrategies/manual"/>
        </strategies>
      </index>
    </indexes>
  </configuration>
</contentSearch>

|improve this answer|||||
  • Thank you for modifying it, will try it. But I got a confirmation from Sitecore, saying that, they do not recommend using single search services for multiple instances. – karthik Feb 13 at 10:44
0

Mark's approach works well for Sitecore 9.1, but not for earlier versions of Sitecore where the ICloudSearchProviderIndexName interface has not been introduced yet.

This blog post (also referenced in Mark's blog post) describes a different solution for Sitecore 9.0.x, based on the implementation of a custom CloudSearchProviderIndex class where the index name gets modified in its constructor. This solution with small changes works with Sitecore 8.2.7 as well.

This is the code of the modified custom CloudSearchProviderIndexName class:

public class CustomCloudSearchProviderIndex : CloudSearchProviderIndex
{
    private readonly string _prefix = Sitecore.Configuration.Settings.GetSetting("ContentSearch.Azure.IndexNamePrefix");

    public CustomCloudSearchProviderIndex(string name, string connectionStringName, string totalParallelServices, IIndexPropertyStore propertyStore)
        : base(name, connectionStringName, totalParallelServices, propertyStore)
    {
        PrefixIndexesNames();
    }

    [Obsolete]
    public CustomCloudSearchProviderIndex(string name, string connectionStringName, string totalParallelServices, IIndexPropertyStore propertyStore, string group)
        : base(name, connectionStringName, totalParallelServices, propertyStore, group)
    {
        PrefixIndexesNames();
    }

    private void PrefixIndexesNames()
    {
        try
        {
            if (!string.IsNullOrEmpty(_prefix))
            {
                PropertyStore.Set("cloud_search_primary_index_name", $"{_prefix}-{CloudIndexName}");
                PropertyStore.Set("cloud_search_secondary_index_name", $"{_prefix}-{CloudIndexName}-secondary");
                string primary = PropertyStore.Get("cloud_search_primary_index_name");
                string secondary = PropertyStore.Get("cloud_search_secondary_index_name");
            }
        }
        catch (System.Exception ex)
        {
            //some code
        }
    }
}

Note that the second obsolete constructor is needed because it is used by the sitecore_analytics_index index.

You can apply the implemented custom class type using the following patch config file (similar to Mark's one, but without roles because they have been introduced later in Sitecore):

<configuration xmlns:patch="http://www.sitecore.net/xmlconfig/">
  <sitecore>
    <settings>
      <setting name="ContentSearch.Azure.IndexNamePrefix" value="local"/>
    </settings>
    <contentSearch>
       <configuration type="Sitecore.ContentSearch.ContentSearchConfiguration, Sitecore.ContentSearch">
        <indexes hint="list:AddIndex">
            <index id="sitecore_master_index">
                <patch:attribute name="type">Custom.CustomCloudSearchProviderIndex,Custom</patch:attribute>
            </index>
            <index id="sitecore_web_index">
                <patch:attribute name="type">Custom.CustomCloudSearchProviderIndex,Custom</patch:attribute>
            </index>
            <index id="sitecore_core_index">
                <patch:attribute name="type">Custom.CustomCloudSearchProviderIndex,Custom</patch:attribute>
            </index>
            <index id="sitecore_analytics_index">
                <patch:attribute name="type">Custom.CustomCloudSearchProviderIndex,Custom</patch:attribute>
            </index>
            <index id="sitecore_marketingdefinitions_master">
                <patch:attribute name="type">Custom.CustomCloudSearchProviderIndex,Custom</patch:attribute>
            </index>
            <index id="sitecore_marketingdefinitions_web">
                <patch:attribute name="type">Custom.CustomCloudSearchProviderIndex,Custom</patch:attribute>
            </index>
            <index id="sitecore_marketing_asset_index_master">
                <patch:attribute name="type">Custom.CustomCloudSearchProviderIndex,Custom</patch:attribute>
            </index>
            <index id="sitecore_marketing_asset_index_web">
                <patch:attribute name="type">Custom.CustomCloudSearchProviderIndex,Custom</patch:attribute>
            </index>
            <index id="sitecore_testing_index">
                <patch:attribute name="type">Custom.CustomCloudSearchProviderIndex,Custom</patch:attribute>
            </index>
            <index id="sitecore_fxm_master_index">
                <patch:attribute name="type">Custom.CustomCloudSearchProviderIndex,Custom</patch:attribute>
            </index>
            <index id="sitecore_fxm_web_index">
                <patch:attribute name="type">Custom.CustomCloudSearchProviderIndex,Custom</patch:attribute>
            </index>
            <index id="sitecore_list_index">
                <patch:attribute name="type">Custom.CustomCloudSearchProviderIndex,Custom</patch:attribute>
            </index>
            <index id="social_messages_master">
                <patch:attribute name="type">Custom.CustomCloudSearchProviderIndex,Custom</patch:attribute>
            </index>
            <index id="social_messages_web">
                <patch:attribute name="type">Custom.CustomCloudSearchProviderIndex,Custom</patch:attribute>
            </index>
            <index id="sitecore_suggested_test_index">
                <patch:attribute name="type">Custom.CustomCloudSearchProviderIndex,Custom</patch:attribute>
            </index>
        </indexes>
        </configuration>
    </contentSearch>
  </sitecore>
</configuration>

I tested this solution on Sitecore 8.2.7 using Azure Cognitive Search service and it worked successfully for me.

|improve this answer|||||
-1

Instead of renaming, you should be able to create separate azure indexes for separate environments.

e.g. sitecore-master-{siteName}-{envName}-index

While fetching the search results, siteName and envName come from Sitecore configuration settings to ensure correct index is being referred in azure functions.

|improve this answer|||||
  • I believe in sitecore 8.2 index names are stored in DB as well. It may not be possible to rename in configs alone. – karthik Feb 11 at 14:54
  • This works perfectly in 9.2 without storing any index names in DB. I m only wondering why wud the index names be stored in DB in 8.2. – Ghanendra Singh Feb 12 at 3:35
  • This answer is not providing any details on how to rename the indexes – Mark Gibbons Feb 14 at 9:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.