Geeks With Blogs
Simon's blog
Issue:

Whenever a SharePoint 2010 environment is rebuilt following either a server move or straight forward rebuild, search scopes and rules must be re-created, this can be a laborious process and depending on how many scopes and rules you have can take a significant amount of time

Process:

To rebuild the scopes and rules, the following tasks need to be completed.
  • Complete a full crawl
  • Create Search Scopes
  • Create Search Rules
There may be other tasks required to fully rebuild the whole of your search functionality, but this post focuses on the sections that can be scripted

Solution:

The search service application can be managed through the use of PowerShell scripts, from the basic start/stop to the creation/deletion/modification of scopes and rules.
The above tasks would need to be split in to four scripts:
  • Start a full crawl of the ‘Local SharePoint Sites’
  • Create search scopes and rules
  • Create display groups
  • Map metadata
Starting a Full Crawl
This does not necessarily have to be run via a script, but it does save some time especially if the performance of the web server is poor as there are a few clicks to get to the correct location in Central Admin.

To start a full crawl through the web interface:
                Central Admin > General Application Settings > Search - Farm Search Administration > Search Service Application > Crawling – Content Sources > Local SharePoint Sites (dropdown menu) > Start Full Crawl

The PowerShell commands to start a full crawl are as follows:
$searchapp = Get-SPEnterpriseSearchServiceApplication "Search Service Application"
            Specifies which search application should be used

$contentsource = Get-SPEnterpriseSearchCrawlContentSource "Local SharePoint Sites" -SearchApplication $searchapp
            Specifies which content source should be used and which search application to use

$contentsource.StartFullCrawl()
            Starts a full crawl on the specified content source

Creating Search Scopes and Rules

The two basic commands required are as follows:

This command creates the Search Scope:
New-SPEnterpriseSearchQueryScope –Name <string> -Description <string> -DisplayInAdminUI <Nullable> -SearchApplication <name of search application>

E.G.:
New-SPEnterpriseSearchQueryScope –Name “All Sites Search” –Description “Search everything available for searching” –DisaplayInAdminUI $true –SearchApplication “Search Service Application”

Note: All this does is create the search scope, no rules are created by default.
 
This command creates the rules within the specified scope.
New-SPEnterpriseSearchQueryScopeRule –RuleType <string> -Scope <name of scope> -URL <the url of the server being crawled>

E.G.:
New-SPEnterpriseSearchQueryScopeRule –RuleType “AllContent” –Scope “All Sites Search” –URL “http://server01” –SearchApplication “Search Service Application”

New-SpEnterpriseSearchQueryScopeRule –RuleType “URL” –MatchingString “http://server01/System” -URLScopeRuleType “Folder” –Scope “All Sites Search” –URL “http://server01” –Behaviour “Exclude” –SearchApplication “Search Service Application”

New-SpEnterpriseSearchQueryScopeRule –RuleType “PropertyQuery” –ManagedProperty “contentclass” -PropertyValue “urn:content-class:SPSPeople” –Behaviour “Exclude” –Scope “All Sites Search” –URL “http://server01”–SearchApplication “Search Service Application”

Note: There are other parameters depending on what type of rule is being added, there are three RuleTypes: AllContent, URL and PropertyQuery, all three are likely to be required on a large installation
 

The Script and XML file

CreateScopes_Rules.ps1

#----------------Get the xml file---------------------------------------------------------------
# Check path name for the XML file
[xml]$xmlData=Get-Content "CreateScopes_Rules.xml"
 
#----------------Create New Scope function---------------------------------------------
 
