SharePoint Search - A better way to exclude folders

Background

There are many things that needs to be done in order to make the SharePoint search experience good (or any search technology for that matter). One of these things are to filter out content that isn't relevant to present as search results.

What needs to be removed depends on how your content looks and your business needs and there are a couple of blogs out there that point out how to filter out stuff, for example this blog by Steve Mann
http://stevemannspath.blogspot.se/2013/04/sharepoint-2013-search-removing-junk.html.

This blog contains some sound advise but also one thing that could be bad advice if you don't know what you are doing. I have seen this tip on other blog posts as well so I thought it could be a good idea to bring it up.

Problem

I am talking about the approach to remove folders from the search results. The recommended way that I have seen so far on the blog posts I have read is to use the "IsContainer" property to exclude folder.

Like this:

You will remove the folders for sure, but you are also removing all other items that are containers. Example of such an item are sites. Probably not good unless you have a custom solution for searching sites. Also - do you really know of all items that are classified as "container" in SharePoint now and in the future?

Solution

So, what should you do?

Well, I've looked into this a bit and my solution is to utilize the fact that folders has a specific content type. That way you only exclude folders in a controlled way, rather than excluding all items that are containers.

The base content type id for folders is 01x0120 so that means you could use the following expression to exclude them:

-contenttypeid:0x0120*

But wait!

When using the content type id approach you are also excluding the top level OneNote document placeholders. These are actually stored as folders that contain OneNote files. To solve that I had to do a little property digging and I came up with the solution to use the ProgID property. This little property is available as a managed property OOTB so it's just a matter of using it. The final query that would exclude folders but not OneNote "containers" as well as sites and other "containers":

(-ContentTypeId:0x0120* OR ProgID:OneNote.Notebook)

Mission completed!

Edit Maximilian Melcher pointed out that there might be other items affected by this exclusion like for example a document set. That is true. You need to analyse what you want to exclude those things only. In any case this approach gives you a formula for a more fine grained control than the IsContainer property. /Edit

PS. Rember that the default search result for the top level OneNote container looks like this:

so don't be confused if you see "folders" even after performing this configuration...

Edit Steven Mann pointed out something that I agree with but didn't bring up in the blog post, it wasn't the main point but is certainly relevant. The point he made was that this configuration should in most cases be done on the result source rather than in the web part. As always you need to know what you are doing and why. Configuring the web part also affects the search statistics and those are important. /Edit

Good luck!

Need help?

If you are running SharePoint in any capacity feel free to contact us - we can help you.