Showing posts with label SharePoint search. Show all posts
Showing posts with label SharePoint search. Show all posts

Wednesday, January 14, 2015

Automate hybrid search center configuration in SharePoint Server 2013



In order to configure a SharePoint hybrid environment to display both SharePoint online search result and on-premises result, you would need to configure search Result Source and  Query Rules. The configuration process is tedious and I would like to provide two scripts to automate the process.

Here are the scripts for both  Result Source and Query Rules creation. I’m creating both on the search service level so they could be used by multiple on-premises search centers.

# Create Result Source on search service level for SharePoint online
if(-not (Get-PSSnapin "Microsoft.SharePoint.PowerShell" -ErrorAction SilentlyContinue)){Add-PSSnapin "Microsoft.SharePoint.PowerShell"}

# Parameter settings
$RemoteSharePointUrl = "https://qualcomm.sharepoint.com";
$ResultSourceName = "SharePoint Online1";
$ProviderID = "1e0c8601-2e5d-4ccb-9561-53743b5dbde7";
# Parameter settings end

#Get the Search Service Application
$SSA = Get-SPEnterpriseSearchServiceApplication;

#Get the Search Service Application Owner
$SSAOwner = Get-SPEnterpriseSearchOwner -Level SSA ;

#Try getting the result source
$resultSource = Get-SPEnterpriseSearchResultSource -Owner $SSAOwner -SearchApplication $SSA -Identity $ResultSourceName;

if(-not $resultSource)
{
$resultSource = New-SPEnterpriseSearchResultSource -Name $ResultSourceName -Owner $SSAOwner -ProviderId $ProviderID -SearchApplication $SSA -RemoteUrl $RemoteSharePointUrl;   
}

 
# Create Query Rules search service level for SharePoint online Result Source
if(-not (Get-PSSnapin "Microsoft.SharePoint.PowerShell" -ErrorAction SilentlyContinue)){Add-PSSnapin "Microsoft.SharePoint.PowerShell"}
# Parameter settings
$queryRuleName='SharePoint Online - Query Rule'
$resultSourceName='SharePoint Online' #This is the Result Source created above
$allSourceContext='TRUE'
# The more URL is the new page created on-premises for SharePoint online search
$moreItemsURL='/search/pages/o365results.aspx?k={subjectTerms}'

#Get the Search Service Application
$SSA = (Get-SPEnterpriseSearchServiceApplication)[0] ;

#Get the Search Service Application Owner
$SSAOwner = Get-SPEnterpriseSearchOwner -Level SSA ;

$resultSource = Get-SPEnterpriseSearchResultSource -Owner $SSAOwner -SearchApplication $SSA -Identity $resultSourceName;

if($resultSource)
{

        #Get the Query Rule Manager
        $QueryRuleManager = New-Object Microsoft.Office.Server.Search.Query.Rules.QueryRuleManager($SSA);

        # Create a search object filter using a $SearchOwner object  (Site collection level - in this case)
        $SearchObjectFilter =  New-Object Microsoft.Office.Server.Search.Administration.SearchObjectFilter($SSAOwner);

        $QueryRules = $QueryRuleManager.GetQueryRules($SearchObjectFilter);
        $queryRule = $QueryRules | Where-Object{$_.DisplayName -eq $queryRuleName };
       
       
        # Create a new rule as a active one if does not exists
        if(-not $queryRule)
        {
            Write-Host -ForegroundColor White "Creating New Query Rule with Name  $queryRuleName";
             "Creating New Query Rule .... " >> $logFile;
           
            $queryRule = $QueryRules.CreateQueryRule($queryRuleName,$null,$null,$true);
           
            $allSource = [bool]::Parse($AllSourceContext);
           
            #Add the Result Source..
            if(-not $allSource )
            {
                $QuerySourceContextCondition = $QueryRule.CreateSourceContextCondition($resultSource);
            }

            # Create result block for the Query Rule
            $QueryRuleAction = $QueryRule.CreateQueryAction([Microsoft.Office.Server.Search.Query.Rules.QueryActionType]::CreateResultBlock);

            #Set the values as seen when creating through UI.
            $QueryRuleAction.QueryTransform.OverrideProperties = New-Object Microsoft.Office.Server.Search.Query.Rules.QueryTransformProperties;
            $QueryRuleAction.QueryTransform.SourceId = $resultSource.Id;
            $QueryRuleAction.QueryTransform.QueryTemplate = "{subjectTerms}";
            $QueryRuleAction.ResultTitle.DefaultLanguageString = 'Results for "{subjectTerms}"';
            $QueryRuleAction.ResultTitleUrl = $MoreItemsURL;
            $QueryRuleAction.GroupTemplateId = "~sitecollection/_catalogs/masterpage/Display Templates/Search/Group_Default.js";
            $QueryRuleAction.AlwaysShow = $true;

            #Update the object
            $QueryRule.Update();
           
        }

}

