由于其他人可能会遇到同样的问题,并在这个问题上登陆(通过搜索),我张贴了我自己的问题的答案。
我编写了两个事件处理程序,如下所示:
-
-
当Examine indexer遇到文档类型别名为“newsArticle”时,将文档提升到相对于上次更新日期的位置。
public class ExamineEvents : Umbraco.Core.ApplicationEventHandler
{
protected override void ApplicationStarted(UmbracoApplicationBase umbracoApplication, ApplicationContext applicationContext)
{
var indexer = (UmbracoContentIndexer)ExamineManager.Instance.IndexProviderCollection["ExternalIndexer"];
indexer.DocumentWriting += Indexer_DocumentWriting;
Umbraco.Core.Services.ContentService.Published += ContentService_Published;
}
private void ContentService_Published(Umbraco.Core.Publishing.IPublishingStrategy sender, Umbraco.Core.Events.PublishEventArgs<Umbraco.Core.Models.IContent> e)
{
ExamineManager.Instance.IndexProviderCollection["ExternalIndexer"].RebuildIndex();
}
private void Indexer_DocumentWriting(object sender, DocumentWritingEventArgs e)
{
//if it is a 'news article' doc type then > BOOST it DOWN - the older the article, the lower the boost value
if (e.Fields.ContainsKey("nodeTypeAlias") && e.Fields["nodeTypeAlias"] == "newsArticle")
{
float boostValue = 1f;
const string umbDateField = "updateDate";
if (e.Fields.ContainsKey(umbDateField))
{
DateTime updateDate = DateTime.Parse(e.Fields[umbDateField]);
var daysInBetween = Math.Ceiling((DateTime.Now - updateDate).TotalDays + 1); // +1 to avoid 0 days
boostValue = (float) (1 / daysInBetween);
}
e.Document.SetBoost(boostValue);
}
}
}