Sunday 1 February 2015

Sitecore search facets


Sitecore search comes with a lot of amazing features that facilitate users to find their data, Tags and facets are examples of these features, we will give a simple information that will help to understand facets concept.



Definition:

The process of categorizing the search results according to fields values and allowing those results to be filtered by one or more category at a time is called FACETING.

Faceted Search Benefits:
    
     There is a lot of benefits to use faceted search and we just mention some of the benefits:
  •  Fully configurable
  • Advance caching for improved performance
  • Faster searching through structured filtering of results, therefore enabling you to find the content you are looking for quicker

In sitecore 7 when you are using the search tab result you can see the facets that applied on the search you did; you can see the category and the aggregate count for each category, as you can see below:


When the returned results are big  the faceted search will make it easy for you to categorize the results and filter these results based on these facets or in another  way helping  to drill down using categories to find the end result we are looking for via the site search.

Another information you should know that you can select the predefined facets for your template or items as you can see in the next screen sot.
Now to make it more clear let's suppose that we have a News items and we want to group these items according to publish date field as an example.
we can do this by the following few lines of code:

var index = ContentSearchManager.GetIndex(new SitecoreIndexableItem(Sitecore.Context.Item));

using (var context = index.CreateSearchContext())
{
                IQueryable<SearchResultItem> result = context.GetQueryable<SearchResultItem>().
                Where(resultItem => resultItem.TemplateName.Equals("News")).
                FacetOn(P => P["PublishDate"]);

                List<FacetCategory> facetCategories = result.GetFacets().Categories;

                StringBuilder sb = new StringBuilder();
                foreach (var category in facetCategories)
                {
                                foreach (var value in category.Values)
                 {
                                sb.Append("<p>" + value.Name + " (" + value.AggregateCount.ToString() + ")<p>");
                 }
        }
        lblFacets.Text = sb.ToString();
}
In the previous code we used [FacetOn] function to get the statistics according to the provided field, and then we get the facets categories using [result.GetFacets().Categories] and in our case we just have one category, after that we loop the category values which provide us with each possible value in that category aggregated and grouped.
The following screen shot represents the facets displaying:



 
 hopefully the above was clear enough to let you start using sitecore search facets, In addition to the above information I recommend you to download Autohaus Example Project which will be useful for our subject here

No comments:

Post a Comment