Tuesday, January 13, 2015

Display hybrid search results and SharePoint online search only in SharePoint Server 2013 for all content including people, conversation, and video

This article describes how to configure a SharePoint hybrid environment so that searches from the SharePoint Server 2013 enterprise Search Center display not only hybrid results—that is, results from both search indexes, but also display only SharePoint online search result on a new search page. This configuration requires an one-way outbound hybrid topology. In this article, we will enhance the procedure not only allow the search results from SharePoint Online appear with the search results from SharePoint Server in same search result page,  but also in a new search page with SharePoint online content only. In this way, user have flexibility to search SharePoint online content directly from SharePoint on-premises. Here are detailed steps.



Step 1: Create three result sources that defines how to get search results from SharePoint Online



Result sources can be created at the Search service application level, the site collection level, or the site level. In this procedure, you create the result source at the Search service application level. This will make the result source available to any query rule that is created at the same level, and also to any query rule that is created for a site collection or site that is in a web application that consumes the Search service application. Here are the detailed steps.
  1. In the SharePoint Server 2013 deployment, in Central Administration, in the Application Management section, click Manage service applications.
  2. Click the Search service application to which you want to add a result source.
  3. On the Search Administration page for the Search service application, in the Quick Launch, click Result Sources.
  4. On the Manage Result Sources page, click New Result Source.
  5. On the Add Result Source page, do the following:
  • In the Name text box, type a name for the new result source like SPO Content
  • For the Protocol, select Remote SharePoint.
  • For the Remote Service URL, type the address of the root site collection of the Office 365 SharePoint Online environment whose results should be included (for example, https://mytenant.sharepoint.com).
  • For the Type, select SharePoint Search Results.
  • Leave Query Transform as default which is {searchTerms}.
  • Leave Credentials Information as default which is Default Authentication.
  • Click OK to save the new result source.
     6. Add another Result Source like SPO Conversations for conversion. The steps are same as step #5 except changing Query Transform to the following value.

{searchTerms?} (ContentTypeId:0x01FD4FB0210AB50249908EAA47E6BD3CFE8B* OR ContentTypeId:0x01FD59A0DF25F1E14AB882D2C87D4874CF84* OR ContentTypeId:0x012002* OR ContentTypeId:0x0107* OR WebTemplate=COMMUNITY)

Here is the screenshot for SPO Conversation result source.


 
     7. Add third Result Source like SPO People for people search. The steps are same as step #5 except changing Type to


Now you should have three new Result Sources associated to the search application.



Step 2: Create three query rule to turn on hybrid search results in SharePoint Server 2013



1. In the SharePoint Server 2013 deployment, in Central Administration, in the Application Management section, click Manage service applications.

2. Click the Search service application in which you created a result source in the previous procedure in this article

3. On the Search_service_application_name: Search Administration page, in the Quick Launch, click Query rules.

4. Under the text For what context do you want to configure rules?, in the Select a Result Source drop-down list, select All Sources. View existing Query Rules.
5. Click New Query Rule.



 6. On the Search_service_application_name: Add Query Rule page, do the following: 

     6.1. In the General Information section, in the Rule Name text box, type a name for the new query rule—for example, SPO Content Query Rule.
     6.2 If the Context section is collapsed, click the arrow next to Context to expand it.
     6.3. In the Context section, select the following three sources. SPO Content, Local SharePoint Results, and Local Video Results.



     6.4 In the Query Conditions section, click Remove Condition so that the rule will fire for any query text.
     6.5 In the Actions section, under Result Blocks, click Add Result Block.
            6.5.1. In the Add Result Block dialog box, do the following:
            6.5.2  In the Block Title section, in the Title text box, change the title to the text that you want to display above the result block on the search results page, such as Results for "{subjectTerms}".
           6.5.3  In the Query section, in the Configure Query text box, keep the default query, which is {subjectTerms}.
           6.5.4  In the Query section, in the Search this Source drop-down list, select the name of the result source that you created in the previous procedure in this article that is SPO Content (Service).
           6.5.5 In the Query section, in the Items drop-down list, select the number of search results such as 2 from SharePoint Online that you want to show in this result block on the search results page.
           6.5.6 In the Settings section, do the following:
            If you want to display a Show More link at the bottom of the result block, select More link goes to the following URL. There are two option for more URL. One is to use SharePoint Online search URL so users will direct search online. Another option is to use the URL we will configure in step #4 so users will search on-premises. Both examples are listed below.

Online URL - 'https://domain.sharepoint.com/search/pages/results.aspx?k={subjectTerms}'
On-premises URL - '/search/pages/o365result.aspx?k={subjectTerms}'
         
        Skip the Routing section. 
        Click OK to add the result block. 


  6.6  On the Add Query Rule page, if the Publishing section is collapsed, click the arrow next to Publishing to expand it.
  6.7 On the Add Query Rule page, in the Publishing section, select Is Active.  
  6.8 Click Save.

7. Click second Query Rule named SPO Conversations Query Rule. Follow the same procedure to create previous SPO Content Query Rule except the following changes.
   7.1 Add SPO Conversations and Conversations to resource as the following screenshot.
   7.2 Select SPO Conversation (Service) as Search this Source. Update the more link to SahrePoint online link like
https://mycompony.sharepoint.com/search/Pages/conversationresults.aspx?k={subjectTerms}

We will add a new search page to display SharePoint online result only and configure "Everything", "People", "Conversation", "Video" to display hybrid result.



Step 3: Create new search result page to display only SharePoint Online content
  1. Open the search site (example is http://spsbx15/search/) using SharePoint designer
  2. Click All Files and then Pages
  3. Copy file named results.aspx and rename to new search result page like o365result.aspx
  4. Right click new search result page o365result.aspx and select Preview in Browser
  5. Edit page
  6. Identify the Search Result webpart and edit the webpart
  7. Click the Change query
  8. In Select a query, select the result source you configured for SharePoint online (Example is SPO Content (Service))
  9. Click SETTINGS tab on the Build Your Query page
  10. Select Don't use Query Rules for Query Rules. This will make sure only return the SharePoint online result.











   11. Edit the Everything search page and edit search result webpart.
       11.1 In Select a query, select the result source Local SharePoint Results (System)
        11.2 Click SETTINGS tab on the Build Your Query page
       11.3 Select Use Query Rules for Query Rules. This will make sure only return both SharePoint online result and SharePoint online result for all content.

  12. Edit the People search page and edit search result webpart.
        12.1 In Select a query, select the result source Local People Results (System)
        12.2 Click SETTINGS tab on the Build Your Query page
       12.3 Select Use Query Rules for Query Rules. This will make sure only return both SharePoint online result and SharePoint online result for People.


  13. Edit the Conversion search page and edit search result webpart.
        13.1 In Select a query, select the result source Conversion (System)
        13.2 Click SETTINGS tab on the Build Your Query page
       13.3 Select Use Query Rules for Query Rules. This will make sure only return both SharePoint online result and SharePoint online result for Conversion.


  14. Edit the Video search page and edit search result webpart.
        14.1 In Select a query, select the result source Local Video Results (System)
        14.2 Click SETTINGS tab on the Build Your Query page
       14.3 Select Use Query Rules for Query Rules. This will make sure only return both SharePoint online result and SharePoint online result for Video.




Step 4: Add SharePoint Online search as a search vertical

1. Browse to the enterprise Search Center you created on your on-premise environment. Click the gear icon and click Site Settings. Under Search, click Search Settings and fill in the following information.

2. Enter a Search Center URL (Optional).You can choose to leave this empty. If you decide to enter a URL of the Global search center, it displays a message to all users offering them the ability to search again from within that search center.

3. Select the Use the same results page settings as my parent check box.

4. Under Configure Search Navigation click Add a Link and fill in the following details:
  • In the Title textbox type in a name to describe the vertical (for example: SharePoint Online)
  • In the URL textbox type the link to the results page you created above, for example, /search/Pages/o365result.aspx
  • If you want a new session to be open for the results, select the Open the Link in new Window check box.
5. You can configure Audience so that this shows up only for people whose OneDrive you have redirected to Office 365 or leave it empty if you want to show it to all.

6. Clicking OK should show a new Navigation called OneDrive, which should now be available to users when they search in the enterprise search center for their On-Premises SharePoint 2013.

Now, you will have hybrid search result on all search pages such as Everything search page.


The more link will redirect to SharePoint Online search result page on SharePoint on-premises.


The People search page will display hybrid people from SharePoint online and on-premises.


The Conversion search page will display hybrid Conversion from SharePoint online and on-premises.


The Video search page will display hybrid Video from SharePoint online and on-premises.



At this time, I have configured the online more link for People, Conversion, and Video to SharePoint online search center. You could create new search pages like "SharePoint Online" to display more People, Conversion, and Video directly from SharePoint on-premises.

There is small bug for this setting, the more link on Video page will point to SharePoint Online search page. I have added another Result Sources and Query Rules to make it point to O365 Video search page. This is a quiz for you to set up your environment.

Now users have flexibility to either search both SharePoint online and on-premises or just SharePoint online content. In next blog, we will provide the scripts to automate the search Result Resource and Query Rule creation to simplify the process.

See Ultimate procedure to display SharePoint online hybrid search results in SharePoint Server 2013 for other steps to configure hybrid search.