+1-888-365-2779
Try Now
More in this section

Forums / Developing with Sitefinity / How to ReIndex a built-in search index

How to ReIndex a built-in search index

2 posts, 0 answered
  1. Joe
    Joe avatar
    21 posts
    Registered:
    01 Dec 2010
    13 Jul 2015
    Link to this post

    I have a 7.3 sitefinity project and I was dissecting the libraries with JustDecompile and came up with this class to pass in an Index name and then execute a Reindex just like if you would from the admin.  I'm wondering if anybody sees any issues with it.  Also i'm using Elmah but you could take that out if you want.

     

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using Telerik.Sitefinity.BackgroundTasks;
    using Telerik.Sitefinity.Publishing.Configuration;
    using Telerik.Sitefinity.Publishing.Web.Services;
    using Telerik.Sitefinity.Publishing.Web.Services.Data;
    using Telerik.Sitefinity.Services;
    using Telerik.Sitefinity.SitefinityExceptions;
     
     
    namespace SitefinityWebApp.CommonClasses
    {
        public class ReindexSearchBackgroundTask : IBackgroundTask
        {
            public string IndexName;
     
            public ReindexSearchBackgroundTask(string thisIndexName)
            {
                IndexName = thisIndexName;
            }
             
            public void Run(IBackgroundTaskContext context)
            {
                SystemManager.RunWithElevatedPrivilege(new SystemManager.RunWithElevatedPrivilegeDelegate(this.RunTask));
     
            }
            private void RunTask(object[] args)
            {
                PublishingAdminService service = new PublishingAdminService();
                var points = service.GetPublishingPoints("SearchPublishingProvider", "LastModified", 0, 50, String.Empty);
                if (points != null && points.TotalCount > 0)
                {
                    foreach (var p in points.Items)
                    {
                        if (p.Title == IndexName)
                        {
                            string searchIndexId = p.Id.ToString();
                            try
                            {
                                service.ReindexSearchContent(PublishingConfig.SearchProviderName, searchIndexId);
                            }
                            catch (System.IO.IOException ioEx)
                            {
                                Elmah.ErrorSignal.FromCurrentContext().Raise(new Exception(ioEx.Message, ioEx.InnerException));
                            }
                            catch (Exception ex)
                            {
                                Elmah.ErrorSignal.FromCurrentContext().Raise(ex);
                            }
                        }
                    }
                }
                
            }
        }
    }​

     

  2. Tim
    Tim avatar
    258 posts
    Registered:
    22 Jun 2011
    18 Jul 2015 in reply to Joe
    Link to this post
    Something similar to this came up in the Sitefinity Devs group on G+. Here's a link to the discussion.
2 posts, 0 answered