I'm trying to include a related document (Sitecore-specifically, the Lucene document is an Item
) so that when a Lucene index creates a document for an item of type A, it will also include all properties from another item B.
The end result being that when the user searches for data that is found on item B, the user gets a hit on item A. Essentially, I guess that I'm trying to "extend" a Lucene document programatically.
Here's my code so far. I'm extending the indexer class and overriding a method in which I'm adding the fields from item B to item A (the context document). In my web.config
I've added a specific search index (for debugging speed) with my custom database crawler class.
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]));
}
}
}
}
}
I've debugged this code and can see that it hits the line calling document.Add
, with the correct data being added. What I've tried differently is varying between calling the base
method first or last, and trying to use the method AddSpecialFields
instead of AddAllFields
. This has not produced any additional data in the index.
To debug/look at the index, I've been both rebuilding the index (in Sitecore) and looking at the end result, as well as looking directly in the generated index files using a tool called Luke.