Back to Basics: Fetch Solr Index Field Name for Sitecore Fields Using Solr FieldNameTranslator
I have been meaning to write this post for a while as I have been using this nifty little feature in all my content search API implementations using the Solr provider. Unsafe: Defining SearchResultItem classes using hard coded Solr fields names for IndexField data annotation attributes as follows: [IndexField("page_title")] public string PageTitle { get; set; } Problems : Cause runtime errors when field names change in Sitecore Hard coded strings, sort of magic values Unmanageable code Safe: Define SearchResultItem classes using strongly typed constants for data annotation attributes as follows: [IndexField(SampleItem.PageTitleSolrIndexFieldName)] public string PageTitle { get; set; } Solution : Compile time errors are way better than runtime errors More manageable code More readable code More dynamic query building Trick : Fetch Solr translated field name values on template fields using FieldNameTranslator and reference them w...