Calling all developers! We invite you to provide your input on Feature Experimentation by completing this brief survey.

 

Per Nergård (MVP)
Oct 24, 2013
  3926
(1 votes)

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”.

1_UnFiltered

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.

 

2_Filtered

Oct 24, 2013

Comments

Eric
Eric Oct 24, 2013 01:32 PM

Nice feature.

Arild Henrichsen
Arild Henrichsen Oct 25, 2013 12:48 PM

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.

Please login to comment.
Latest blogs
Optimizely CMS easy RSS feed integration library

As I've mentioned in my  previous blog post , while I was developing the Optimizely version of my blog, I tried to look for a library that could...

David Drouin-Prince | Jan 25, 2025 | Syndicated blog

Decimal numbers in Optimizely Graph

Storing prices as decimal numbers on a commerce website and planning to expose them through Optimizely Graph? It might not be as straightforward as...

Damian Smutek | Jan 23, 2025 | Syndicated blog

Find and delete non used media and blocks

On my new quest to play around with Blazor and MudBlazor I'm going back memory lane and porting some previously plugins. So this time up is my plug...

Per Nergård (MVP) | Jan 21, 2025

Optimizely Content Graph on mobile application

CG everywhere! I pull schema from our default index https://cg.optimizely.com/app/graphiql?auth=eBrGunULiC5TziTCtiOLEmov2LijBf30obh0KmhcBlyTktGZ in...

Cuong Nguyen Dinh | Jan 20, 2025