Let editors set pages to always be on top in search result (SearchDataSource not Find)
I got a question from a customer if it was possible to specify certain pages that would always be displayed on top of the search results list disregarding that other pages might have higher ranking.
So I added a checkbox property named “GivePriorityInSearch” to my page types. And ticked it for two pages see image below from Alloy demo site when searching for the word “plan”.
Then I created a simple filter.
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Threading.Tasks; 6 using EPiServer.Core; 7 using EPiServer.Filters; 8 9 namespace Nergard.EPi.Filters 10 { 11 public class PrioritySearchFilter : IPageFilter 12 { 13 #region IPageFilter Members 14 15 public void Filter(EPiServer.Core.PageDataCollection pages) 16 { 17 var priorityPdc = new PageDataCollection(); 18 var normalPdc = new PageDataCollection(); 19 20 for (var x = 0; x <= pages.Count - 1; x++) 21 { 22 if (pages[x]["GivePriorityInSearch"] != null) 23 priorityPdc.Add(pages[x]); 24 else 25 normalPdc.Add(pages[x]); 26 } 27 28 priorityPdc.Add(normalPdc); 29 30 for (var x = 0; x <= pages.Count - 1; x++) 31 { 32 pages[x] = priorityPdc[x]; 33 } 34 35 } 36 37 public void Filter(object sender, FilterEventArgs e) 38 { 39 this.Filter(e.Pages); 40 } 41 42 public bool ShouldFilter(EPiServer.Core.PageData page) 43 { 44 throw new NotImplementedException(); 45 } 46 47 #endregion 48 } 49 }
After that I hooked it up to the SearchDataSource in the search page.
1 SearchDataSource.Filter += SearchDataSource_Filter; 2 3 void SearchDataSource_Filter(object sender, Filters.FilterEventArgs e) 4 { 5 new Nergard.EPi.Filters.PrioritySearchFilter().Filter(e.Pages); 6 }
And now searching for plan gives the following result.
Nice feature.
Nice solution Per. Sticky search results might be especially useful on sites with lots of similar content (e.g. an archive of statistics published each year) where old content will compete with the new content for rankings. In those cases, the site owner will want to make sure the updated content is given priority.