Ravindra S. Rathore
Sep 20, 2019
  2543
(7 votes)

Reindex a target site in Find (Schedule Job)

Hey Guys,

We all use Episerver Find as a search provider and it is a very handy tool for the site search. I also prefer Find over all the other search provider in Epi.

But index rebuild is very time consuming if you have multiple sites hosted in the Episerver CMS.

I am working on a project and it has around 50+ sites those are using the search functionality and whenever I want to rebuild the index for one site I have to run the pre-defined schedule job(EPiServer Find Content Indexing Job) and it takes around 1-2 hours because it rebuilt the index for all the sites.

I need a way to rebuild the indexes for the specific site so I have created a new schedule job.

using System;
using System.Text;
using System.Web;
using EPiServer.Find.Cms;
using EPiServer.Find.Helpers.Text;
using EPiServer.PlugIn;
using EPiServer.Scheduler;
using EPiServer.Web;

namespace ABC.Framework.Web.Business
{
    [ScheduledPlugIn(DisplayName = "Rebuild Targeted Site Indexes", GUID = "A71C3D81-C481-4CB5-A5B9-66BC7C0095FA")]
    public class SiteIndexScheduledJob : ScheduledJobBase
    {
        private bool _stopSignaled;

        public SiteIndexScheduledJob()
        {
            IsStoppable = true;
        }

        /// <summary>
        /// Called when a user clicks on Stop for a manually started job, or when ASP.NET shuts down.
        /// </summary>
        public override void Stop()
        {
            _stopSignaled = true;
        }

        /// <summary>
        /// Called when a scheduled job executes
        /// </summary>
        /// <returns>A status message to be stored in the database log and visible from admin mode</returns>
        public override string Execute()
        {

            if (!string.IsNullOrEmpty(SiteDefinition.Current.Name))
            {
                //Call OnStatusChanged to periodically notify progress of job for manually started jobs
                OnStatusChanged($"Starting execution of rebuild indexes for {SiteDefinition.Current.Name} site");

                var statusReport = new StringBuilder();

                // ReIndex the indexes for the sites that have * in its host configuration
                ContentIndexer.ReIndexResult reIndexResult = ContentIndexer.Instance.ReIndex(
                    status =>
                    {
                        if (status.IsError)
                        {
                            string errorMessage = status.Message.StripHtml();
                            if (errorMessage.Length > 0)
                                statusReport.Append($"{errorMessage}");
                        }

                        OnStatusChanged(
                            $"Indexing job [{(SiteDefinition.Current.Name)}] [content]: {status.Message.StripHtml()}");
                    },
                    () => _stopSignaled);
            }
            else
            {
                return "Job is cancelled. Please add a host entry with * in site, which you rebuild the indexes";
            }


            //For long running jobs periodically check if stop is signaled and if so stop execution
            if (_stopSignaled)
            {
                return "Stop of job was called";
            }


            return $"Index rebuild successful for {SiteDefinition.Current.Name}";
        }
    }
}

It basically rebuilds the indexes for the current site but as you know that if you try to access the "SiteDefinition.Current" in the schedule job it will not return the site, so to overcome this thing you need to apply a wild card character * in the site. Once you applied the wild card character you will get that site as a current site. You can read more about it HERE

So add a wild card character on the site on which you want to rebuild the indexes. Please refer below screenshot for your reference.

Once you are done with this. Please go to Admin->Admin and run the new schedule job "Rebuild Targeted Site Indexes". You will see that it will start the indexing for the wild card character site.

I hope it helps.

Thanks

Ravindra

Sep 20, 2019

Comments

Ha Bui
Ha Bui Sep 20, 2019 04:25 PM

Nice!

Could better if we have a Admin Tool that will show multiple sites for selection.

When click reindex then trigger job for those sites only!

// Ha Bui

Sep 22, 2019 04:48 AM

Nice write up (Y)

Ravindra S. Rathore
Ravindra S. Rathore Sep 22, 2019 03:17 PM

Thanks, @Son Do.

@Ha Bui - Created an Admin Tool for this - https://world.episerver.com/blogs/ravindra-s--rathore/dates/2019/9/reindex-a-target-site-in-episerver-find--admin-tool/

Thanks

Ravindra S. Rathore

Ha Bui
Ha Bui Sep 23, 2019 05:09 AM

Good work @Ravindra!

Please login to comment.
Latest blogs
Opti ID overview

Opti ID allows you to log in once and switch between Optimizely products using Okta, Entra ID, or a local account. You can also manage all your use...

K Khan | Jul 26, 2024

Getting Started with Optimizely SaaS using Next.js Starter App - Extend a component - Part 3

This is the final part of our Optimizely SaaS CMS proof-of-concept (POC) blog series. In this post, we'll dive into extending a component within th...

Raghavendra Murthy | Jul 23, 2024 | Syndicated blog

Optimizely Graph – Faceting with Geta Categories

Overview As Optimizely Graph (and Content Cloud SaaS) makes its global debut, it is known that there are going to be some bugs and quirks. One of t...

Eric Markson | Jul 22, 2024 | Syndicated blog

Integration Bynder (DAM) with Optimizely

Bynder is a comprehensive digital asset management (DAM) platform that enables businesses to efficiently manage, store, organize, and share their...

Sanjay Kumar | Jul 22, 2024

Frontend Hosting for SaaS CMS Solutions

Introduction Now that CMS SaaS Core has gone into general availability, it is a good time to start discussing where to host the head. SaaS Core is...

Minesh Shah (Netcel) | Jul 20, 2024

Optimizely London Dev Meetup 11th July 2024

On 11th July 2024 in London Niteco and Netcel along with Optimizely ran the London Developer meetup. There was an great agenda of talks that we put...

Scott Reed | Jul 19, 2024