I added a custom field in this open source project I wrote. https://github.com/buildabonfire/Bonfire-Company-Personalization
Get the data
First you need a pipeline to pull the facet from the contact facet in xDB.
using System.Collections.Generic;
using System.Linq;
using Bonfire.Feature.Kickfire.Analytics.Interfaces.Entries;
using Sitecore.ContentSearch;
using Sitecore.ContentSearch.Analytics.Pipelines.ContactIndexableLoadFields;
using Sitecore.Diagnostics;
namespace Bonfire.Feature.Kickfire.Analytics.Pipelines.CompanyIndexableLoadFields
{
class LoadFields : ContactIndexableLoadFieldsProcessor
{
protected override IEnumerable<IIndexableDataField> GetFields(ContactIndexableLoadFieldsPipelineArgs args)
{
Assert.ArgumentNotNull(args, "args");
var contact = args.Contact;
Assert.IsNotNull(contact, "contact");
//Log.Info("Starting company field aggregator",this);
var list = new List<IIndexableDataField>();
var value = contact.Facets.FirstOrDefault(kvp => kvp.Value is ICustomerLookup).Value;
if (value != null)
{
var companyInfo = (ICustomerLookup) value;
list = new List<IIndexableDataField>
{
new IndexableDataField<string>("contact.company", companyInfo.name)
};
}
return list;
}
}
}
Pipeline
Then patch in this config to add the code above to the ontactindexable.loadfields
pipeline and then the config to add the field. Be sure to notice that the field name contact.company
is both in the pipeline and the config.
<configuration xmlns:patch="http://www.sitecore.net/xmlconfig/">
<sitecore>
<pipelines>
<contactindexable.loadfields>
<processor type="Bonfire.Feature.Kickfire.Analytics.Pipelines.CompanyIndexableLoadFields.LoadFields, Bonfire.Feature.Kickfire.Analytics" />
</contactindexable.loadfields>
</pipelines>
<contentSearch>
<configuration type="Sitecore.ContentSearch.ContentSearchConfiguration, Sitecore.ContentSearch">
<indexes hint="list:AddIndex">
<index id="sitecore_analytics_index" type="Sitecore.ContentSearch.LuceneProvider.LuceneIndex, Sitecore.ContentSearch.LuceneProvider">
<configuration ref="contentSearch/indexConfigurations/defaultLuceneIndexConfiguration">
<fieldMap ref="contentSearch/indexConfigurations/defaultLuceneIndexConfiguration/fieldMap">
<fieldNames hint="raw:AddFieldByFieldName">
<field fieldName="contact.company" storageType="YES" indexType="TOKENIZED" vectorType="WITH_POSITIONS_OFFSETS" boost="1f" emptyString="_EMPTY_" nullValue="_NULL_" type="System.String" settingType="Sitecore.ContentSearch.LuceneProvider.LuceneSearchFieldConfiguration, Sitecore.ContentSearch.LuceneProvider">
<Analyzer type="Sitecore.ContentSearch.LuceneProvider.Analyzers.LowerCaseKeywordAnalyzer, Sitecore.ContentSearch.LuceneProvider" />
</field>
</fieldNames>
</fieldMap>
</configuration>
</index>
</indexes>
</configuration>
</contentSearch>
</sitecore>
</configuration>