Freigeben über


Custom Search Web Parts in 2010

The SharePoint 2010 out-of-the box People Search Core Results web part in SharePoint 2010 has the ability to sort search results by name.  The option actually sorts FirstName, then LastName…likely because the XSLT displays the names as FirstName LastName.  A likely common scenario will be to sort the results by LastName, then FirstName like you would see names in a directory like the White Pages.  This blog covers how to build a web part to sort the results by LastName, then FirstName. 

Before jumping into the details, I did hit an issue in which the EnablePhonetic property did not return phonetic results until I specified a RankingModelID.  Considering we’re sorting the results, ranking doesn’t really apply; however, if you want phonetic results, you need to specify a ranking model.  On top of that, the ranking model needs to include the Pronunciations Managed Metadata Property in order for phonetic results to be returned.  As a side note, you can also add additional crawl properties to this managed property if you want phonetic searches across those properties. 

To see the OOB behavior for sorting with phonetic results do a people search on your SharePoint 2010 farm, then select Name from the Sort By dropdown.  If you don’t have a SharePoint 2010 farm with profiles populated, you can download the Information Worker Demo VM and perform a people search for Barry.  You’ll get results for Barr, Barry, Berry, Cari, and Kari.  If you disable phonetic search in your custom web part, you’ll only get exact matches.

Here's the code for a web part that will always sort by LastName, then FirstName.  Once you add the web part to a page, you can specify the People scope, tweak the XSLT, or anything else you can normally do with the core results web part.  Todd Carter delivered a session that goes into more depth on customizing search for more complex scenarios on the SharePoint 2010 Advanced Developer Training site.

namespace SortableSearchResults

{

    [ToolboxItemAttribute(false)]

    public class SortableCoreResults : CoreResultsWebPart

    {

        protected override void ConfigureDataSourceProperties()

        {  

            base.ConfigureDataSourceProperties();

 

            CoreResultsDatasource ds = this.DataSource as CoreResultsDatasource;

            ds.EnablePhonetic = true;

            ds.EnableNicknames = true;

 

            ds.RankingModelID = "D9BFB1A1-9036-4627-83B2-BBD9983AC8A1";

 

            ds.SortOrder.Clear();

            ds.SortOrder.Add("LastName", Microsoft.Office.Server.Search.Query.SortDirection.Ascending);

            ds.SortOrder.Add("FirstName", Microsoft.Office.Server.Search.Query.SortDirection.Ascending);

        }

    }

}

In order to find the GUID you need to specify in the RankingModelID, you can run the following PowerShell.  This will get the default Enterprise Search Service Application, then return all the Ranking Models that are configured.  As mentioned previously, only ranking models that include the Pronunciations Managed Metadata Property will return Phonetic Results.  From some testing, the People based models include the property.  In my sample, I’m using the MainPeopleModel. 

Get all the installed Ranking Models:      
Get-SPEnterpriseSearchServiceApplication | Get-SPEnterpriseSearchRankingModel | fl Name, ID

image

 

You can also take this further by creating a custom ranking model that contains the Pronunciations Managed Metadata Property and install it.  A complex ranking model doesn’t really apply once you sort the results alphabetically.  In a simple load test in my VM based dev box showed my ranking model being slightly slower (very slight) when compared to the MainPeopleModel.  With that, I’m mainly including this to show you can do it, and to show the Pronunciations property in the ranking model:

<?xml version='1.0' encoding='utf-8'?>
<rankingModel name='MyPeopleRanking'
              id='c978ef2b-300a-444b-af9a-d51261294587'
              description='Ranking model that only included Pronunciations managed property'
              xmlns='https://schemas.microsoft.com/office/2009/rankingModel'>
  <queryDependentFeatures>
    <queryDependentFeature name='Pronunciations'
                           pid='180'
                           weight='0'
                           lengthNormalization='5.0000000000'/>
  </queryDependentFeatures>
</rankingModel>

 

The pid attribute is very important as that links the ranking model to the managed property.  To get the pid value used for the queryDependentFeature, you have to get the ID of the Managed Metadata Property.  Here’s some PowerShell that will get that for you.
 

This will dump out all the properties:   
Get-SPEnterpriseSearchServiceApplication | Get-SPEnterpriseSearchMetadataManagedProperty

This will only dump out Pronunciations: 
Get-SPEnterpriseSearchServiceApplication | Get-SPEnterpriseSearchMetadataManagedProperty | where {$_.Name –eq “Pronunciations”}

image

 

In order to use the Ranking Model, you need to add it into SharePoint.  You use the New-SPEnterpriseSearchRankingModel cmdlet, and pass it the above rankingModel XML in a long string.

Get-SPEnterpriseSearchServiceApplication | New-SPEnterpriseSearchRankingModel

image

You’ll then get prompted for the RankingModelXML property.  Paste in the above XML as a string, and hit Enter.  Just swap out the RankingModelID in the sample for the one from your ranking model, and you’re all set.

image

 

Special thanks to Neil Hodgkinson for linking me up with Boxin Li, Victor Poznanski, and Dmitriy Meyerzon from the product group.  They pointed out the link between EnablePhonetic, and the ranking model requiring Pronunciations managed property.

 

Additional Links:

Ranking Model Schema

Blog discussing Ranking Models in SharePoint

Comments

  • Anonymous
    March 05, 2013
    Exactly what I was looking for! Could you possibly highlight the necessary changes on the XSLT as well? Thanks