Sitefinity 4.4- Searches - How to Programatically Reindex

Posted by Community Admin on 04-Aug-2018 03:14

Sitefinity 4.4- Searches - How to Programatically Reindex

All Replies

Posted by Community Admin on 17-May-2012 00:00

We have implemented a custom pipe for indexing custom database content as per blog:

public class CoursesModule : ModuleBase...

public class CoursesInboundPipe : PipeBase, IPushPipe, IInboundPipe

 

The index works fine and search results from the custom index are as expected.
However, we need to schedule a re-index of database content to run every night.
I note that manually reindexing through the Admin section seems to call the method below on our custom pipe.

public void ToPublishingPoint()
Calling this method removes old index and recreates lucene data files.

I would appreciate a code snippet that gets a reference to a registered pipe and subsequently calls the "ToPublishingPoint" method (or whatever approach is required to kick off the reindex). 

We intend to make an authenticated nightly 'Get' request for a page whose code behind programatically re-creates the index.
 
Thanks Steve


Posted by Community Admin on 17-May-2012 00:00

I'm guessing it may be something along these lines:

var pipeSettings=PublishingSystemFactory.GetPipeSettings("CoursesInboundPipe");
var pipe = PublishingSystemFactory.GetPipe("CoursesInboundPipe");
pipe.Initialize(pipeSettings);
pipe.Initialise should set the PublishingPoint:

 

public virtual void Initialize(Telerik.Sitefinity.Publishing.Model.PipeSettings pipeSettings)
       
           this.PipeSettings = pipeSettings;
           this.PublishingPoint = PublishingSystemFactory.GetPublishingPoint(this.PipeSettings.PublishingPoint);
       

Once the PublishingPoint is initialised it may be possible(?) to trigger a reindex using invoke method below.
 Problem is, the PublishingPoint within the PipeSettings object is always null using my initialsation code above. When reindexing through Admin the PipeBase Initialise method is passed a fully populated PipeSettings.PublishingPoint.

if (point != null)
    PublishingManager.InvokeInboundPushPipes(point.Id, null);

Posted by Community Admin on 18-May-2012 00:00

Hello,

This code gets the search index with name "myCustomSearchIndexName" and invoke reindex on it.

var searchIndex = manger.GetPublishingPoints().Where(p => p.Name == "as").FirstOrDefault();
if(searchIndex != null)
   var searchIndexPublishingPointId = searchIndex.Id;
   SearchManager.GetManager().DeleteCatalogue("myCustomSearchIndexName");   
   PublishingManager.InvokeInboundPushPipes(searchIndexPublishingPointId , String.Empty);

This code uses the default search provider, which i guess you use too. If you have any problems with it or any other questions, contact us again.

All the best,
Bonny
the Telerik team
Do you want to have your say in the Sitefinity development roadmap? Do you want to know when a feature you requested is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items

Posted by Community Admin on 18-May-2012 00:00

Thanks for the reply Bonny, 

I've tried your code as per below:
var searchIndex = manger.GetPublishingPoints().Where(p => p.Name ==
"as").FirstOrDefault();

But this returns null for searchIndex, also below returns null:

var searchIndex = manager.GetPublishingPoints().Where(p => p.DateCreated>DateTime.Now.AddDays(-90)).FirstOrDefault;

So not sure what I'm doing wrong?
Cheers
Steve

Posted by Community Admin on 18-May-2012 00:00

for clarity I'm initialising the manager as follws:
PublishingManager manager = PublishingManager.GetManager();

Posted by Community Admin on 18-May-2012 00:00

also can conform index exists:

SearchManager.GetDefaultProviderName();
SearchManager manager = SearchManager.GetManager(SearchManager.GetDefaultProviderName());
var searchIndexFound = manager.SearchIndexExists("courses");

Posted by Community Admin on 18-May-2012 00:00

Hello,

Instead of "as" you should use your search index name - "courses". The "search index name" is the name that you see in the UI - same as that you use for your search index. And if you want to get all publishing points, you can just remove your Where clause. Write me back if the problem exists.

