我正在尝试包含一个相关文档(特别是 Sitecore,Lucene 文档是 an Item
),以便当 Lucene 索引为 A 类型的项目创建文档时,它还将包含另一个项目 B 的所有属性。
最终结果是,当用户搜索在项目 B 上找到的数据时,用户在项目 A 上得到了点击。本质上,我想我正在尝试以编程方式“扩展”Lucene 文档。
到目前为止,这是我的代码。我正在扩展索引器类并覆盖一个方法,在该方法中我将字段从项目 B 添加到项目 A(上下文文档)。在我web.config
的自定义数据库爬虫类中,我添加了一个特定的搜索索引(用于调试速度)。
public class DatabaseCrawlerExtension : Sitecore.Search.Crawlers.DatabaseCrawler
{
protected override void AddAllFields(Lucene.Net.Documents.Document document, Sitecore.Data.Items.Item item, bool versionSpecific)
{
base.AddAllFields(document, item, versionSpecific);
string fieldName;
if (/* item is of template A */)
{
var targetItems = /* get items based on a property */;
foreach (var additionalIndexItem in targetItems)
{
foreach (var fieldKey in additionalIndexItem.Fields
.Select(f => f.Key)
.Where(fk => !fk.StartsWith("_")))
{
document.Add(base.CreateValueField(fieldKey, additionalIndexItem[fieldKey]));
}
}
}
}
}
我已经调试了这段代码,可以看到它到达了调用行document.Add
,并添加了正确的数据。我尝试过的不同之处在于base
首先或最后调用该方法,以及尝试使用该方法AddSpecialFields
而不是AddAllFields
. 这并没有在索引中产生任何额外的数据。
为了调试/查看索引,我一直在重建索引(在 Sitecore 中)并查看最终结果,以及使用名为Luke的工具直接查看生成的索引文件。