Thursday, November 9, 2017

Sitecore SOLR custom fields are not appearing in the index

After setting up a custom index configuration for SOLR in a Sitecore 9 instance, I noticed that the fields in which I had added to the raw:AddComputedIndexField were not present in the index (when viewed under schema in the SOLR admin site).

The index definition XML was valid and the index would built, yet my fields were not present.

After a bit of playing around I notice that the ordering of documentOptions and fieldMap mattered. I originally had them in this order:
<configuration ref="contentSearch/indexConfigurations/defaultSolrIndexConfiguration">
 <fieldMap ref="contentSearch/indexConfigurations/defaultSolrIndexConfiguration/fieldMap">
   <fieldNames hint="raw:AddFieldByFieldName">
  <field fieldName="title" returnType="text" storageType="YES" indexType="TOKENIZED" vectorType="NO" boost="1f" />
  <field fieldName="description" returnType="text" storageType="YES" indexType="TOKENIZED" vectorType="NO" boost="1f" />
   </fieldNames>
 </fieldMap>
 <documentOptions ref="contentSearch/indexConfigurations/defaultSolrIndexConfiguration/documentOptions">
   <include hint="list:AddIncludedTemplate">
  <Page>{49E6B85F-02F4-405F-923F-66761933E80F}</Page>
   </include>
   <fields hint="raw:AddComputedIndexField">
  <field fieldName="MyComputedField" returnType="string">Sitecore.Base.Search.MyComputedImageField, Sitecore.Base</field>
   </fields>
 </documentOptions>
</configuration>
Which was incorrect, however swapping to the following order worked.
<configuration ref="contentSearch/indexConfigurations/defaultSolrIndexConfiguration">
 <documentOptions ref="contentSearch/indexConfigurations/defaultSolrIndexConfiguration/documentOptions">
   <include hint="list:AddIncludedTemplate">
  <Page>{49E6B85F-02F4-405F-923F-66761933E80F}</Page>
   </include>
   <fields hint="raw:AddComputedIndexField">
  <field fieldName="MyComputedField" returnType="string">Sitecore.Base.Search.MyComputedImageField, Sitecore.Base</field>
   </fields>
 </documentOptions>
 <fieldMap ref="contentSearch/indexConfigurations/defaultSolrIndexConfiguration/fieldMap">
   <fieldNames hint="raw:AddFieldByFieldName">
  <field fieldName="title" returnType="text" storageType="YES" indexType="TOKENIZED" vectorType="NO" boost="1f" />
  <field fieldName="description" returnType="text" storageType="YES" indexType="TOKENIZED" vectorType="NO" boost="1f" />
   </fieldNames>
 </fieldMap>
</configuration>
It was a strange one to get working, so hopefully this post is able to help anyone else that comes across this error.

No comments:

Post a Comment