Instead of using a console project and scheduling it to run every 15 minutes, you could take advantage of Umbraco's Content event handling and index the node every time it's published. This way your index is refreshed straight away, and you don't have to worry about external scheduling etc.
Here's how:
- Create a new solution (I normally use an empty Web Project but you can just create a Library project) and install the UmbracoCms.Core nuget package corresponding to your UmbracoCms version (it will install all the necessary dependencies for you including Examine).
- Add the Luncene.Net packages as per the tutorial
- Add a class deriving from
Umbraco.Core.ApplicationEventHandler
and override ApplicationStarted - this will give you access to the Umbraco Services as well as the Examine Indexing events - you can use either for this exercise - you may find the Examine Events more suitable.
- In your Umbraco project add a reference to your new project so it get's pulled in.
Your EventHandler class may look something like this:
public class SpacialIndexingEventHandler : ApplicationEventHandler
{
protected override void ApplicationStarted(UmbracoApplicationBase umbracoApplication, ApplicationContext applicationContext)
{
Umbraco.Core.Services.ContentService.Publishing += ContentService_Publishing;
}
private void ContentService_Publishing(Umbraco.Core.Publishing.IPublishingStrategy sender, Umbraco.Core.Events.PublishEventArgs<IContent> e)
{
// For each published node, perform the necessary indexing.
string nodeTypeAlias = "ArticlePage";
foreach (var node in e.PublishedEntities.Where(a => a.ContentType.Alias == nodeTypeAlias))
{
// Index this
}
}
}
If you wanted to do the indexing whenever Umbraco's indexing triggers, use this instead:
public class SpacialDataIndexingEventHandler : ApplicationEventHandler
{
protected override void ApplicationStarted(UmbracoApplicationBase umbracoApplication, ApplicationContext applicationContext)
{
// connect to the GatheringNodeData event of the provider you're interested in:
foreach(var provider in ExamineManager.Instance.IndexProviderCollection.AsEnumerable<BaseIndexProvider>()) {
if (provider.Name.StartsWith("External")) {
provider.GatheringNodeData += provider_GatheringNodeData;
break;
}
}
}
void provider_GatheringNodeData(object sender, IndexingNodeDataEventArgs e)
{
if (e.IndexType == IndexTypes.Content)
{
// Get the Node from the ContentService:
var node = ApplicationContext.Current.Services.ContentService.GetById(e.NodeId);
// Do your spacial indexing here;
}
}
}