Skip to content

Instantly share code, notes, and snippets.

@ryanlewis
Created August 18, 2015 18:48
Show Gist options
  • Save ryanlewis/66d207ecb6981055cf87 to your computer and use it in GitHub Desktop.
Save ryanlewis/66d207ecb6981055cf87 to your computer and use it in GitHub Desktop.
Hooking into the Lucene events with Umbraco
using System.Web.UI;
using Examine;
using Examine.LuceneEngine;
using Examine.LuceneEngine.Providers;
using Umbraco.Core;
using Umbraco.Web;
namespace AMAZINGWEBSITE.Web.Core.ApplicationEventHandlers
{
public class ConfigureLuceneIndexing : IApplicationEventHandler
{
public void OnApplicationInitialized(UmbracoApplicationBase umbracoApplication, ApplicationContext applicationContext)
{
}
public void OnApplicationStarting(UmbracoApplicationBase umbracoApplication, ApplicationContext applicationContext)
{
}
public void OnApplicationStarted(UmbracoApplicationBase umbracoApplication, ApplicationContext applicationContext)
{
var indexer = ExamineManager.Instance.IndexProviderCollection["ExternalIndexer"];
var luceneIndexer = indexer as LuceneIndexer;
if (luceneIndexer != null) luceneIndexer.DocumentWriting += OnDocumentWriting;
}
private static void OnDocumentWriting(object sender, DocumentWritingEventArgs e)
{
// cancel the indexing of anything that isn't underneath the homepage (which has ID 1056)
if (!e.Fields["path"].StartsWith("-1,1056"))
{
e.Cancel = true;
return;
}
// boost the following doctypes
var docType = e.Fields["nodeTypeAlias"];
switch (docType)
{
case "MyBoostedDocTypeAlias":
e.Document.SetBoost(30f);
break;
}
}
}
}
@ryanlewis
Copy link
Author

Another use case here is to attach additional information to your Lucene document, so that node becomes searchable by your attributed data.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment