Sitefinity 4.4- Searches - How to Programatically Reindex
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
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);
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);
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?for clarity I'm initialising the manager as follws:
PublishingManager manager = PublishingManager.GetManager();
also can conform index exists:
SearchManager.GetDefaultProviderName();
SearchManager manager = SearchManager.GetManager(SearchManager.GetDefaultProviderName());
var searchIndexFound = manager.SearchIndexExists("courses");
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
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();
Hello,
Can you send me your whole project with your DB, so I can investigate this issue?
All the best,
Bonny
the Telerik team
.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);
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
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.
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);
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);
Hi,
I am glad to hear that. If you have other problems, feel free to contact us again.
Kind regards,
Bonny
the Telerik team