Skip to content

Instantly share code, notes, and snippets.

@bradchristie-velir
Last active September 23, 2020 18:22
Show Gist options
  • Save bradchristie-velir/d69a3fdbc63b69b0871bf11005e461f3 to your computer and use it in GitHub Desktop.
Save bradchristie-velir/d69a3fdbc63b69b0871bf11005e461f3 to your computer and use it in GitHub Desktop.
Create SOLR Indexes

Create SOLR Indexes

Programmatic approach to creating SOLR indexes as desribed by Sitecore documentation in an automated approach.

Summary

The scripts begins by stopping your local SOLR service. Next, it iterates through your indexes copying the original configset to a matching index directory. Within that directory, the managed-schema file is then backed up and modified to use _uniqueid as the primaryKey and goes on to define the _uniqueid field.

Arguments

SolrInstallDir

Root install path of SOLR (where the bin and server folders exists)

default: C:\solr

SolrServiceName

Name of the SOLR service

default: *solr*

Indexes

List of indexes to create.

default includes:

  • sitecore_core_index
  • sitecore_master_index
  • sitecore_web_index
  • sitecore_marketingdefinitions_master
  • sitecore_marketingdefinitions_web
  • sitecore_marketing_asset_index_master
  • sitecore_marketing_asset_index_web
  • sitecore_testing_index
  • sitecore_suggested_test_index
  • sitecore_fxm_master_index
  • sitecore_fxm_web_index

ConfigSet

Config set to base indexes on

default: _default (if you're running <= 6.6 you'll need to use basic_configs)

[CmdletBinding(SupportsShouldProcess)]
Param(
[Parameter()]
[ValidateScript({ Test-Path $_ -PathType "Container" })]
[string]$SolrInstallDir = "C:\solr-7.5.0"
,
[Parameter()]
[ValidateNotNull()]
[string]$SolrServiceName = "*solr*"
,
[Parameter()]
[ValidateNotNull()]
[string[]]$Indexes = @(
"sitecore_analytics_index"
"sitecore_core_index"
"sitecore_fxm_master_index"
"sitecore_fxm_web_index"
"sitecore_list_index"
"sitecore_marketing_asset_index_master"
"sitecore_marketing_asset_index_web"
"sitecore_marketingdefinitions_master"
"sitecore_marketingdefinitions_web"
"sitecore_master_index"
"sitecore_suggested_test_index"
"sitecore_testing_index"
"sitecore_web_index"
"social_messages_master"
"social_messages_web"
)
,
[Parameter()]
[ValidateScript({ Test-Path (Join-Path $SolrInstallDir -ChildPath "server\solr\configsets\${_}") -PathType "Container" })]
[string]$ConfigSet = "_default" # use "basic_configs <= SOLR 6.6
)
Begin {
$_eap = $ErrorActionPreference
$ErrorActionPreference = "Stop"
}
Process {
$solrService = Get-Service $SolrServiceName -ErrorAction SilentlyContinue
If ($null -eq $solrService) {
Write-Error "SOLR service not found."
}
Write-Host "Stopping SOLR service ($($solrService.Name))"
Stop-Service $solrService.Name
$configSetSource = Join-Path $SolrInstallDir -ChildPath "server\solr\configsets\${ConfigSet}"
$configSetDest = Join-Path $SolrInstallDir -ChildPath "server\solr"
$Indexes | ForEach-Object {
$index = $_
$indexDest = Join-Path $configSetDest -ChildPath $index
$managedSchema = Join-Path $indexDest -ChildPath "conf\managed-schema"
$managedSchemaBackup = "${managedSchema}.bak"
Write-Host "Processing ${index}..."
If (!(Test-Path $indexDest)) {
Write-Host " Copying files to ${indexDest}"
Copy-Item $configSetSource -Destination $indexDest -Recurse -Container
} Else {
Write-Host " Index already found, skipping copy."
}
If ($PSCmdlet.ShouldProcess($managedSchema, "Backup") -and !(Test-Path $managedSchemaBackup)) {
Write-Host " Backing up managed-schema"
Move-Item $managedSchema -Destination "${managedSchema}.bak"
}
If ($PSCmdlet.ShouldProcess($managedSchema, "Modify")) {
Write-Host " Making Sitecore modifications to schema"
$xmlDoc = [System.Xml.XmlDocument](Get-Content $managedSchemaBackup)
$xmlDoc.schema.uniqueKey = "_uniqueid"
$field = [System.Xml.XmlNode]$xmlDoc.CreateElement("field")
$field.SetAttribute("name", "_uniqueid")
$field.SetAttribute("type", "string")
$field.SetAttribute("indexed", "true")
$field.SetAttribute("required", "true")
$field.SetAttribute("stored", "true")
$idField = [System.Xml.XmlNode]$xmlDoc.schema.GetElementsByTagName("field")[0]
[void]$xmlDoc.schema.InsertBefore($field, $idField)
$xmlDoc.Save($managedSchema)
}
Write-Host "Done."
}
Write-Host "Starting SOLR service ($($solrService.Name))"
Start-Service $solrService.Name
}
End {
$ErrorActionPreference = $_eap
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment