An Independent Sitecore Learning Resource

Menu

Lucene to Solr: Fixing a Field Map Error

Lucene to Solr: Fixing a Field Map Error

Author

Brandon Bruno

Published

November 10, 2020

Tags

A Simple Conversion Gone Wrong

While upgrading a Sitecore 8 implementation with Lucene search to Sitecore 10 with Solr, several custom search indexes had configuration that required updates. Lucene and Solr have slightly different <contentSearch> node configurations.

After converting a section of custom fields, I wound up with this configuration (this example simplified for clarity):

<indexConfigurations>
	<defaultSolrIndexConfiguration type="Sitecore.ContentSearch.SolrProvider.SolrIndexConfiguration, Sitecore.ContentSearch.SolrProvider">
		<fieldMap ref="contentSearch/indexConfigurations/defaultSolrIndexConfiguration/fieldMap">
			<fieldNames hint="raw:AddFieldByFieldName">
				<field fieldName="productEstimateValue" returnType="System.Double" />
				<field fieldName="actualSaleValue" returnType="System.Double" />
			</fieldNames>
		</fieldMap>
	</defaultSolrIndexConfiguration>
</indexConfigurations>

Upon loading Sitecore, I was greeted by this yellow screen of death error:

[KeyNotFoundException: The given key was not present in the dictionary.]
   System.ThrowHelper.ThrowKeyNotFoundException() +38
   System.Collections.Generic.Dictionary`2.get_Item(TKey key) +54
   Sitecore.ContentSearch.SolrProvider.SolrFieldMap.AddFieldByFieldName(XmlNode configNode) +590

So what's going on?

An Issue of Bad Naming

The error message above isn't very specific to the actual problem. It turned out that I was using a return type that wasn't defined in the field map, hence the dictionary key exception.

I was using System.Double for my return type:

<field fieldName="productEstimateValue" returnType="System.Double" />

I should have used just double:

<field fieldName="productEstimateValue" returnType="double" />

Verify With the Field Map

The defaultSolrIndexConfiguration node contains an explicit reference to a specific field map for that configuration to utilize:

<fieldMap ref="contentSearch/indexConfigurations/defaultSolrIndexConfiguration/fieldMap">

Here's a sample of the field map from the Sitecore.ContentSearch.Solr.DefaultIndexConfiguration.config file where the referenced field map can be found:

<fieldMap type="Sitecore.ContentSearch.SolrProvider.SolrFieldMap, Sitecore.ContentSearch.SolrProvider">
	<typeMatches hint="raw:AddTypeMatch">
		<typeMatch typeName="string" type="System.String" fieldNameFormat="{0}_s" settingType="Sitecore.ContentSearch.SolrProvider.SolrSearchFieldConfiguration, Sitecore.ContentSearch.SolrProvider" />
		<typeMatch typeName="int" type="System.Int32" fieldNameFormat="{0}_tl" settingType="Sitecore.ContentSearch.SolrProvider.SolrSearchFieldConfiguration, Sitecore.ContentSearch.SolrProvider" />
		<typeMatch typeName="double" type="System.Double" fieldNameFormat="{0}_td" settingType="Sitecore.ContentSearch.SolrProvider.SolrSearchFieldConfiguration, Sitecore.ContentSearch.SolrProvider" />
	</typeMatches>
</fieldMap>

That verified my issue: the System.Double field map uses just double for its type name, which is what I should have used in the first place on each of my custom field configurations.

If you're converting Sitecore search configuration from Lucene to Solr, remember to update the return type of custom fields to match those from the relevant field map.

Further Reading

The following article was helpful in solving my original issue:

Do you have questions, comments, or corrections for this post? Find me on Twitter: @SitecoreSpark