Index custom contact facets

Current version: 8.2

You can create custom contact facets to include information that your organization wants to track, for example, addresses, country, or email addresses. To index these custom contact facets during the aggregation process, you must extend the contactindexable.loadfields pipeline.

The contactindexable.loadfields pipeline is used by the Sitecore.ContentSearch.Analytics.Models.ContactIndexable field type to load fields that are available for indexing. Custom processors in the contactindexable.loadfields pipeline must inherit from the Sitecore.ContentSearch.Analytics.Pipelines.ContactIndexableLoadFields.ContactIndexableLoadFieldsProcessor base class.

Depending on whether you use Lucene or Solr, you must configure the Sitecore.ContentSearch.Lucene.Index.Analytics.config or Sitecore.ContentSearch.Solr.Index.Analytics.config files to define which indexable fields are added to the appropriate indexes.

Note

This functionality is only available from Sitecore 8.1 update 3 and later.

To index a custom contact facet:

  1. Implement a custom processor that extends the contactindexable.loadfields pipeline to add data from the custom facet to the collection of contact indexable fields:

    RequestResponse
    using System.Collections.Generic;
    using Sitecore.ContentSearch;
    using Sitecore.ContentSearch.Analytics.Pipelines.ContactIndexableLoadFields;
    using Sitecore.Diagnostics;
    namespace Sitecore.IndexingCustomFacets
    {
      public class LoadFields : ContactIndexableLoadFieldsProcessor
      {
        protected override IEnumerable<IIndexableDataField> GetFields(ContactIndexableLoadFieldsPipelineArgs args)
        {
          Assert.ArgumentNotNull(args, "args");
          var contact = args.Contact;
          Assert.IsNotNull(contact, "contact");
          var fields = new List<IIndexableDataField>();
          var customFacet = contact.GetFacet<ICustomContactFacet>("CustomContactFacetName");
          if (customFacet != null)
          {
            fields.Add(new IndexableDataField<bool>("contact.CustomFieldName", customFacet.CustomField));
          }
          return fields;
        }
      }
    }
  2. In the \App_Config\Include folder, create a new configuration include file that adds the new processor to the contacindexable.loadfields pipeline:

    RequestResponse
    <?xml version="1.0" encoding="utf-8" ?>
    <configuration xmlns:patch="http://www.sitecore.net/xmlconfig/"
      <sitecore>
        <pipelines>
          <contactindexable.loadfields>
            <processor type="Sitecore.IndexingCustomFacets.LoadFields, Sitecore.IndexingCustomFacets" />
          </contactindexable.loadfields>
        </pipelines>
      </sitecore>
    </configuration>
  3. Copy the new configuration file to the App_Config\Include folder of your website.

  4. Configure the analytics index to store the new field in the index.

    • If you use Solr, create the new configuration include file to add the new field under the <fieldNames hint="raw:AddFieldByFieldName"> node defined in the Sitecore.ContentSearch.Solr.Index.Analytics.config file.

      For example:

      RequestResponse
      <?xml version="1.0" encoding="utf-8" ?>
      <configuration xmlns:patch="http://www.sitecore.net/xmlconfig/"
        <sitecore>
          <contentSearch>
            <configuration>
              <indexes>
                <index id="sitecore_analytics_index">
                  <configuration>
                    <fieldMap>
                      <fieldNames>
                        <field fieldName="contact.CustomFieldName" returnType="bool" />
                      </fieldNames>
                    </fieldMap>
                  </configuration>
                </index>
              </indexes>
            </configuration>
          </contentSearch>
        </sitecore>
      </configuration>
    • If you use Lucene, create the new configuration include file to add the new field under the <fieldNames hint="raw:AddFieldByFieldName"> node defined in in the Sitecore.ContentSearch.Lucene.Index.Analytics.config file.

      For example:

      RequestResponse
      <?xml version="1.0" encoding="utf-8" ?>
      <configuration xmlns:patch="http://www.sitecore.net/xmlconfig/"
       <sitecore>
         <contentSearch>
           <configuration>
             <indexes>
               <index id="sitecore_analytics_index">
                 <configuration>
                   <fieldMap>
                     <fieldNames>
                       <field fieldName="contact.CustomFieldName" storageType="YES" indexType="TOKENIZED" vectorType="NO" boost="1f" type="System.Boolean" settingType="Sitecore.ContentSearch.LuceneProvider.LuceneSearchFieldConfiguration, Sitecore.ContentSearch.LuceneProvider" />
                     </fieldNames>
                   </fieldMap>
                 </configuration>
               </index>
             </indexes>
           </configuration>
         </contentSearch>
       </sitecore>
      </configuration>

Do you have some feedback for us?

If you have suggestions for improving this article,