Search Configuration

23/03/2012 18:05

#Post Deployment Settings

#0. INPUT PARAMETERS

#1. Site Collection Search Setting: Do not use custom scopes
#2. Creating dummy content, one FAQ and one Category Card (to create crawled properties)
#3. Search Index Reset and start full crawl
#4. Creating 'category cards' and 'faq' search scope with rules
#5. Creating managed properties and mapping
#6. Start full Crawl
#7. Remove dummy content
#8. Update scopes


#======================= one function to get input parameters and snapin ===================
 #if input parameter is collection, function enables choice
function SelectChoice
 {
         param(
         [array]$options
         )


 if(($options.length -gt 1))
     {
         write-host "======================================================="
         write-host "There are more options of parameter, choose the number."
         write-host "======================================================="
         for($i=0; $i -le $options.length-1; $i++)
             {
                 write-host ($i+1) ". " $options[$i]
             }
         $index=read-host "Select the option number."
         #write-host "You selected number "$index " with value " $options[$index-1]
         $result=$options[$index-1]
         return $result
   
     }
  else
  {
     #write-host "They are no choices, only this one value: "$options[0]
     $result=$options[0]
     return $result
  }  
 }
 
 # check to ensure Microsoft.SharePoint.PowerShell is loaded
     $snapin=Get-PSSnapin | Where-Object {
        $_.Name -eq 'Microsoft.SharePoint.Powershell'}
        if ($snapin -eq $null) {
        Write-Host "Loading SharePoint Powershell Snapin"
        Add-PSSnapin "Microsoft.SharePoint.Powershell"
        }


#0. INPUT PARAMETERS

#Set Site collection Url
if(($siteUrl -eq $null) -or ($siteUrl -eq ""))
{
       $siteUrl="https://td_sp_ui/gbp/portal"  
}
 
#set default search service application name
 if($searchAppName -eq $null -or $searchAppName -eq '')
 {
     $app=get-spenterprisesearchserviceapplication
     $app=SelectChoice -option $app
     write-host "You have chosen " $app.Name
     $searchAppName=$app.Name
 }
#set path to dummy .aspx files for Category Card and FAQ
$srcPath="C:\GBP.Install"
$ccFileName="DummyCategoryCard.aspx"
$faqFileName="DummyFAQ.aspx"


#====================== FUNCTIONS ===================================

 
 
