Customizing Enterprise Search Results
Posted
Wednesday, June 24, 2009 1:29 PM
by
CoreyRoth
I’ve seen a number of posts by people asking about how to customize the search results screen in MOSS Enterprise Search, so I thought I would take a little bit of time to describe how to do it. Since this goes well with the two new talks I am building on Enterprise Search for SPSOzarks, I figured this would be a good time. To customize search results, you will need to start by going to the results.aspx page of your Search Center. If you don’t want to experiment on your existing Search Center, you can always create a new one. Edit the page, and then modify the CoreResultsWebPart. Some of the things we can change can be done via properties. However, most customizations will involve changing XSL.
In the Results Display / Views section, you can customize the number of results per page as well as how much text is included in the summary of each result. In the Results Query Option section, you can enable search term stemming and turn off duplicate removal. Although I typically leave these options alone. The Selected Columns field we will talk about in a bit when we discuss adding more data to the search results page. You can also choose to sort by relevance or modified date here. Sorting by other managed properties is not an option with the CoreResultsWebPart. Although if you use the Wildcard Search web part you can sort by other managed properties because it uses a Full Text SQL Query.
The Cross-Web Part query ID is used to group all of the other search web parts on the page with the same set of results. The default value, User Query, uses the value the user typed into the search box for the query. You can use the other values Query2, Query3, etc.. to group a different set of results together when using a fixed keyword query. The Fixed Keyword Query can be used to hard code a pre-determined query on a results page. Useful if you always want to display the same thing on a given page. If you use this option, be sure and change the Cross-Web Part Query ID to something besides User Query.
Those are some of the basic customizations you can do, but the most common thing people want is to change the formatting or add additional data to the search results page. The way the CoreResultsWebPart (or CRWP for short) displays results is with an XSL transform. I won’t go much into changing the formatting since all it really involves is changing the HTML inside the XSL document, but I will cover how to display more data in your results. The first thing you need to know is that any thing you want to display has to be a managed property. That means anything you have mapped to a managed property can be displayed in the search results. Before adding your new managed properties to the XSL document, you have to tell the CRWP to include your custom managed properties. You do this by modifying the Selected Columns field. Clicking on it gives you a fancy editor that looks like this:
The editor doesn’t format things very well, but the syntax is pretty simple. To add your own Managed Property, find a place at the end before the Columns element closes and add a Column element. For example, to add a managed property called DocumentType, it would like this:
<Column Name="DocumentType" />
I believe the case is sensitive. If you get the managed property name wrong, you will get an error when you try to execute a search query. The next thing you need to do is edit the XSL document to include your new managed property. If you click the XSL Editor button, you will get a similar screen.
You can edit the XSL directly in here, but I would recommend, you copy and paste the whole thing into a file somewhere and store it in source control. It’s quite easy to forget to close a tag or something else and get an error displaying your search results. Saving the content of the XSL somewhere gives you something to fall back on in case you mess anything up.
Now there is a lot in this XSL file,but there really is not that much you are concerned with. If you want to customize result, the place to start is with the Result template, look for the following line.
<xsl:template match="Result">
This template gets executed for each search result. The srch-Icon span tag displays the icon next to the search result. The srch-Title span tag displays the title and link to the result. The $url variable has the path to the search result. It makes a call to a special template to highlight keyword hits in the title. After this point, is a good place to add your own managed properties. For example, I want to display the DocumentType managed property mentioned above. The best thing to use is a span with a class of srch-Description. Then it is just simple XSL with the value-of element to display the data. The one thing to note is that in this XSL document, all Managed Properties must be specified in all lower case. I have no idea why it was implemented this way, but if you try to match the case you used in the Selected Columns XML document, it simply will not work. Here is what it looks like.
<div class="srch-Description">
Document Type: <xsl:value-of select="documenttype" />
</div>
After this, the description, document size, author, and modified date are displayed. All of these can easily be customized, moved around, wrapped in other elements, or formatted differently. Here is what my new search results screen looks like.
As you can see, it’s not difficult at all to add your own managed properties. I’ve customized this page in many different ways. It doesn’t have to look like a search results page at all. You can make it a grid, a list or whatever. This makes it very easy to provide a rich search experience with all of the additional data that your users need.
Follow me on twitter.