Quantcast
Channel: briancaos – Brian Pedersen's Sitecore and .NET Blog
Viewing all articles
Browse latest Browse all 276

Sitecore ComputedIndexField extends your SOLR index

$
0
0

The Sitecore SOLR index is your quick access to Sitecore content. And you can extend this access by adding computed index fields. This is a way of enriching your searches with content that is not part of your Sitecore templates, but is needed when doing quick searches.

THE SIMPLE SCENARIO: GET A FIELD FROM THE PARENT ITEM

This is a classic scenario, where the content in Sitecore is organized in a hierarchy, for example by Category/Product, and you need to search within a certain category:

Category/Product Hierarcy

In order to make a direct search for products within a certain category, you will need to extend the product template with the category ID, so you can do a search in one take. So lets add the category ID to the product template SOLR search using a computed index field.

STEP 1: THE CONFIGURATION:

<?xml version="1.0" encoding="utf-8"?>
<configuration xmlns:patch="http://www.sitecore.net/xmlconfig/" xmlns:role="http://www.sitecore.net/xmlconfig/role/" xmlns:env="http://www.sitecore.net/xmlconfig/env/">
  <sitecore>
    <contentSearch>
      <indexConfigurations>
        <defaultSolrIndexConfiguration>
          <fieldMap>
            <fieldNames hint="raw:AddFieldByFieldName">
              <field fieldName="CategoryId" returnType="guid" />
            </fieldNames>
          </fieldMap>
          <documentOptions>
            <fields hint="raw:AddComputedIndexField">          
              <field fieldName="CategoryId" returnType="string">MyCode.ComputedIndexFields.CategoryId, MyDll</field>
            </fields>
          </documentOptions>
        </defaultSolrIndexConfiguration>
      </indexConfigurations>
    </contentSearch>
  </sitecore>
 </configuration>  

The configuration is a 2 step process. The “fieldMap” maps field names (CategoryId in this case) to output types, in this case a GUID. The documentOptions maps the field name to a piece of code that can compute the field value. Please note that the documentOptions claims that the output type is a string, not a Guid. But don’t worry, as long as our code returns a Guid, everything will be fine.

STEP 2: THE CODE

using Sitecore.ContentSearch;
using Sitecore.ContentSearch.ComputedFields;
using Sitecore.Data.Items;

namespace MyCode.ComputedIndexFields
{
  public class CategoryId : IComputedIndexField
  {
    public object ComputeFieldValue(IIndexable indexable)
    {
      Item item = indexable as SitecoreIndexableItem;

      if (item == null)
        return null;

      if (item.TemplateName != "Product")
        return null;

      Item categoryItem = item.Parent;
      if (categoryItem.TemplateName != "Category")
        return null;

      return categoryItem.ID.ToGuid();
    }

    public string FieldName
    {
      get;
      set;
    }

    public string ReturnType
    {
      get;
      set;
    }
  }
}

The code is equally straight forward. If the code returns NULL, no value will be added.

The code first checks to see if the item being indexed is a product. If not, the code is skipped. Also, if the parent item is not a category, we also skip the code. Only if the item is a product and the parent is a category, the category ID is added to the index.

You will need to re-index your SOLR index. When the index is updated, you will find a “CategoryId” field on all of the “Product” templates in the SOLR index.

MORE TO READ:


Viewing all articles
Browse latest Browse all 276

Trending Articles