function UploadFile #upload file to pages library, check in, publish, approve
{
    param([string]$srcPath,
            [string]$fileName,
            [string]$subsiteUrl)
           
  $web=get-spweb $subsiteUrl
  $folder=$web.GetFolder("Pages")         
  $file=get-childitem ($srcPath + "\" + $fileName)
  $files=$folder.Files
  $files.Add(($subsiteUrl+"/Pages/" + $fileName), $file.OpenRead(), $true)|out-null
  $files[$fileName].checkIn("checked in by Powershell")
  $files[$fileName].publish("published by Powershell")
  start-sleep -s 2
  $files[$fileName].Item.File.Approve("Approved by Powershell")
  write-host $fileName " was uploaded and approved."
  $web.dispose()
           
}

function StartFullCrawl
{
    param([string]$searchAppName)

$searchapp = Get-SPEnterpriseSearchServiceApplication $searchAppName
$CrawlContent = Get-SPEnterpriseSearchServiceApplication $searchapp| Get-SPEnterpriseSearchCrawlContentSource

if ( $CrawlContent.CrawlStatus  -eq "Idle" )
    {
        $CrawlContent.StartFullCrawl()
    }
   
$CrawlContent.CrawlStatus   
while ($CrawlContent.CrawlStatus -ne "Idle")
{
    start-sleep -s 60 #wait one minute
    write-host $CrawlContent.CrawlStatus
}
 write-host "Full crawl finished."
}

 
function GetServerFromUrl(
     [string]$url
 )
 {
 $first=$url.IndexOf('/')
 $short=$url.substring($first+2,$url.length-$first-2)
 $server=$url.substring(0, $short.IndexOf('/')+$first+2)
 return $server
 }
 
function GetSiteFromUrl(
     [string]$url
 )
 {
 $first=$url.IndexOf('/')
 $short=$url.substring($first+2,$url.length-$first-2)
 $index=$short.IndexOf('/')
 $site=$short.substring($index+1, $short.length-$index-1)
 return $site
 }
 
 function CreateSearchScope
 {
     param(
         [String]$siteUrl,
         [String]$searchAppName,
         [String]$scopeName,
         [String]$libraryLocation,
         [String]$scopeDescription
             )
 
#calculated urls
 $server=GetServerFromUrl $siteUrl
 $site=GetSiteFromUrl $siteUrl
 
#scope parameters
 $scopeUrl= $server +"/"+ $site + $libraryLocation #"/CategoryCards/Pages"
 #$scopeName="category cards_fromPS"
 #$scopeDescription="Category Card library"
 
 
  # check to ensure Microsoft.SharePoint.PowerShell is loaded
     $snapin=Get-PSSnapin | Where-Object {
        $_.Name -eq 'Microsoft.SharePoint.Powershell'}
        if ($snapin -eq $null) {
        Write-Host "Loading SharePoint Powershell Snapin"
        Add-PSSnapin "Microsoft.SharePoint.Powershell"
        }
      
$searchapp = Get-SPEnterpriseSearchServiceApplication $searchAppName
$category = Get-SPEnterpriseSearchMetadataCategory –Identity SharePoint -SearchApplication $searchapp
 
 
#if scope exists, delete and create new
 $scope = Get-SPEnterpriseSearchQueryScope -Identity $scopeName -SearchApplication $searchapp -ea 0
 
 if($scope -ne $null) #if scope exists, delete
     {
     write-host $scopeName " scope already exist in " $searchAppName ", deleting..."
     Remove-SPEnterpriseSearchQueryScope -Identity $scopeName -SearchApplication $searchapp -Confirm:$false
     write-host $scopeName " scope has been successfuly removed from " $searchAppName "."
     }

  #Scope doesn't exis, create
   write-host $scopeName " scope doesn't exist in " $searchAppName ", creating..."
     $searchapp | New-SPEnterpriseSearchQueryScope -Name $scopeName -Description $scopeDescription -DisplayInAdminUI $true|out-null
     $scope = Get-SPEnterpriseSearchQueryScope -Identity $scopeName -SearchApplication $searchapp
  
     $scopeRule = New-SPEnterpriseSearchQueryScopeRule  -RuleType Url -MatchingString $scopeUrl -UrlScopeRuleType "Folder" -FilterBehavior "Include" -Url $server -Scope $scope
     #$scopeRule
     $scopeRule = New-SPEnterpriseSearchQueryScopeRule  -RuleType Url -MatchingString ($scopeUrl+ "/Forms") -UrlScopeRuleType "Folder" -FilterBehavior "Exclude" -Url $server -Scope $scope
     #$scopeRule
    
      write-host $scopeName " scope has been successfuly created in " $searchAppName "."
 
 
 }  #end function

function CreateManagedProperty
 {
     param(
         [String]$searchAppName,
         [String]$managedPropertyName,
         [String]$crawledPropertyName
             )
 
#set default value for search application
 if($searchAppName -eq $null -or $searchAppName -eq '')
 {
     $searchAppName = 'Enterprise Search Service Application'
 }

 
  # check to ensure Microsoft.SharePoint.PowerShell is loaded
     $snapin=Get-PSSnapin | Where-Object {
        $_.Name -eq 'Microsoft.SharePoint.Powershell'}
        if ($snapin -eq $null) {
        Write-Host "Loading SharePoint Powershell Snapin"
        Add-PSSnapin "Microsoft.SharePoint.Powershell"
        }
      
$searchapp = Get-SPEnterpriseSearchServiceApplication $searchAppName
#$category = Get-SPEnterpriseSearchMetadataCategory –Identity SharePoint -SearchApplication $searchapp
$category="Sharepoint"

$crawledproperty = New-SPEnterpriseSearchMetadataCrawledProperty -SearchApplication $searchapp -Category $category -VariantType 31 -PropSet "00130329-0000-0130-c000-000000131346" -Name $crawledPropertyName -IsNameEnum $false
$managedproperty=Get-SPEnterpriseSearchMetadataManagedProperty -SearchApplication $searchapp -Identity $managedPropertyName -ea 0

if($managedproperty -ne $null) #if managed property exists, remove it
{
   $managedproperty.DeleteAllMappings()
   $managedproperty.Delete()
}
#managed property doesn't exist, create

    $managedproperty = New-SPEnterpriseSearchMetadataManagedProperty -SearchApplication $searchapp -Name $managedPropertyName -Type 1 -EnabledForScoping 1
    New-SPEnterpriseSearchMetadataMapping -SearchApplication $searchapp -ManagedProperty $managedproperty -CrawledProperty $crawledproperty
 
 }  #end function
 
 
function RemoveFile
{
    param(  [string]$fileName,
            [string]$subsiteUrl)
           
  $web=get-spweb $subsiteUrl
  $folder=$web.GetFolder("Pages")         
  $files=$folder.Files
  $file=$files[$fileName]
  if($file -ne $null)
  {
    $file.Delete()
    write-host $fileName " was deleted."
  }
  #$files.Add(($subsiteUrl+"/Pages/" + $fileName), $file.OpenRead(), $true)
  #$files[$fileName].checkIn("checked in by Powershell")
  #$files[$fileName].publish("published by Powershell")
 
  $web.dispose()
           
}

function Update-Scopes($siteUrl)
{
 [void][reflection.assembly]::Loadwithpartialname("Microsoft.SharePoint") | out-null
 [void][reflection.assembly]::Loadwithpartialname("Microsoft.office.server.search") | out-null
 
 $s = [microsoft.sharepoint.spsite]$siteUrl
 $sc = [microsoft.office.server.servercontext]::GetContext($s)
 $search = [Microsoft.Office.Server.Search.Administration.SearchContext]::GetContext($sc)
 $scopes = [microsoft.office.server.search.administration.scopes]$search
 $scopes.StartCompilation()
 while ($scopes.CompilationPercentComplete -lt 100) { sleep -seconds 3; write-host "$($scopes.CompilationPercentComplete)% complete" }
}
 
 #================== code  ========================
 
 #Setting summary
 
write-host ""
write-host "*****************  SEARCH SETTINGS SUMMARY ========================"
write-host ""
write-host "1. Site Collection Search Setting: Do not use custom scopes"
write-host "2. Creating dummy content, one FAQ and one Category Card (to create crawled properties)"
write-host "3. Search Index Reset and start full crawl"
write-host "4. Creating 'category cards' and 'faq' search scope with rules"
write-host "5. Creating managed properties and mapping"
write-host "6. Start full Crawl"
write-host "7. Remove dummy content"
write-host "8. Update scopes#Summary of setting"
write-host ""

 
 
 #1. Site Collection Search Setting: Do not use custom scopes
 
$web=get-spweb $siteUrl
$web.AllProperties["SRCH_ENH_FTR_URL"]=$null
$web.update()
write-host "1. Do not use custom scopes setting: OK"
$web.Dispose()

#2. Creating dummy content, one FAQ and one Category Card (to create crawled properties)

write-host "2. Creating dummy content, one FAQ and one Category Card (to create crawled properties)"

UploadFile -srcPath $srcPath -fileName $faqFileName -subsiteUrl ($siteUrl +"/helpdesk/faq")
UploadFile -srcPath $srcPath -fileName $ccFileName -subsiteUrl ($siteUrl +"/CategoryCards")

write-host ""

 

#3. Search Index Reset

write-host "3. Search Index Reset and full crawl"
$searchapp = Get-SPEnterpriseSearchServiceApplication $searchAppName

#Reset index with the following options in brackets:
#Disable Alerts $true/$false
#Ignore unreachable server $true/$false
try
{
    Write-Host "Attempting to reset the index...please wait"
    $searchapp.Reset($false, $false)
    Write-Host "Index successfully reset"
}
catch
{
    Write-Host "There was a problem resetting the index:" $_ -ForegroundColor Red
}


StartFullCrawl -searchAppName $searchAppName
write-host ""

#4. Creating 'category cards' and 'faq' search scope with rules


write-host "4. Creating 'category cards' and 'faq' search scope with rules"
      
 CreateSearchScope -siteUrl $siteUrl -searchAppName $searchAppName -scopeName "category cards" -libraryLocation "/CategoryCards/Pages" -scopeDescription "Category cards library"
 CreateSearchScope -siteUrl $siteUrl -searchAppName $searchAppName -scopeName "faq" -libraryLocation "/Helpdesk/faq/Pages" -scopeDescription "FAQs library"
 
 write-host ""
 
#5. Creating managed properties and mapping

write-host "5. Creating managed properties and mapping"

CreateManagedProperty -searchAppName $searchAppName -managedPropertyName "categorycard" -crawledpropertyName "ows_AvanadeGBPTaxonomy"
CreateManagedProperty -searchAppName $searchAppName -managedPropertyName "categorytags" -crawledpropertyName "ows_Avanade_x002d_GBP_x002d_CategoryCardTags"
CreateManagedProperty -searchAppName $searchAppName -managedPropertyName "faqtopic" -crawledpropertyName "ows_GBPFaqTopic"
CreateManagedProperty -searchAppName $searchAppName -managedPropertyName "faqquestion" -crawledpropertyName "ows_GBPFaqQuestion"
CreateManagedProperty -searchAppName $searchAppName -managedPropertyName "faqbusinessunit" -crawledpropertyName "ows_GBPusinessUnit"
CreateManagedProperty -searchAppName $searchAppName -managedPropertyName "pagelanguage" -crawledpropertyName "ows_Avanade_x002d_GBP_x002d_PageLanguage"

write-host ""

#6. Start full Crawl


write-host "6. Start full Crawl"

StartFullCrawl -searchAppName $searchAppName

write-host ""

#7. Remove dummy content

write-host "7. Remove dummy content"

RemoveFile -fileName $faqFileName -subsiteUrl ($siteUrl +"/helpdesk/faq")
RemoveFile -fileName $ccFileName -subsiteUrl ($siteUrl +"/CategoryCards")

write-host ""

#8. Update scopes

write-host "8. Update scopes"

Update-Scopes($siteUrl)

write-host ""

#Finished
read-host "Search settings completed. Press Enter to close the window."