Created
August 18, 2015 18:48
-
-
Save ryanlewis/66d207ecb6981055cf87 to your computer and use it in GitHub Desktop.
Hooking into the Lucene events with Umbraco
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Another use case here is to attach additional information to your Lucene document, so that node becomes searchable by your attributed data.