Boosting Search Results with a Template Name

If we want to achieve relevant search results, we need to boost a Sitecore item based on keywords which are entered by user in the a search bar. In this article, I want to share an example of how we can boost results by the item Template Name.

      

public Lis<SearchResultItem> Search(string keyword)
        {
            var sparePartsIndex = ContentSearchManager.GetIndex("YOUR INDEX NAME");
            var sparePartsSearchContext = sparePartsIndex.CreateSearchContext();

            var predicateRoots = PredicateBuilder.True<SearchResultItem>();

            var configuration = GetSearchConfiguration(); //Method is implemented below

            //Here you can put your conditions

            if (!string.IsNullOrWhiteSpace(keyword))
            {
                foreach (var boostingModel in configuration)
                {
                    if (boostingModel.Keywords.Contains(keyword))
                    {
                        predicateRoots =
                            predicateRoots.Or(
                                itm => (itm.TemplateName == boostingModel.TemplateName)
                                    .Boost(boostingModel.BoostValue));
                        predicateRoots =
                            predicateRoots.Or(itm => (itm.TemplateName != boostingModel.TemplateName).Boost(1f));
                    }
                }
            }

            var queryable = sparePartsSearchContext.GetQueryable<SearchResultItem>().Where(predicateRoots);

            var searchresults = queryable.GetResults();

            return searchresults.Hits.Select(x => x.Document).ToList();
        }




        public List<BoostingMode> GetSearchConfiguration()
        {
         //I’ve implemented an example of search configuration for boosting the search result. 
    //This configuration can be implemented in the Sitecore for being more flexible  
            var config = new List<BoostingModel>
            {
                new BoostingModel
                {
                    //If user enter “root” or “roots” keywords, we will boost an item         with the “Highlights Root” template name  
      Keywords = new List<string> {"root", "roots"},
                    TemplateName = "Highlights Root",
                    BoostValue = 3f
                    
                },
                new BoostingModel
                {
      //If user enter “high” or “Highlight” keywords, we will boost an item         with the “Highlight” template name  
                    Keywords = new List<string> { "high", "Highlight"},
                    TemplateName = "Highlight",
                    BoostValue = 5f
                }
            };

            return config;
        }

        public class BoostingModel
        {
            public List<string> Keywords { get; set; }
            public string TemplateName { get; set; }
            public float BoostValue { get; set; }
        }
          

The results would be similar to what is shown below:

boost search template

The code that is shown above is just a simple example of how we would boost an item using its template name. To make this work in a real production environment, you would need to take some time and adapt it into your infrastructure.

Did it work for you? Tell us what you think!