Update solrconfig.xml and/or managed-schema on the fly in your running SolrCloud instance on Kubernetes

Friends, Sitecorians, world citizens, lend me your ears. What excellent stuff Microsoft has to offer. Did you guys watch the Microsoft Build 2022? If not, check out the videos on youtube

NGINX is coming to Azure.This is great.
https://www.nginx.com/blog/introducing-f5-nginx-for-azure-load-balancing-available-natively-as-saas-offering-on-microsoft-azure/

DAPR, DAPR, DAPR. The sidecar pattern, and it’s not only for Kubernetes. Meaning you can take advantage of the Actor model pattern in your solutions using the DAPR .Net SDK https://docs.microsoft.com/en-us/azure/aks/dapr-overview
https://docs.dapr.io/developing-applications/building-blocks/actors/actors-overview/
https://docs.microsoft.com/en-us/dotnet/architecture/dapr-for-net-developers/service-invocation

Microsoft Dev Box, now this is really interesting. See it as Code Space running on windows. I see a lot of possibilities here, especially for us Sitecore devs 😉
https://techcommunity.microsoft.com/t5/azure-developer-community-blog/introducing-microsoft-dev-box/ba-p/3412063

WASI, wasi, wasi – WebAssembly on the Server. Steve Sanderson, the father of Blazor. As always mind-boggling ideas, check out his latest video:


And so much more…

Anyway, let’s proceed with today’s post 🙂 I want to share how you can update your solrconfig.xml or managed-schema in your running SolrCloud instance. In this example, we are running SolrCloud in AKS (So no SearchStax here 😉 ). I have an earlier post on how to set up SolrCloud in AKS – Setup SolrCloud for Sitecore 10 in Kubernetes. There is a section where we upload the configurations – Configsets as zip files in the post. Now, that is the approach we will use in our example 🙂

As you know, SolrCloud has collections, and each collection is “pointing” to a “configuration” – Configsets. You can quickly locate it. Just open solr in your browser and locate a collection. Click on it, and you should see something like this: 


You normally give the “configurations” the same name as your collection. For instance, the collection sitecore_web_index will point to a “configuration” called sitecore_web_index_config:

As I mentioned before, we have our configurations in zip files. So let’s open up our sitecore_web_index_config zip file. Update the solrconfig.xml file and zip it again:

The next part is to upload the zip file to our SolrCloud instance. To do this, we will use the Solr API – ConfigSets API – https://solr.apache.org/guide/8_8/configsets-api.html

Jeremy Davis and his excellent work have already done the heavy lifting – An example of a SolrCloud Docker container for Sitecore v10. Let’s use parts of the excellent script, MakeCollections.ps1, to make a script. We shall call it…

Not Mini-Me, Dr. Evil. We will name “him” – UpdateSolrCloudInKubernetes.ps1

$solrHostname = "localhost"
$solrClientPort = 58105

$coreConfigFolder = "C:\Projects\SampleProject\docker\build\solrcloud\schemas"

function Test-SolrConfigSetExists
{
    param(
        [string]$solrHost,
        [int]$solrPort,
        [string]$configSetName
    )
    
    $url = "http://$($solrHost):$solrPort/solr/admin/configs?action=LIST"
    
    $result = Invoke-WebRequest -UseBasicParsing -Uri $url
    $match = $result.Content.Contains("`"$configSetName`"")
    
    return $match
}

function Upload-SolrConfigSet
{
    param(
        [string]$solrHost,
        [int]$solrPort,
        [string]$zipFile,
        [string]$configName
    )

    $exists = Test-SolrConfigSetExists $solrHost $solrPort $configName

    if( $exists -ne $true )
    {
        Write-Host "Uploading config set $configName"

        $uri = "http://$($solrHost):$solrPort/solr/admin/configs?action=UPLOAD&name=$configName"

        Invoke-RestMethod -Uri $uri -Method Post -InFile $zipFile -ContentType "application/octet-stream" | Out-Null
    }
    else
    {
        Write-Host "Config set $configName exists - skipping"
    }
}

function Test-SolrCollectionExists
{
    param(
        [string]$solrHost,
        [int]$solrPort,
        [string]$solrCollectionName
    )

    $url = "http://$($solrHost):$solrPort/solr/admin/collections?action=LIST"
    
    $result = Invoke-WebRequest -UseBasicParsing -Uri $url
    $match = $result.Content.Contains("`"$solrCollectionName`"")
    
    return $match
}



function Update-SolrCollection
{
    param(
        [string]$solrHost,
        [int]$solrPort,
        $solrCollectionName,
        $solrCollectionConfig
    )

    $exists = Test-SolrCollectionExists $solrHost $solrPort $solrCollectionName

    if( $exists -eq $true)
    {
        Write-Host "Updating collection $solrCollectionName with config $solrCollectionConfig"

        $url = "http://$($solrHost):$solrPort/solr/admin/collections?action=MODIFYCOLLECTION&collection=$solrCollectionName&collection.configName=$solrCollectionConfig"
        Invoke-WebRequest -UseBasicParsing -Uri $url | Out-Null
    }
    else
    {
        Write-Host "Collection $solrCollectionName does not exists - skipping"
    }
}

Upload-SolrConfigSet $solrHostname $solrClientPort "$coreConfigFolder\sitecore_web_index_config.zip" "sitecore_web_index_updated_config"
Update-SolrCollection $solrHostname $solrClientPort "sitecore_web_index" "sitecore_web_index_updated_config"




The interesting part is the 2 lines at the bottom, Upload-SolrConfigSet and, Update-SolrCollection. The first method will upload the Configsets, sitecore_web_index_config.zip, to our SolrCloud instance. An important note, you cannot overwrite an existing Configset. That’s why we have named it sitecore_web_index_updated_config. Let’s see the result 🙂

Wonderful, we have the configuration in place. Next will be to point the collection sitecore_web_index to the new configuration sitecore_web_index_updated_config. We will do this with the following method Update-SolrCollection. If you look at the method in the Powershell script, notice the parameter MODIFYCOLLECTION. This will allow us to update our collection and re-point to our new configuration using the parameter collection.configName.

In this example, we are port-forwarding to the SolrCloud. The excellent tool Lens, makes it so easy for us. We have to locate the solr-service(in the Kubernetes cluster), and by “clicking” the service port link. It will trigger an automatic port-forward and open our browser to a forwarded port 🙂

It gives us the following URL http://localhost:58105/solr/#/
Now we just have to update the script with the port number and run the script 🙂

Let’s have a look if the collection is pointing to our new configuration

Success!

That’s all for now folks 😊


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

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