SharePoint Server Search & Taxonomy

The Search API of SharePoint is a very powerful way of querying elements across you farm. With SharePoint 2010 it is very easy to refine your results thanks to the taxonomy fields. Both these features offer you interesting development capabilities.

Dealing with this subject I recently encountered a problem while trying to filter elements on taxonomy fields with a FullTextSqlQuery: I was able to get elements with a specified taxonomy term but not the elements with a child term.

The problem

Here is my term store:

  • Locations
    • Europe
      • France
      • Italy
      • UK
      • Spain
    • North America
      • United States
      • Canada

I use a crawled property named “Location”.

Here is my code snippet to query my items for which the location value is “Canada”:

And here is the content of my query:

And obviously I only get the documents with a location explicitly set to “North America”. How to retrieve these which belong to “Canada” and “United States”?

Solution

The solution is quite simple. I found it here on the MSDN Library. For each taxonomy column (only site columns I presume), a corresponding crawled property is automatically generated. Its name is “owstaxId{FieldInternalName}”. You have to use this crawled property to filter your query.

Moreover the filter value must follow the format : #{TermGuid} where {TermGuid} is the GUID of the term you want to filter. If you want to exclude the child results you have to add a 0 in front of the Guid (#0{TermGuid}).

Example 1: I want to retrieve all the documents with a location set to North America or to a child term of North America. The Guid of the North America term is ‘24DDA1D3-2C38-4F5A-83D5-AFCFC369E8A9’ so I have to use the following query:

Example 2 : I want to retrieve only documents with a location set to North America and exclude child terms. I will use this query:

Et voilà!