Kind regards,
Bonny
the Telerik team

Do you want to have your say in the Sitefinity development roadmap? Do you want to know when a feature you requested is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items

Posted by Community Admin on 19-May-2012 00:00

The problem seems to be the fact I appear to have no Publishing Points!
Using the code below, the specific request for 'courses' returns null.
The request for a list of Publishing Points returns an empty list.

PublishingManager pubManager = PublishingManager.GetManager();
var pubPoint = pubManager.GetPublishingPoints().FirstOrDefault(w => w.Name == "Courses");
var allpubPoints = pubManager.GetPublishingPoints().ToList();
Not sure what could be the cause of this? I re-indexed 'courses' manually and ran fine. The Lucene index files updated successfully. Is there anything I can try to further test Publishing Points?
Cheers

Steve


Cheers

Steve




Posted by Community Admin on 23-May-2012 00:00

Hello,

Can you send me your whole project with your DB, so I can investigate this issue?

All the best,
Bonny
the Telerik team

Do you want to have your say in the Sitefinity development roadmap? Do you want to know when a feature you requested is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items

Posted by Community Admin on 07-Jun-2012 00:00

.Hi Bonny,
Been away for a few weeks so apologies for late reply...
I now have it working correctly with valid Publishing Points.
The problem was in the CourseInboundPipe class: changing the publishing provider from default to SearchProvider resulted in valid publishing points being returned.

CourseInboundPipe.cs:
_publishingProviderName = publishingConfig.SearchProviderName;


Then invoke reindexing based on CourseInboundPipe's provider name:  

//Get a reference to the pipe.
var pipe = (CoursesInboundPipe)PublishingSystemFactory.GetPipe(CoursesInboundPipe.PipeName);
//Set the correct provider name for the publishing point.
string providerName = pipe.PublishingProviderName;

//Initialise the appropriate Publishing Manager based on the Pipe's Publishing Provider Name. PublishingManager pubManager = PublishingManager.GetManager(providerName);
//Initialise the specific Publishing Point associated with required search index.
var publishingPoint = pubManager.GetPublishingPoints().FirstOrDefault(w => w.Name == indexName);
//Proceed only if Publishing Point was found.
if (publishingPoint != null) PublishingManager.InvokeInboundPushPipes(publishingPoint.Id, providerName);

 

 

Posted by Community Admin on 07-Jun-2012 00:00

Hello,

I am happy to hear that everything is okay now. If you have any other troubles, don't hesitate to contact us again.

Greetings,
Bonny
the Telerik team

Do you want to have your say in the Sitefinity development roadmap? Do you want to know when a feature you requested is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items

Posted by Community Admin on 13-Jun-2012 00:00

Just hit a PipeSettings problem we think is related to our Sitefinity upgrade from 4.3 to 5.0.
The Course index still rebuilds OK from Admin but NOT manually using the following:

PublishingManager .InvokeInboundPushPipes(publishingPoint.Id, providerName);

I can step through the code and see that the PipeSettings are initialised correcly in the PipeBase.cs using Sitefinity Admin or InvokeInboundPipe.
Here's the init code that sets the PipeSettings:
PipeBase.cs:
public virtual void Initialize(Telerik.Sitefinity.Publishing.Model.PipeSettings pipeSettings)
 this.PipeSettings = pipeSettings;
 this.PublishingPoint = PublishingSystemFactory.GetPublishingPoint(this.PipeSettings.PublishingPoint);
 
However, when using InvokeInboundPipe, the PipeSettings are null when needed in subsequent method calls eg: ToPublishingPoint()

We now receive error from Telerik.OpenAccess.Exceptions.InvalidOperationException
 'IObjectScope' is already closed' error when attempting to retreive the PipeSettings.
eg
            WrapperObject obj = new WrapperObject(null);
            obj.MappingSettings = this.PipeSettings.Mappings;
