Custom Ranking Model in Sharepoint search

How do you keep your CDs sorted to quickly find what your looking for? In alphabetical order, based on popularity, genre or just a nice organized chaos?  A search in Sharepoint 2010 usually lists the search results after the built-in relevancy order. According to Microsoft, it's based on BM25 . If it's true or what it really means is hard to know. Since we don't want to blindly rely on Microsoft, we have tested to create our own sort order. The advantage is that we know how it is supposed to act. We have chosen to weight very high if the search keyword is in the name of the document, and then weighted our properties based on what we feel is most relevant. We're evaluating this right now but so far we are pretty pleased.

It was hard to find any good description of how to go about building custom order of relevance, but I hope the following description will still be pretty accurate:

In SharePoint 2007 there was a nice tool for weight our search results according to your properties. The tool is available here:

In 2010, you have to fix this yourself. What you do is creating an XML with instructions on how the search should be weighted. With the help of the XML you then create a custom ranking model that is added to Sharepoint as possible choices. One can then choose whether to use any of the Sharepoint default ranking models or our own.

How do you do that? Well, here is a template on how the xml should look like. It looks like this:
<rankingModel name=”string” id=”GUID” description=”string” xmlns=”“>
                          <queryDependentFeature pid=”PID” name=”string” weight=”weightValue” lengthNormalization=”lengthNormalizationSetting” />
                          <categoryFeature pid=”PID” default=”defaultValue” name=”string”>
                            <category value=”categoryValue” name=”string” weight=”weightValue” />
                          <languageFeature pid=”PID” name=”string” default=”defaultValue” weight=”weightValue” />
                          <queryIndependentFeature pid=”PID” name=”string” default=”defaultValue”  weight=”weightValue”>
                            <transformRational k=”value” />
                            <transformInvRational k=”value” />
                            <transformLinear max=”maxValue” />

Open in new window

The following link describes the different elements in the XML: (office.14). aspx

I begin with the element RankingModel where I must state the name and id. The description is optional. In queryDependentFeatures I can add my managed properties. I will add a queryDependentFeature element, but do not understand what they mean by pid. I finally found out how to get the pid. This is done with the following PowerShell command:
Get-SPEnterpriseSearchServiceApplication | Get-SPEnterpriseSearchMetadataManagedProperty

Open in new window

Well, let's go on with the queryIndependentFeatures. categoryFeature I guess is about Categories contained in the Metadata Property Mapping’s. I’m not interested in it, so I skip it. To weight different language with LanguageFeature does not interest me either, because everything is in Swedish.
However, I am curious about what a queryIndependentFeature is. It turns out when you look at the example that it is about the weighting of other things than manage properties.Such as where the document is placed in the hierarchy or how many people opened it. I’ve found these four names on a queryIndependentFeature: DistanceFromAuthority, URLdepth, DocumentPopularity, DocumentUnpopularity. I weighted DocumentPopularity. I have no idea what transformsRational, transformInvRational and transformLinear are, but I cut out from an example and they used transformsRational so I did that to.

The XML becomes something like this in the end:
<?xml version="1.0" encoding="utf-8"?>
                      <rankingModel name="KM_Ranking" id="0f054f54-6a42-4a9b-abcb-123456778" description=”KMs viktning på managed property” xmlns=”“>
                          <queryDependentFeature pid=”56" name=”Filename” weight=”75"  lengthNormalization=”75” />
                          <queryDependentFeature pid=”11" name=”Land” weight=”70" lengthNormalization=”70"/>
                          <queryDependentFeature pid=”2" name=”Län” weight=”70" lengthNormalization=”70"/>
                          <queryDependentFeature pid=”3" name=”Kommun” weight=”60" lengthNormalization=”60"/>
                          <queryDependentFeature pid=”4" name=”Stad” weight=”55" lengthNormalization=”55"/>
                          <queryDependentFeature pid=”5" name=”Stadsdel” weight=”50" lengthNormalization=”50" />
                          <queryDependentFeature pid=”6" name=”Församling” weight=”45" lengthNormalization=”45"/>
                          <queryDependentFeature pid=”7" name=”Postnummer” weight=”40"lengthNormalization=”40"/>
                          <queryDependentFeature pid=”8" name=”Created” weight=”30¿ lengthNormalization=”30"/>
                         <queryDependentFeature pid=”1" name=”Body”  weight=”20" lengthNormalization=”20" /> 
                          <queryIndependentFeature name=”DocumentPopularity”
                                                       pid=”306" default=”0" weight=”80">
                            <transformRational k=”1.2170868558"/>

Open in new window

Edit: It's importent to include the DependentFeature “body” if you want search hits on content (text in document).

To install this I run the following PowerShell script:
Get-SPEnterpriseSearchServiceApplication | New-SPEnterpriseSearchRankingModel

Open in new window

When I run the script, I will be prompted to write the XML. Organize the XML into one line and past it into the PowerShell command window.

When you come this far, it's always nice to try around with the ranking models. Here are some PowerShell commands that can be useful to know:

Delete a ranking model:
$rankingModel = Get-SPEnterpriseSearchServiceApplication -Identity “Search Service” | Get-SPEnterpriseSearchRankingModel $rankingModel | Remove-SPEnterpriseSearchRankingModel

Open in new window

Show installed ranking models:
Get-SPEnterpriseSearchServiceApplication -identity “Search Service Application Name” | get-SPEnterpriseSearchRankingModel 

Open in new window

If you want to see the XML, try this SQL-script in the database:
                        SELECT TOP 1000 [ModelId]
                        FROM [Servernamn_Search_AdminDB].[dbo].[MSSRankingModels]

Open in new window

Can I test my Custom Ranking Model to see if it works? Well, go to the search results page and add the parameter rm in the url with the guid to your ranking model as value. Should look something like this: http://site/sitepages/searchresult.aspx?rm=0f054f54-6a42-4a9b-abcb-12345678

Then it's time to install it so that it works for the users. According to this blog:
there are several options. I chose the first one. What you do is export the search results web part, change the ranking settings, and import it again. How do I go about:
Go to the search results page and choose to export the search core results webpart.

I download the file and open it in SharePoint Designer. I search for DefaultRankingModelID. I put the quid in the tag, so that it becomes:
<property name=”DefaultRankingModelID” type=”string”>0f054f54-6a42-4a9b-abcb-1234567</property>

Open in new window

I save the file and go to the search results page. I choose Edit –> Insert –> web part ->  “Upload a Web part” and transfer my core results web part.

Now it is transferred to Sharepoint, but not shown in the layout. Therefore, we choose Edit –> Insert –> web part again. There should be a new folder called something like “Imported Web Parts” where it supposed to be. Is it not, then try importing it again.

Good Luck!

Note: This article is based on one of my post from my blog:

Comments (1)



Have a question about something in this article? You can receive help directly from the article author. Sign up for a free trial to get started.