4

What is the best way to boost a template (in standard values?) when a specific search term is entered?

eg. when a user searches on "training events" I would like to boost all "Event" template pages.

I was hoping this could have been done with "Item Rules" but I could not find the parameters in the rule set editor.

  • 1
    Do you mean a content search from a code (programmatically) or search at the Content Editor interface? – Artsem Prashkovich May 13 '17 at 9:10
  • I haven't tested it, but I think your answer is here. Create a new search rule for when template = Events, boost results. doc.sitecore.net/sitecore_experience_platform/… – Chris Auer May 13 '17 at 13:48
  • In a content search programatically. I am using Solr, but I am hoping the search engine doesn't matter. – PatMac May 14 '17 at 10:20
  • As long as you use the ContentSearchManager, all changes are translated to the underlying search provider. So the provider Lucene, SOLR or Azure search doesn't matter. – Chris Auer May 15 '17 at 2:18
5

If we speak about the search that implimented in the code, you can use something like below:

public List<SearchResultItem> Search(string keyword)
{
    var sparePartsIndex = ContentSearchManager.GetIndex(IndexName);
    var sparePartsSearchContext = sparePartsIndex.CreateSearchContext();

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

    //Here you can put your conditions

    if (!string.IsNullOrWhiteSpace(keyword))
    {

         if (keyword.Contains("root") || keyword.Contains("roots"))
         {
              predicateRoots = predicateRoots.Or(itm => (itm.TemplateName == "Highlights Root").Boost(5));
              predicateRoots = predicateRoots.Or(itm => (itm.TemplateName != "Highlights Root").Boost(4));
          }

          if (keyword.Contains("high") || keyword.Contains("high"))
          {
              predicateRoots = predicateRoots.Or(itm => (itm.TemplateName == "Highlight").Boost(5));
              predicateRoots = predicateRoots.Or(itm => (itm.TemplateName != "Highlight").Boost(4));
           }
     }

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

     var searchresults = queryable.GetResults();

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

I've implemented that on the test environment and it works for me:

result of executing the search method

We need to understand that method is implemented as an example and conditions can be more flexible, without a hardcodes.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.