2013-04-22

My search within the SP2013 search functionality continues…

Previously, I posted about Provisioning a new Search application (using Powershell) in SP2013 and continuing with that we will see how to configure Search Result Source using Powershell in SharePoint 2013.

Summary:

  • Create a new Site Collection and a Publishing Site with a separate content database
  • Create two test pages (“Test Page1” and “Test Page2”)
  • Create a new Content Source
  • Run a Full crawl.
  • Create a new Site Collection level Search Result Source with a query filter “{searchTerms} Title:Page1”.
  • Set the new Result Source as a default one.

Powershell:

#Create a new Site collection
$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"
}
$DBServer = "WIN-2J3IDCDEUH6\DEFAULTDB"
$SiteUrl = "http://win-2j3idcdeuh6/"
$SiteContentDBName = "Content_Root"
$WebApp = "http://win-2j3idcdeuh6"
$SiteCollectionName = "Root"
$SiteOwner = "Administrator"
$WebTemplateName = "BLANKINTERNET#0"

#----------------------------------------
#Create a new Publishing Site
#----------------------------------------
$SearchServiceApplicationName = "Search Application"
$ContentSourceName = "My Search"
$SPContentDatabase = Get-SPContentDatabase -site $SiteUrl -ErrorAction SilentlyContinue
if($SPContentDatabase)
{
Write-Host "Content DB : $SiteContentDBName already exist. Removing the Database...."
Remove-SPContentDatabase -Identity $SPContentDatabase.Id -Confirm:$false
}

$RootSiteCollection = Get-SPSite $SiteUrl -ErrorAction SilentlyContinue
if($RootSiteCollection)
{
Remove-SPSite -Identity $SiteUrl -Confirm:$false
}

New-SPContentDatabase -Name $SiteContentDBName -DatabaseServer $DBServer -WebApplication $WebApp
New-SPSite -URL $SiteUrl -OwnerAlias $SiteOwner -ContentDatabase $SiteContentDBName -Name $SiteCollectionName

$RootWeb = Get-SPWeb $SiteUrl
$RootWeb.ApplyWebTemplate($WebTemplateName)
#--------------------------------------------------------
#Create some test pages for search...
#-------------------------------------------------------
$PageLayoutName = "EnterpriseWiki.aspx"
$PublishingWeb = [Microsoft.SharePoint.Publishing.PublishingWeb]::GetPublishingWeb($RootWeb)
$PageLayout = $PublishingWeb.GetAvailablePageLayouts() | where{$_.name -eq $PageLayoutName}
$Page = $PublishingWeb.GetPublishingPages($publishingWeb).Add("testpage1.aspx", $PageLayout)
$Page.Title = "Test Page1"
$Page.Update()
$Page.CheckIn("CheckedIn by the System")
$Page.ListItem.File.Publish("Published by the System")
$Page = $PublishingWeb.GetPublishingPages($publishingWeb).Add("testpage2.aspx", $PageLayout)
$Page.Title = "Test Page2"
$Page.Update()
$Page.CheckIn("CheckedIn by the System")
$Page.ListItem.File.Publish("Published by the System")

#--------------------------------------------
# Create a new Content Source
#--------------------------------------------
$SearchServiceApplication = Get-SPEnterpriseSearchServiceApplication -Identity $SearchServiceApplicationName -ErrorAction SilentlyContinue
$ContentSources = Get-SPEnterpriseSearchCrawlContentSource -SearchApplication $SearchServiceApplication
$ContentSources | ForEach-Object {
if ($_.Name.ToString() -eq $ContentSourceName)
{
Write-Host "Content Source : $ContentSourceName already exist. Deleting the Content source..."
Remove-SPEnterpriseSearchCrawlContentSource -SearchApplication $SearchServiceApplication -Identity $ContentSourceName -Confirm:$false
}
}
$SPContentSource = New-SPEnterpriseSearchCrawlContentSource -SearchApplication $SearchServiceApplication -Type Web -name $ContentSourceName -StartAddresses $SiteUrl -MaxSiteEnumerationDepth 0
if($SPContentSource.CrawlState -eq "Idle")
{
Write-Host "Starting the FullCrawl for the content source : $SPContentSource"
$SPContentSource.StartFullCrawl()
do {Start-Sleep 2; Write-Host "." -NoNewline}
While ( $SPContentSource.CrawlState -ne "CrawlCompleting")
Write-Host "FullCrawl for the content source : $SPContentSource completed."
}

[void] [Reflection.Assembly]::LoadWithPartialName("Microsoft.Office.Server.Search")

#------------------------------------------------------
# create a new content result source...
#------------------------------------------------------
$RootSiteCollection = Get-SPSite $SiteUrl -ErrorAction SilentlyContinue
$SearchServiceApplication = Get-SPEnterpriseSearchServiceApplication -Identity $SearchServiceApplicationName -ErrorAction SilentlyContinue
$FederationManager = New-Object Microsoft.Office.Server.Search.Administration.Query.FederationManager($SearchServiceApplication)

#$SearchOwner = New-Object Microsoft.Office.Server.Search.Administration.SearchObjectOwner
# –ArgumentList @[Microsoft.Office.Server.Search.Administration.SearchObjectLevel]::SPWeb,$RootWeb)
# The above line creates the Search result source at the Web level.

$SearchOwner = New-Object Microsoft.Office.Server.Search.Administration.SearchObjectOwner –ArgumentList @[Microsoft.Office.Server.Search.Administration.SearchObjectLevel]::SPSite,$RootSiteCollection.RootWeb)

#----------------------------------
# create result source
#----------------------------------
$ResultSourceName ="Test Result Source"
$Query = "{searchTerms} Title:Page1"

$ResultSource = $FederationManager.GetSourceByName($ResultSourceName,$SearchOwner)
if($ResultSource)
{
Write-Host "Result Source : $ResultSourceName already exist. Deleting..."
$FederationManager.RemoveSource($ResultSource)
}
$ResultSource = $FederationManager.CreateSource($SearchOwner)
$ResultSource.Name = $ResultSourceName
$ResultSource.ProviderId = $FederationManager.ListProviders()['Local SharePoint Provider'].Id
$ResultSource.CreateQueryTransform($queryProperties, $query)
$ResultSource.Commit()

#---------------------------------------------------------------------------
# Update the new Search source as a default one...
#---------------------------------------------------------------------------
$FederationManager.UpdateDefaultSource($ResultSource.Id,$SearchOwner)

Search Result:

  • Go to the site (http://win-2j3idcdeuh6/) and search for “test”. The search result will contain only the “test page1” because of the Search Result source with the query “{searchTerms} Title:Page1”
  • Deactivate the “Test Result Source” from the site collection search result source and Set the “Local SharePoint Results” as a default one.
  • Go to the site (http://win-2j3idcdeuh6/) and search for “test” . The search result will contain both the test pages (Test Page1 and Test Page2)

Next, I am going to continue my search for the Query Rules configuration (after tonight’s Manchester United football match).
Any comments / mistakes on this one, please let me know and I will amend it accordingly.

Thanks,

Balamurugan Kailasam

About the author 

Balamurugan Kailasam