Sitecore Faceting Novice Learnings: Part-1

Sitecore 7 and above versions are really powerful in search capabilities, there are lot of things that can be accomplished. Lucene, Solr, ComputedFields, VirtualFields, Buckets and Faceting has made search very easy in Sitecore.

I have intentionally highlighted “Faceting” above as this is what I am going to blog about in this post. This post is about how you can get started with Faceting in Sitecore.

Task Description

Create a facet for News Category that will be displayed and used to filter results on a bucket folder which contains News Articles.

Here I am demonstrating News Articles which are very common to sites other examples where faceting can be implemented are Products, Employees, Customers, Case Studies and many more.

Creating a Facet

In order to get started with faceting the very first step is to define a facet, you can define a facet at “/sitecore/system/Settings/Buckets/Facets

1)      Select Facets-> Right Click and Insert Facet

Create Facet

2)      Provide Name for the facets in our case it would be “News Category”.

Facet Name

3)      Once the facet is created, there are few important fields that needs to be understood,

Field Usage
Field Name This field should have the name of the field in lower case and should match the field name in your sitecore_master_index. Ideally it would be the field name on your News Article template.

Note: If your field name have spaces replace the space with “_” so for e.g News Article template has “News Category” field, then the field name to be specified in facet should be “news_category”

Global Check this field if you want this facet to appear on all your buckets.Note: If your facet is not global and it should appear on a specific bucket only? Refer to section “Non-Global Facets” of this post to achieve this.

Facet-Fields

 

4) Now go to your bucket folder and search, custom configured facet will appear.

Facet-Results

News Category is a single line text field and “Dealernews” value can be seen faceted in above screen.

News-Item

Non-Global Facet

In most of the cases we will be creating non-global facets as they should be displayed only on specific Buckets and not all over the other sitecore buckets.

1)      To do so first off all uncheck the “Global Facet” field on your custom facet.

2)      Select the bucket on which you want the facet to be applied, in Content Tab -> Go to Indexing Section and look for “Facets” field. Select required facet.

Non-Global-Facet

Below is a code snippet in case if you want to add facets to an item programmatically.

public void AddNewsFacets(ID itemID)
        {
            const string newsCategoryFacet = "{C4788FE1-EF4A-4814-961C-1CF85B9144CA}";
            var item = Sitecore.Context.Database.GetItem(itemID);

            Logger.LogMessage(string.Format("Adding Facets {0}", itemID), this);
            using (new EditContext(item))
            {
                MultilistField fieldfacet = item.Fields["__facets"];
                if (fieldfacet==null)
                {
                    return;
                }
                //clear the values first if any saved in the field
                item.Fields["__Facets"].Value ="";

               //now add the required facets
                fieldfacet.Add(newsCategoryFacet);                
            }
            Logger.LogMessage("Adding TOT Facets Complete", this);
        }

Be tuned on more for Sitecore Faceting! Till then Keep Sitecoring and Sharing

5 thoughts on “Sitecore Faceting Novice Learnings: Part-1

Leave a Reply

Your email address will not be published. Required fields are marked *