LibrarySites.Banner

Sitecore 7 Search Tips: Computed Fields

Computed index fields are allow you to perform additional processing before adding data to an index. For example, you may want to store the contents of a droplink field (the raw value being a GUID) as the target item's name, or a particular field value from the target item. This short blog post covers a couple of tips on how to set up and configure your computed field.

Untokenized computed field

In .config, a computed field looks like this:

<field fieldName="summaryimagecomputed"             storageType="yes"  indexType="untokenized">Training.Utilities.BaseCore.Search.ComputedFieldExample,Training.Utilities</field>

Notice that indexType is set to untokenized - you would expect that your computed field is now indexed as a single string, but it will actually still appear as tokenized (split up, noise words and separators removed) in your index.

If you are storing a value for display or faceting purposes (e.g. the name of a city, which you would want saved as 'Los Angeles', not 'Los' and 'Angeles' separately), you want it to be stored as untokenized. To do this, add your computed field as an entry in the <fieldNames hint="raw:AddFieldByFieldName"> section of the config (in my case,Sitecore.ContentSearch.Lucene.DefaultIndexConfiguration.config)
alongside the regular, non-computed fields and specify an Analyzer:

<fieldNames hint="raw:AddFieldByFieldName">
  <fieldfieldName="summaryimagecomputed"            storageType="YES" indexType="TOKENIZED"    vectorType="NO" boost="1f" type="System.String" settingType="Sitecore.ContentSearch.LuceneProvider.LuceneSearchFieldConfiguration, Sitecore.ContentSearch.LuceneProvider">
    <Analyzer type="Sitecore.ContentSearch.LuceneProvider.Analyzers.LowerCaseKeywordAnalyzer, Sitecore.ContentSearch.LuceneProvider" />
  </field>
</fieldNames>

The computed value will now be indexed as untokenized.

Return null instead of String.Empty

When creating your own computed or virtual field, don't return String.Empty unless it is important that your index contains an empty string (e.g. you are storing form submissions and String.Empty indicates that a choice was made to omit information, whilst null might mean that there was never an opportunity to enter the information in the first place) - that value will actually be indexed, and you might end up with 12,000 entries for 'nothing' that take up space in your index. If you return null, nothing will be added to your index.