Duplicate entries in custom index with syncMaster strategy

Duplicate entries in custom index with syncMaster strategy

If you have tried creating your own custom index with Sitecore 7 you might have run into this issue already. We ran into this issue at work yesterday.

After saving an item it would create another entry in our custom index with the new data but the old entry would still be there. Another entry would be added after each save. After rebuilding the index everything would be fine but that wasn’t really a solution.

A quick google search revealed the cause of our problem. This appears to only be a problem when using the syncMaster update strategy (which updates the index whenever an item is saved in the master database – in contrast to onPublishEndAsync which only updates after a publish).

If you are missing the _uniqueid field in your index configuration, Lucene won’t be able to identify the updated item in the index and instead creates a new entry.

Adding that field to your field names list in your index configuration as shown below should fix the problem.

<fieldNames hint="raw:AddFieldByFieldName">
  <!-- Use <fieldType> instead of <field> if you are using Sitecore 7.0 rev130424 (Initial) -->
  <field fieldName="_uniqueid" 
         storageType="YES" 
         indexType="TOKENIZED" 
         vectorType="NO" 
         boost="1f" 
         type="System.String" 
         settingType="Sitecore.ContentSearch.LuceneProvider.LuceneSearchFieldConfiguration,
                      Sitecore.ContentSearch.LuceneProvider">
     <analyzer type="Sitecore.ContentSearch.LuceneProvider.Analyzers.LowerCaseKeywordAnalyzer,
                     Sitecore.ContentSearch.LuceneProvider" />
  </field>
</fieldNames>

As noted in the comment, if you are using the initial release of Sitecore 7.0 (rev130424) the field element should be named <fieldType> instead of <field> as this was changed in Sitecore 7.0 Update 1 (rev130810).

So, if you are making your own custom index remember to add this field to your index configuration.