Function CreateNewScope()
{
    $ssa=Get-SPEnterPriseSearchServiceApplication -Identity $xmlData.SSAName
    $scopeCollection=Get-SPEnterpriseSearchQueryScope -SearchApplication $ssa
    $xmlData.Scopes.Scope | ForEach-
    Object {
        $newScopeName=$_.Name
        $newScopeDescription=$_.Description
        $scope=Get-SPEnterpriseSearchQueryScope -Identity $newScopeName -SearchApplication $ssa
        if($scope -eq $null)
        {
            write-host -ForegroundColor Magenta Creating scope $_.scope
            New-SPEnterpriseSearchQueryScope -Name $newScopeName -Description $newScopeDescription -SearchApplication $ssa -DisplayInAdminUI $true
            write-host -ForegroundColor Green $newscope.Name is created successfully
        }
        else
        {
            write-host -ForegroundColor yellow $newScopeName scope already exists
            write-host -ForegroundColor yellow Deleting $newScopeName scope
            Remove-SPEnterpriseSearchQueryScope -Identity $newScopeName -SearchApplication $ssa -confirm:0
            write-host -ForegroundColor green $newScopeName scope is deleted successfully
            write-host -ForegroundColor Magenta Creating $newScopeName scope
            New-SPEnterpriseSearchQueryScope -Name $newScopeName -Description $newScopeDescription -SearchApplication $ssa -DisplayInAdminUI $true
            write-host -ForegroundColor green $newScopeName scope is successfully created
        }
    }
}
Function CreateNewRule()
{
    $ssa=Get-SPEnterPriseSearchServiceApplication -Identity $xmlData.SSAName
    $scopeCollection=Get-SPEnterpriseSearchQueryScope -SearchApplication $ssa
    $xmlData.Rules.Rule | ForEach-
      Object {
            $ScopeName=$_.ScopeName
            $newType=$_.Type
            $newRuleBehaviour=$_.Behaviour
            $newRuleURL=$_.URL
            $newManagedProperty=$_.ManagedProperty
            $newMatchingString=$_.MatchingString
            $newPropertyValue=$_.PropertyValue
            $newRuleType=$_.RuleType
            if($newType="All Content")
            {
                  Write-Host -ForegroundColor Magenta Creating new rule $newManagedProperty + $newPropertyValue
                  New-SPEnterpriseSearchQueryScopeRule -RuleType $newType -url $newRuleURL -scope $ScopeName -SearchApplication $ssa
                  Write-Host -ForegroundColor Green New Rule $newManagedProperty + $newPropertyValue created
            }
            else
            {
                  if($newType="URL")
                  {
                        Write-Host -ForegroundColor Magenta Creating new rule $newManagedProperty + $newPropertyValue                    
                        New-SPEnterpriseSearchQueryScopeRule -RuleType $newType -MatchingString $newMatchingString -UrlScopeRuleType $newRuleType -Filterbehavior $newRuleBehaviour -url $newRuleURL -scope $ScopeName -SearchApplication $ssa
                        Write-Host -ForegroundColor Green New Rule $newManagedProperty + $newPropertyValue created
                  }
                  else
                  {
                        if($newType="PropertyQuery")
                        {
                              Write-Host -ForegroundColor Magenta Creating new rule $newManagedProperty + $newPropertyValue
                              New-SPEnterpriseSearchQueryScopeRule -RuleType $newType -ManagedProperty $newManagedProperty -PropertyValue $newPropertyValue -Filterbehavior $newRuleBehaviour -url $newRuleURL -scope $ScopeName -searchapplication $ssa
                              Write-Host -ForegroundColor Green New Rule $newManagedProperty + $newPropertyValue created
                        }
                        else
                        {
                              Write-Host No more rules to create
                        }
                  }
            }
      }    
}


CreateScopes.xml
<?
xml version="1.0" encoding="utf-8" ?>

<Scopes>
    <
    SSAName>Search Service Application</SSAName>
    <
    Scope Name="All Sites Search" Description="Searches all content" />
    <
    Scope Name="IT Search" Description="Only searches IT pages in the Intranet" />
</
Scopes>

<
ScopeRules>

