Export Query Rules Best Bets using PowerShell – SharePoint 2013

Without using any custom code, Search service application administrators, site collection administrators, and site owners can help searches respond to the intent of users by creating query rules. In a query rule, you specify conditions and correlated actions. When a query meets the conditions in a query rule, the search system performs the actions specified in the rule to improve the relevance of the search results, such as by narrowing results or changing the order in which results are displayed.

We all know how hard it is to manage these one by one. Exporting these query rules to Excel, making the modifications and importing from Excel would save tremendous amount of time for Administrators.

Add-PSSnapin Microsoft.Sharepoint.Powershell;
$siteUrl = "https://contoso.com/search";
$resultSourceName = "Local SharePoint Results";
$site = Get-SPWeb $siteUrl;
$web = Get-SPWeb $siteUrl;
$searchServiceApplication = Get-SPServiceApplication -Name "Search Service";
$searchOwner = New-Object Microsoft.Office.Server.Search.Administration.SearchObjectOwner -ArgumentList @([Microsoft.Office.Server.Search.Administration.SearchObjectLevel]::SPSite,$site);
$searchObjectFilter = New-Object Microsoft.Office.Server.Search.Administration.SearchObjectFilter($searchOwner);
$queryRuleManager = New-Object Microsoft.Office.Server.Search.Query.Rules.QueryRuleManager($searchServiceApplication);
$queryRules = $queryRuleManager.GetQueryRules($searchObjectFilter);
$level = "SPWeb"
$searchObjectOwner = New-Object Microsoft.Office.Server.Search.Administration.SearchObjectOwner($level, $web)
$searchObjectFilter = New-Object Microsoft.Office.Server.Search.Administration.SearchObjectFilter($searchObjectOwner)
$federationManager = New-Object Microsoft.Office.Server.Search.Administration.Query.FederationManager($searchServiceApplication)
$resultSource = $federationManager.GetSourceByName($resultSourceName,$searchObjectOwner)
$queryRules = $queryRuleManager.GetQueryRules($searchObjectFilter)
$promotedResultCollection = $searchServiceApplication.GetBestBets($searchObjectFilter)
$customQueryRules = $queryRuleManager.GetQueryRules($searchObjectFilter) | ?{$_.Owner.Level -ne $searchServiceApplication}
Write-Host "There are" $customQueryRules.Count "Customised Query Rules"
"Title `t Url `t Keywords `t Description" | out-file ExportQueryRules.csv
foreach($qryRule in $customQueryRules)
{
#Write-Host "Exporting Query Rule " $qryRule.DisplayName
#$qryRule.QueryConditions
#$queryAction = $qryRule.CreateQueryAction([Microsoft.Office.Server.Search.Query.Rules.QueryActionType]::AssignBestBet)
$termstring ="";
foreach ($term in $qryRule.QueryConditions.Terms)
{
$termstring += $term +";";
}
foreach ($bestbet in $qryRule.AssignBestBetsAction.BestBetIds)
{
$promotedResultCollection[$bestbet].Title + "`t" + $promotedResultCollection[$bestbet].Url.OriginalString+ "`t" +$termstring+ "`t" + $promotedResultCollection[$bestbet].Description | Out-File ExportQueryRules.csv -Append
}}

Leave a Reply

Your email address will not be published. Required fields are marked *