Per Nergård
Oct 24, 2013
  3470
(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 finally releases new and improved list properties!

For years, the Generic PropertyList has been widely used, despite it being unsupported. Today a better option is released!

Tomas Hensrud Gulla | Mar 28, 2023 | Syndicated blog

Official List property support

Introduction Until now users were able to store list properties in three ways: Store simple types (int, string, DateTime, double) as native...

Bartosz Sekula | Mar 28, 2023

New dashboard implemented in CMS UI 12.18.0

As part of the CMS UI 12.18.0 release , a new dashboard has been added as a ‘one stop shop’ to enable editors to access all of their content items,...

Matthew Slim | Mar 28, 2023

How to Merge Anonymous Carts When a Customer Logs In with Optimizely Commerce 14

In e-commerce, it is common for users to browse a site anonymously, adding items to their cart without creating an account. Later, when the user...

Francisco Quintanilla | Mar 27, 2023

How to Write an xUnit Test to Verify Unique Content Type Guids in Content Management

When developing an Optimizely CMS solution, it is important to ensure that each content type has a unique GUID. If two or more content types share...

Minesh Shah (Netcel) | Mar 27, 2023

Extend TinyMCE in Optimizely CMS 12

Since technologies are upgraded to newer versions the ways to extend or override the out-of-the-box functionality are also changed a little bit so...

Ravindra S. Rathore | Mar 27, 2023 | Syndicated blog