This is very puzzling. No indexing code has been changed apart from the Sitefinity upgrade dll references.

Telerik.OpenAccess is now  rtv v2.050727, Version 2011.3.1320.1
Telerik.Sitefinity is now rtv v4.0.30319, Version 5.0.2860.0

Thanks

Steve.


 




Posted by Community Admin on 14-Jun-2012 00:00

Hello,

This is very strange behaviour. Can you try this code and tell me if the problem continues:

var pointGuid = new Guid(pointId);
var providers = PublishingManager.GetManager().Providers;
SearchIndexPipeSettings pipeSettings = null;
foreach (var provider in providers)
  
     pipeSettings = PublishingManager.GetManager(provider.Name).GetPipeSettings<SearchIndexPipeSettings>().Where(ps => ps.PublishingPoint.Id == pointGuid).FirstOrDefault();
     if (pipeSettings != null)
                    break;
  
 
  if (pipeSettings != null)
  
      SearchManager.GetManager(pipeSettings.SearchProviderName).DeleteCatalogue(pipeSettings.CatalogName);
  
 
  PublishingManager.InvokeInboundPushPipes(pointGuid, providerName);

If the problem still exists with this code, we will need your whole project + DB + configuration to reproduce it and investigate it.

Kind regards,
Bonny
the Telerik team
Do you want to have your say in the Sitefinity development roadmap? Do you want to know when a feature you requested is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items

Posted by Community Admin on 14-Jun-2012 00:00

Thanks very much Bonnie, your code suggestion did the trick!

Here's the merged code deleting the existing catalogue:

private void CreateCourseIndex(string indexName)
 
     //Get a reference to the Courses pipe.
     var pipe = (CoursesInboundPipe)PublishingSystemFactory.GetPipe(CoursesInboundPipe.PipeName);
 
     //Set the correct provider name for the publishing point.
     // Note: CoursesInboundPipe sets provider name using: PublishingConfig.SearchProviderName;
     string providerName = pipe.PublishingProviderName;
 
     //Initialise the appropriate Publishing Manager based on the Pipe's Publishing Provider Name. 
     PublishingManager pubManager = PublishingManager.GetManager(providerName);
 
     //Initialise the specific Publishing Point associated with required search index [courses].
     var publishingPoint = pubManager.GetPublishingPoints().FirstOrDefault(w => w.Name == indexName);
 
     //Proceed only if Publishing Point was found.
     if (publishingPoint != null)
     
         var pointGuid = new Guid(publishingPoint.Id.ToString());
         var providers = PublishingManager.GetManager().Providers;
 
         SearchIndexPipeSettings pipeSettings = null;
 
         //Get Pipe Settings for 'Course' Publishing Point.
         foreach (var provider in providers)
         
             //pipeSettings = PublishingManager.GetManager(provider.Name).GetPipeSettings<SearchIndexPipeSettings>().Where(ps => ps.PublishingPoint.Id == pointGuid).FirstOrDefault();
             pipeSettings = PublishingManager.GetManager(provider.Name).GetPipeSettings<SearchIndexPipeSettings>().FirstOrDefault(ps => ps.PublishingPoint.Id == pointGuid);
             if (pipeSettings != null)break;
         
 
         if (pipeSettings != null)
         
             //Remove old Lucene Catalogue Files for Course Index.
             SearchManager.GetManager(pipeSettings.SearchProviderName).DeleteCatalogue(pipeSettings.CatalogName);
         
 
         //Trigger the indexing using the appropriate Publishing Point Id and Publishing Provider Name.
         PublishingManager.InvokeInboundPushPipes(publishingPoint.Id, providerName);
 
     
 
 
 

Posted by Community Admin on 14-Jun-2012 00:00

Hi,

I am glad to hear that. If you have other problems, feel free to contact us again.

Kind regards,
Bonny
the Telerik team

Do you want to have your say in the Sitefinity development roadmap? Do you want to know when a feature you requested is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items

This thread is closed