Most SharePoint environments these days make use of managed metadata. If your site does so and you want to use the managed properties within your search Web Parts, you could experience some strange behaviour when sorting your results based on these managed metadata properties. When using the auto-created managed properties linked to your managed metadata column for sorting, a disorder of your results can occur. In this article, I will explain why it seems that your results are not in the correct order and how you can fix this issue.
For this article I will use a simple list with items that are each mapped with a term (a, b, c, d, …) to highlight the sorting problem.
On a page I added a Content Search Web Part (you can also use a Search Result Web Part if you want) to show only the items of that specific list with test items. Figure 1 shows the output without sorting:
Figure 1: Search results without sorting defined
When I configure sorting for the results on my owstaxIdTestTerm managed property (which is the auto-created managed property linked to the managed metadata column), I retrieve the search result order shown in figure 2:
Figure 2: Search results with sorting defined
You can see that the result of the sorting seems to be out of the order you'd expect, but for SharePoint your results are correctly sorted.
This behaviour is related to the managed property you use for configuring your sort order. In this example, the sorting was performed with the owstaxIdTestTerm managed property, which is the auto-created managed property that SharePoint mapped to the crawled property for your column.
Auto-created managed properties for managed metadata columns are, by default, mapped to a crawled property with the following name: ows_taxId_ColumnName. The ows_taxId crawled property contains the following information:
- GP0: Term ID
- L0: Term ID + Display value
- GTSet: Term set ID
Figure 3 shows the same result set with the internal values:
Figure 3: Managed properties term values
In figure 3 I highlighted in red the term ID on which SharePoint performs the sorting of the results. If you check these term ID values, you can see that the results are correctly sorted (that is, 003..., 1147..., 28e1). To solve this problem, you need to configure the managed property to sort on the display value of the term, which requires some extra configuration on your environment.
You solve the problem by using another crawled property that does not contain this term ID related information.
Go to your search schema page and click the Crawled Properties link. On the Crawled Properties page (figure 4), search for the crawled properties linked to your managed metadata column.
Figure 4: Crawled properties
You should get two crawled property results: a crawled property with and without a taxId in its name. The one without the taxId only contains the term's display value. This crawled property will not yet be mapped to a managed metadata column.
Click the Managed Properties link and search for RefinableString (figure 5). Click on one of the RefinableString properties that is not yet in use, and map this managed property with the ows_TestTerm (ows_ColumnName) crawled property.
Figure 5: RefinableString - map the Managed Crawled Property to ows_TestTerm
Wait for a full crawl, or if you are working on-premises you could start one. Once the crawl is complete you can configure the new managed property on the sorting tab in the query builder. Now the results should be in the correct visual order (figure 6):
Figure 6: Correctly sorted results
As figure 6 shows, the results are sorted based on the display value of the managed property term instead of the term ID.
You can find Elio's original blog post here.