locked
Creating Bestbets via powershell, how do I set the various options? RRS feed

  • Question

  • I have this script to create promoted results (best bets)

    But it creates a bestbet using the "advanced query text match" option.

    I want to use the "Query matches keyword exactly".

    When I dig into the objects, I came up with this,

    $queryRules | select -ExpandProperty queryconditions | select id, BoundVariables, MatchingOptions, SubjectTermsOrigin 

    But I am not real sure how to use any of those objects.

    Here is the full script:

    $siteUrl = "http://sandbox0391"
    $resultSourceName = "Local SharePoint Results"
    
    $web = Get-SPWeb $siteUrl
    $ssa = Get-SPEnterpriseSearchServiceApplication "SBX Search Service Application 2"
    $queryRuleManager = New-Object Microsoft.Office.Server.Search.Query.Rules.QueryRuleManager($ssa)
    $site = Get-SPSite $siteUrl
    $searchOwner = New-Object Microsoft.Office.Server.Search.Administration.SearchObjectOwner -ArgumentList  @([Microsoft.Office.Server.Search.Administration.SearchObjectLevel]::Ssa,$site.RootWeb)
    $searchObjectFilter = New-Object Microsoft.Office.Server.Search.Administration.SearchObjectFilter($searchOwner)
    $federationManager = New-Object Microsoft.Office.Server.Search.Administration.Query.FederationManager($ssa)
    $resultSource = $federationManager.GetSourceByName($resultSourceName,$searchOwner)
    $queryRules = $queryRuleManager.GetQueryRules($searchObjectFilter)
    $promotedResultCollection = $ssa.GetBestBets($searchObjectFilter)
    
    function CreatePromotedResult([string]$title, [string]$url, [string]$description)
    {
    		Write-Host "Creating Promoted Result .." $title
            $queryRule = $queryRules.CreateQueryRule($title,$null,$null,$true)
    		[string[]]$keywords = $title.split(' ')
            $keywords += $title		
            $keywordCondition = $queryRule.QueryConditions.CreateKeywordCondition($keywords,$false)
            $queryAction = $queryRule.CreateQueryAction([Microsoft.Office.Server.Search.Query.Rules.QueryActionType]::AssignBestBet)
            #PromotedResult = BestBet
    		$promotedResult = $promotedResultCollection.CreateBestBet($title, $url, $description, $false)
            #Map the best bet to the query action
            $queryAction.BestBetIds.Add($promotedResult.Id);
            #Update the query rule
            $queryRule.Update()
            $queryRule = $null	
    		Write-Host "Created Promoted Result for" $title "Finished"		
    }
    
    CreatePromotedResult "Benefits" "https://URL.com/Pages/OpenEnrollment.aspx" "Benefits page!"


    Joshua Fuente

    Tuesday, November 17, 2015 4:50 PM

Answers

All replies

  • It's not a solution but have you tried creating one through the GUI and identifying the differences between the coded creation and the GUI creation?
    Tuesday, November 17, 2015 4:59 PM
  • Ya, I was hoping to not have to spend the time reverse engineering it.  I figured someone has to have done it before.  But if no other option is presented, I guess that's what I will do...

    Would be nice to have a bit of a guide or something.  If I do it, I may write an article.


    Joshua Fuente

    Tuesday, November 17, 2015 6:47 PM
  • Hi Joshua,

    The “Query matches keyword exactly” option is FullQueryin QueryCondition.MatchingOptions.

    https://msdn.microsoft.com/EN-US/library/office/microsoft.office.server.search.query.rules.matchingoptions.aspx

    And here is an link about using the matching options in creating the query rule for your reference:

    http://panduchunduri.com/create-promoted-results-using-powershell-import-from-csv/

    Best regards,
    Victoria


    TechNet Community Support
    Please remember to mark the replies as answers if they help, and unmark the answers if they provide no help. If you have feedback for TechNet Support, contact tnmff@microsoft.com.

    • Marked as answer by NachoScript Thursday, November 19, 2015 3:28 PM
    Thursday, November 19, 2015 3:40 AM
  • Perfect, that's what I was looking for...

    $queryRule.QueryConditions[0].MatchingOptions = “FullQuery,ProperPrefix,ProperSuffix”

    I am not a dev, so I didn't know how to use the enum.  But that makes since.


    Joshua Fuente

    Thursday, November 19, 2015 3:28 PM