1

Tip: Enabling custom Sitecore fields with Solr

Recently I was building a new custom field for Sitecore which stored in the same manner as any of the multilist fields (a list of Sitecore ID’s). One of the tasks with getting this custom field up and running was to get it hooked up with the new Sitecore 7 Solr search implementation.

To ensure that your custom field correctly stores in the search index you’ll need to make sure the following config entries are made.

App_Config\FieldTypes.config – add your custom field name and point it to the correct type. I was adding a “tags” field so I pointed it to the MultilistField type in the Sitecore.Kernel assembly.

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
	<!-- Defines the releation between a field type name, e.g. "lookup", and an implementation class -->

	<!-- List Types -->
	<fieldType name="tags" type="Sitecore.Data.Fields.MultilistField,Sitecore.Kernel" resizable="true" />
</configuration>

App_Config\Include\Sitecore.ContentSearch.Solr.Indexes.config

  1. Add your custom field to the <fieldTypes> node and point it to the matching typeMatch. In my case I entered the same typeMatch used by the multilist (stringCollection)
  2. Add your custom field to the <mapFieldByTypeName> node (inside the <FieldReaders> node)
<configuration xmlns:patch="http://www.sitecore.net/xmlconfig/">
	<sitecore>
		<contentSearch>
			<configuration type="Sitecore.ContentSearch.SolrProvider.SolrSearchConfiguration, Sitecore.ContentSearch.SolrProvider">
				<DefaultIndexConfiguration type="Sitecore.ContentSearch.SolrProvider.SolrIndexConfiguration, Sitecore.ContentSearch.SolrProvider">
					<fieldMap type="Sitecore.ContentSearch.SolrProvider.SolrFieldMap, Sitecore.ContentSearch.SolrProvider">
						<!-- FIELD TYPE MAPPING
							 This allows you to map a field type in Sitecore to a type in the index.
							 USAGE: When you add new field types to Sitecore, add the mappings here so they work through the Linq Layer
						 -->
						<fieldTypes hint="raw:AddFieldByFieldTypeName">
							<fieldType fieldTypeName="tags" returnType="stringCollection" />
						</fieldTypes>
					</fieldMap>
					<!-- SITECORE FIELDTYPE MAP
					   This maps a field type by name to a Strongly Typed Implementation of the field type e.g. html maps to HTMLField
					-->
					<FieldReaders type="Sitecore.ContentSearch.FieldReaders.FieldReaderMap, Sitecore.ContentSearch">
						<mapFieldByTypeName hint="raw:AddFieldReaderByFieldTypeName">
							<fieldReader fieldTypeName="tags"    fieldReaderType="Sitecore.ContentSearch.FieldReaders.MultiListFieldReader, Sitecore.ContentSearch" />
						</mapFieldByTypeName>
					</FieldReaders>
				</DefaultIndexConfiguration>
			</configuration>
		</contentSearch>
	</sitecore>
</configuration>

Jason Bert