<!-- All Sites Search Rules-->
      <
      ScopeRule Type="AllContent" URL="http://server01" ScopeName="All Sites Search" />
      <
      ScopeRule Type="URL" MatchingString="http://server01/System" URL="http://server01" RuleType="Folder" Behaviour="Exclude" ScopeName="All Sites Search"/>
      <
      ScopeRule Type="URL" MatchingString="http://server01/extranet/" URL="http://server01" RuleType="Folder" Behaviour="Exclude" ScopeName="All Sites Search"/>
      <
      ScopeRule Type="URL" MatchingString="http://server01/Events" URL="http://server01" RuleType="Folder" Behaviour="Exclude" ScopeName="All Sites Search"/>
      <
      ScopeRule Type="PropertyQuery" ManagedProperty="contentclass" PropertyValue="STS_ListItem_PictureLibrary" Behaviour="Exclude" URL="http://server01" ScopeName="All Sites Search"/>
      <
      ScopeRule Type="PropertyQuery" ManagedProperty="contentclass" PropertyValue="STS_ListItem_850" Behaviour="Exclude" URL="http://server01" ScopeName="All Sites Search"/>
      <
      ScopeRule Type="PropertyQuery" ManagedProperty="contentclass" PropertyValue="STS_ListItem_Tasks" Behaviour="Exclude" URL="http://server01" ScopeName="All Sites Search"/>
      <
      ScopeRule Type="PropertyQuery" ManagedProperty="contentclass" PropertyValue="STS_List_DocumentLibrary" Behaviour="Exclude" URL="http://server01" ScopeName="All Sites Search"/>
      <
      ScopeRule Type="PropertyQuery" ManagedProperty="contentclass" PropertyValue="STS_List_Events" Behaviour="Exclude" URL="http://server01" ScopeName="All Sites Search"/>
      <
      ScopeRule Type="PropertyQuery" ManagedProperty="Filename" PropertyValue="mod-view.aspx" Behaviour="Exclude" URL="http://server01" ScopeName="All Sites Search"/>
      <
      ScopeRule Type="PropertyQuery" ManagedProperty="Filename" PropertyValue="Thumbnails.aspx" Behaviour="Exclude" URL="http://server01" ScopeName="All Sites Search"/>
      <
      ScopeRule Type="PropertyQuery" ManagedProperty="Filename" PropertyValue="AllItems.aspx" Behaviour="Exclude" URL="http://server01" ScopeName="All Sites Search"/>

<!--IT Search-->
      <
      ScopeRule Type="URL" MatchingString="http://server01/search-it/" URL="http://server01" RuleType="Folder" Behaviour="Include" ScopeName="IT Search"/>
      <
      ScopeRule Type="URL" MatchingString="http://server01/search-it/Pages/" URL="http://server01" RuleType="Folder" Behaviour="Include" ScopeName="IT Search"/>
      <
      ScopeRule Type="PropertyQuery" ManagedProperty="contentclass" PropertyValue="STS_ListItem_PictureLibrary" Behaviour="Exclude" URL="http://server01" ScopeName="IT Search"/>
      <
      ScopeRule Type="PropertyQuery" ManagedProperty="contentclass" PropertyValue="STS_List_850" Behaviour="Exclude" URL="http://server01" ScopeName="IT Search"/>
      <
      ScopeRule Type="PropertyQuery" ManagedProperty="contentclass" PropertyValue="STS_List_DocumentLibrary" Behaviour="Exclude" URL="http://server01" ScopeName="IT Search"/>
      <
      ScopeRule Type="PropertyQuery" ManagedProperty="contentclass" PropertyValue="STS_List_PictureLibrary" Behaviour="Exclude" URL="http://server01" ScopeName="IT Search"/>
      <
      ScopeRule Type="PropertyQuery" ManagedProperty="contentclass" PropertyValue="STS_List_Events" Behaviour="Exclude" URL="http://server01" ScopeName="IT Search"/>
      <
      ScopeRule Type="PropertyQuery" ManagedProperty="Filename" PropertyValue="AllItems.aspx" Behaviour="Exclude" URL="http://server01" ScopeName="IT Search"/>
      <
      ScopeRule Type="PropertyQuery" ManagedProperty="Filename" PropertyValue="Thumbnails.aspx" Behaviour="Exclude" URL="http://server01" ScopeName="IT Search"/>
      <
      ScopeRule Type="PropertyQuery" ManagedProperty="Filename" PropertyValue="mod-view.aspx" Behaviour="Exclude" URL="http://server01" ScopeName="IT Search"/>
</
Rules>  

Posted on Wednesday, April 17, 2013 9:59 AM SharePoint 2010 , Search Scopes | Back to top


Comments on this post: Automation of Search scopes and rules rebuild

# re: Automation of Search scopes and rules rebuild
Requesting Gravatar...
Hi,

Are you creating Scopes in 2013, Because in 2013 scopes has been replaced by Result Source.

Thanks
Left by ajit on Jun 24, 2013 8:17 AM

# re: Automation of Search scopes and rules rebuild
Requesting Gravatar...
Sorry, yes this is for 2010 not 2013, edited the post to make that clear.

Simon
Left by Simon on Jun 24, 2013 8:50 AM

Your comment:
 (will show your gravatar)


Copyright © simonh | Powered by: GeeksWithBlogs.net