Excluding Folders from Search Results
Posted
Wednesday, August 26, 2009 1:07 PM
by
CoreyRoth
I see this topic come up quite a bit. You have a set of search results, and the user just wants to see the documents, not the folder that contains the documents. Consider the following example, when I query for accounting documents below.
As you can see in the results above, the first item returned is the folder named Accounting which contains the documents listed after it. Many times, people do not want this showing up in the results, so we need to come up with a way to remove it. The easiest way of course is to use the IsDocument keyword and pass it a value of 1. This will give you results like the ones below.
As you can see, this works great, but you don’t exactly want to have to explain to the user how to type in IsDocument:1 into the search box. However, it is quite easy to include this if you are building your own advanced search control. One way to handle this is to create a custom scope that just returns documents. This is quite easy to do but there are a few steps. The first thing we need to do is allow the IsDocument managed property to be used in scope rules. To do this, go to Search Administration –> Metadata Properties and then find IsDocument in the list. Check the Allow this property to be used in scopes checkbox and hit ok.
After making changes to any property in search, you of course need to perform a full crawl. This could take minutes or days depending on the size of your search index. Once you are done, go to the Scopes page and create a new scope. For the purpose of my example today, I’ll just call mine Documents. After you create the new scope, you need to add a new rule. Choose Property Query and then select IsDocument from the list. Enter a value of 1 and choose Require. Now whenever you query against this scope only documents will be returned. Perform a scope update when you are finished or simply wait for the timer job to update it whenever it is scheduled.
At this point you are ready to use your new scope in the Search Center. There are a number of ways to include this such as putting it in a scope display group and giving the user a choice of scopes to use, but for today’s example, I am just going to use the Scope parameter on the CoreResultsWebPart (CRWP). Edit your results.aspx page and then edit the CRWP. Under Miscellaneous, type in the name of your scope in the field and then save your changed to the web part and page.
At this point you are ready to try our your new search results. If all works successfully, you should have search results like this.
As you can see, it’s pretty simple to set this up. If you don’t want all of your queries to be for documents only, you can customize the search box web part to display a list of scopes or something like that. I’ll probably cover how to do that in a future post (although it’s pretty easy).
Follow me on twitter.