Appending to the _content field in Sitecore Search 7.2 and 7.5

The _content field in Lucene for Sitecore is basically the full text index. If you index things like PDFs, Word documents, etc., then the results of the IFilter get stored in the _content field.

You can add computed fields into the _content field simply by specifying them with the name ‘_content’. However, I found some differences with how patch files behave. 

First off, let’s look at a simple computed field:

using Sitecore.ContentSearch;
using Sitecore.ContentSearch.ComputedFields;
using Sitecore.Data.Items;
namespace Example.Indexing
{
public class FullNameComputedField : IComputedIndexField
{
public string FieldName { get; set; }
public string ReturnType { get; set; }
public object ComputeFieldValue(IIndexable indexable)
{
Item item = indexable as SitecoreIndexableItem;
if (item == null)
{
return null;
}
if (item.TemplateID == TemplateIDs.ContactTemplate)
{
string fullName = string.Format("{0} {1}", item["Name"], item["Surname"]);
return fullName.ToString();
}
return null;
}
}
}

This example generates a ‘full name’ from the ‘name’ and ‘surname’ fields. Pretty obvious really, and there are lots of examples out there.

Next, I use a patch file to add the computed field definition into my configuration. In 7.5, this would be like:

<?xml version="1.0" encoding="utf-8" ?>
<configuration xmlns:patch="http://www.sitecore.net/xmlconfig/" xmlns:set="http://www.sitecore.net/xmlconfig/set/">
<sitecore>
<contentSearch>
<indexConfigurations>
<defaultLuceneIndexConfiguration type="Sitecore.ContentSearch.LuceneProvider.LuceneIndexConfiguration, Sitecore.ContentSearch.LuceneProvider">
<fields hint="raw:AddComputedIndexField">
<field fieldName="_content">Example.Indexing.FullNameComputedField, Example</field>
</fields>
</defaultLuceneIndexConfiguration>
</indexConfigurations>
</contentSearch>
</sitecore>
</configuration>

However, in 7.2 this didn’t work properly. Instead of adding another field to write into content, it would change/update the existing, built-in definition for _content.

In 7.2 it needs to be:

<fields hint="raw:AddComputedIndexField">
<field fieldName="_content" type="Example.Indexing.FullNameComputedField, Example" />
</fields>

That’s just the central bit, but this adds another field to write, rather that overwriting it. I think this is due to the ‘type’ attribute being present, but it is definitely a difference between 7.2 and 7.5

 

Advertisements
Appending to the _content field in Sitecore Search 7.2 and 7.5

2 thoughts on “Appending to the _content field in Sitecore Search 7.2 and 7.5

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s