I have a class (SBJSearchItem) that inherits SearchResultItem that I'm using with the ContentSearchManager. The problem is that some of my SBJSearchItem properties are not populating. I'm using Azure Search as part of Sitecore Azure PaaS.
public class SBJSearchItem : SearchResultItem
{
#region Public Properities
// Type is working. This stores an ID and comes back as a string, which is fine.
[IndexField("type")]
public string Type { get; set; }
// Authors is working. This stores a list of IDs.
[IndexField("authors")]
public string Authors { get; set; }
// Title is working. It stores a string.
[IndexField("title")]
public string Title { get; set; }
/*
Publish Date does not work. This is just a date/time field in Sitecore.
It is stored as a string type in the index. I made it nullable because
I get an error that says that it cannot convert null to DataTime.
I believe that's because it also pulls the template which doesn't have
a date set.
*/
[IndexField("publish_date")]
public DateTime? PublishDate { get; set; }
// Section does not work. It is a computed field that I set to a string.
[IndexField("section")]
public string Section { get; set; }
/*
DateSearch does not work. This is a computed field. It is stored as a
DateTimeOffset type. I get a DateTime.MinValue.
*/
// EDIT: Added this per comments
[TypeConverter(typeof(IndexFieldDateTimeValueConverter))]
[IndexField("date_search")]
public DateTime DateSearch { get; set; }
#endregion
}
Edit 4 Apparently getting the index in the manner that I was doesn't work the same as getting the index specifically by name.
Here's an example of how I'm executing the query:
// Be careful if you use the commented lines here:
//var indexableItem = new SitecoreIndexableItem(currentStory);
//var index = ContentSearchManager.GetIndex(indexableItem);
// This apparently is DIFFERENT than the previous index declaration above
var index = ContentSearchManager.GetIndex("sitecore_master_index");
using (var context = index.CreateSearchContext())
{
var query = context.GetQueryable<SBJSearchItem>()
.Where(i => i.TemplateId == SitecoreItemIds.BLOG_STORY_TEMPLATE
&& i.ItemId == currentStory.ID)
.ToList();
}
The query works except those few properties that I mentioned above are null in my list of items (well one item in this case).
Here's my patch that adds the computed fields into the index. I have verified that the values are actually in the index.
<index id="sitecore_master_index">
<configuration>
<!-- EDIT: Added per comments -->
<fieldMap type="Sitecore.ContentSearch.Azure.FieldMaps.CloudFieldMap, Sitecore.ContentSearch.Azure">
<fieldNames hint="raw:AddFieldByFieldName">
<field type="System.String" fieldName="section" cloudFieldName="section" boost="1f" settingType="Sitecore.ContentSearch.Azure.CloudSearchFieldConfiguration, Sitecore.ContentSearch.Azure"/>
<field type="System.DateTime" fieldName="date_search" cloudFieldName="date_search" boost="1f" format="yyyy-MM-ddTHH:mm:ssZ" settingType="Sitecore.ContentSearch.Azure.CloudSearchFieldConfiguration, Sitecore.ContentSearch.Azure"/>
</fieldNames>
</fieldMap>
<!-- END EDIT -->
<fields hint="raw:AddComputedIndexField">
<field fieldName="section" storageType="yes" indexType="tokenized">
SBJSitecore.Components.Editorial.SectionComputedField,SBJSitecore
</field>
<field fieldName="date_search" storageType="yes" indexType="tokenized" type="System.DateTime">
SBJSitecore.Components.Editorial.DateSearchComputedField,SBJSitecore
</field>
</fields>
</configuration>
</index>
EDIT 2
Per comments added my Computed fields into the Map. Reindex and tried again. Still not returning
<cloudTypeMapper type="Sitecore.ContentSearch.Azure.Schema.CloudSearchTypeMapper, Sitecore.ContentSearch.Azure" patch:source="Sitecore.ContentSearch.Azure.Index.SectionField.config">
<maps hint="raw:AddMap" patch:source="Sitecore.ContentSearch.Azure.Index.SectionField.config">
<map type="System.Char" cloudType="Edm.String"/>
<map type="System.String" cloudType="Edm.String"/>
<map type="System.Guid" cloudType="Edm.String"/>
<map type="System.Int32" cloudType="Edm.Int32"/>
<map type="System.Int64" cloudType="Edm.Int64"/>
<map type="System.Double" cloudType="Edm.Double"/>
<map type="System.Single" cloudType="Edm.Double"/>
<map type="System.Boolean" cloudType="Edm.Boolean"/>
<map type="System.DateTime" cloudType="Edm.DateTimeOffset"/>
<map type="System.DateTimeOffset" cloudType="Edm.DateTimeOffset"/>
<map type="System.String[]" cloudType="Collection(Edm.String)"/>
<map type="System.Guid[]" cloudType="Collection(Edm.String)"/>
<map type="System.Int32[]" cloudType="Collection(Edm.String)"/>
<map type="System.Object[]" cloudType="Collection(Edm.String)"/>
<map type="Sitecore.ContentSearch.IIndexableId, Sitecore.ContentSearch" cloudType="Edm.String"/>
<map type="Sitecore.ContentSearch.IIndexableUniqueId, Sitecore.ContentSearch" cloudType="Edm.String"/>
<map type="Sitecore.Data.ID, Sitecore.Kernel" cloudType="Edm.String"/>
<map type="Sitecore.Data.ItemUri, Sitecore.Kernel" cloudType="Edm.String"/>
<map type="Sitecore.ContentSearch.SitecoreItemId, Sitecore.ContentSearch" cloudType="Edm.String"/>
<map type="Sitecore.ContentSearch.SitecoreItemUniqueId, Sitecore.ContentSearch" cloudType="Edm.String"/>
<map type="Sitecore.Marketing.Taxonomy.Model.TaxonUri, Sitecore.Marketing.Taxonomy" cloudType="Edm.String"/>
<map type="Sitecore.Marketing.Definitions.TaxonomicClassification, Sitecore.Marketing" cloudType="Edm.String"/>
<!-- Patched these values in -->
<map type="SBJSitecore.Components.Editorial.SectionComputedField,SBJSitecore" cloudType="Edm.String" patch:source="Sitecore.ContentSearch.Azure.Index.SectionField.config"/>
<map type="SBJSitecore.Components.Editorial.DateSearchComputedField,SBJSitecore" cloudType="Edm.DateTimeOffset" patch:source="Sitecore.ContentSearch.Azure.Index.SectionField.config"/>
<!-- End Patch -->
</maps>
</cloudTypeMapper>
Snippit of the index:
"value": [
{
"date_search": "2017-02-14T17:23:00Z",
"publish_date": "20170214T122300",
}
]
EDIT
I made a couple of edits per comments. Neither of these changes seem to have any impact. I am in touch with Sitecore support on this issue.
I also noticed that the CreatedDate & Updated fields are DateTime.MinValue on my search results (var query
above).