Create search indexes that apply to different groups of pages

Create search indexes that apply to different groups of pages

Posted on August 19, 2014 0 Comments

The content you're reading is getting on in years
This post is on the older side and its content may be out of date.
Be sure to visit our blogs homepage for our latest news, updates and information.

By default, when you create Search indexes in Sitefinity, you cannot specify the pages to which the search index applies. Sitefinity indexes the content of all pages. However, in some specific use case scenarios you may need to have search indexes that apply to different groups of pages. For example, you may need to have a search index which indexes the content under a specific group page and to have another search index which indexes the content under another group page. Moreover, you may also want to preserve the default functionality and to be able to create global search indexes as well.

The easiest way to achieve this functionality is to extend the default page inbound pipe and to add your custom logic there.

The first steps will be to create a custom page inbound pipe which inherits from the default PageIndoundPipe class in the following way:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Telerik.Sitefinity.Pages.Model;
using Telerik.Sitefinity.Publishing;
using Telerik.Sitefinity.Publishing.Pipes;
  
namespace SitefinityWebApp
{
    public class PageInboundPipeCustom : PageInboundPipe
    {
          
    }
}

In the PageInboundPipeCustom.cs class, you need to override the PushData() method. Inside this method you can get the name of the search index from the publishing point as shown in the code sample below:

string searchIndexName = this.PipeSettings.PublishingPoint.Name;

and depending on the name of the search index specified under Administration -> Search indexes

Search Index Name
 

we can get the pages under the group page that we would like this search indexed to be applied to and pass these pages to the base PushData() method.

Here is a sample implementation of the PushData() method where we have specified that the Search index 1 will be applied to pages created under Group page 1 while Search index 2 will index the pages under Group page 2:

public class PageInboundPipeCustom : PageInboundPipe
    {
        public override void PushData(IList<PublishingSystemEventInfo> items)
        {
string searchIndexName = this.PipeSettings.PublishingPoint.Name;
 
            List<PublishingSystemEventInfo> myItems = new List<PublishingSystemEventInfo>();
  
             
  
            foreach (var item in items)
            {
                PageNode node = null;
  
                if (item.Item is WrapperObject && ((WrapperObject)item.Item).WrappedObject != null)
                {
                    node = GetContentItem((WrapperObject)item.Item);
                }
                else
                {
                    node = ((PageNode)item.Item);
                }
  
                if (node.Page != null && node.Page.Status == Telerik.Sitefinity.GenericContent.Model.ContentLifecycleStatus.Live)
                {
                    while (true)
                    {
                        
                        node = node.Parent;
  
                        if (node.Parent == null)
                        {
                            break;
                        }
  
                        if (searchIndexName == "Search index 1")
                        {
                            if (node.Title == "Group page 1")
                            {
                                // this will index only pages under Group page 1                            myItems.Add(item);
                                break;
                            }                                                      
                        }
                        if (searchIndexName == "Search index 2")
                        {
                            if (node.Title == "Group page 2")
                            {
                                // this will index only pages under Group page 2                                myItems.Add(item);
                                break;
                            }   
                        }                     
                    }
                }
            }
  
            // pass the pages under the selected group page to the base PushData() method
            base.PushData(myItems);
        }
    }

In order to allow the global search indexes to function as expected we need to add a condition inside the PushData() method (in the very beginning of the method before our custom logic) so that when the name of the search index is not equal to the specified search indexes to pass all the pages to the base.PushData() method and this way to include all the pages in the internal search when using the rest of the search indexes. Here is also a sample code:

public class PageInboundPipeCustom : PageInboundPipe
    {
        public override void PushData(IList<PublishingSystemEventInfo> items)
        {
                         string searchIndexName = this.PipeSettings.PublishingPoint.Name;
 
        if (searchIndexName != "Search index 1" && searchIndexName != "Search index 2")
            {
                base.PushData(items);
            }
 
            List<PublishingSystemEventInfo> myItems = new List<PublishingSystemEventInfo>();
 
           // your custom logic goes here
     }

To replace the default PageInboundPipe with the custom one, we need to unregister the default pipe and register the custom one in our Global.asax file as shown below:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.SessionState;
using Telerik.Sitefinity.Publishing;
using Telerik.Sitefinity.Publishing.Pipes;
  
namespace SitefinityWebApp
{
    public class Global : System.Web.HttpApplication
    {
        protected void Application_Start(object sender, EventArgs e)
        {
            Telerik.Sitefinity.Abstractions.Bootstrapper.Initialized += Bootstrapper_Initialized;
        }
  
        void Bootstrapper_Initialized(object sender, Telerik.Sitefinity.Data.ExecutedEventArgs e)
        {
            if (e.CommandName == "Bootstrapped")
            {
                PublishingSystemFactory.UnregisterPipe(PageInboundPipe.PipeName);
                PublishingSystemFactory.RegisterPipe(PageInboundPipeCustom.PipeName, typeof(PageInboundPipeCustom));
            }
        }
    }
}

After performing the above please build the solution, restart the application and re-index the search indexes from Administration -> Search indexes -> <Your Search index> -> Actions link -> Reindex. Then you will be able to test the custom functionality for the search indexes.

The PageInboundPipeCustom.cs and the Global.asax files are available on GitHub.

Here is a video of the end result:

Sabrie Nedzhip

Sabrie Nedzhip is a Tech Support Engineer at Telerik. She joined the Sitefinity Support team in December 2013.

Comments
Comments are disabled in preview mode.
Topics
Latest Stories
in Your Inbox

Subscribe to get all the news, info and tutorials you need to build better business apps and sites

Loading animation

Sitefinity Training and Certification Now Available.

Let our experts teach you how to use Sitefinity's best-in-class features to deliver compelling digital experiences.

Learn More
More From Progress
New Digital Challenges, New Me: Meet Sitefinity DX 14.0
Read More
 
232x131_ResourceImage
Creating Epic Digital Experiences for B2B Companies
Read More
 
dev_thumb370x200
Five Reasons Why Developers Love Sitefinity Marketplace
Read More