I'm using 8.2 Update-3 and have deployed it to Azure with Sitecore's ARM template.
I have indexed a page called Event Page
. The Event Page
has a DateTime field called Start Date
on it. It is indexed as an Edm.DateTimeOffset field. I have mapped the index to a custom SearchResultItem called EventItem
. The EventItem
looks like this:
public class EventItem : IndexedItem
{
[IndexField(Constants.IndexFields.Header)]
public string Header { get; set; }
[IndexField(Constants.IndexFields.Content)]
public override string Content { get; set; }
[IndexField(Constants.IndexFields.StartDate)]
public DateTimeOffset StartDate { get; set; }
}
I want to search for all events that has a Start Date
in the future:
using (var context = this.SearchIndexResolver.GetIndex("my_index").CreateSearchContext())
{
var queryable = this.CreateAndInitializeQuery(context);
queryable = queryable.OrderBy(x => x.StartDate);
var dateTime = new DateTimeOffset(DateTime.UtcNow);
queryable = queryable.Where(x => x.StartDate >= dateTime);
var results = queryable.GetResults();
return results;
}
This gives me the search result that I want. If I stick a breakpoint on the results
variable I can see that there are 3 hits which is correct. However, when I use the mapped EventItem
's from the search result I get the following exception:
[NotSupportedException: IndexFieldUtcDateTimeOffsetValueConverter cannot convert from System.DateTime.]
System.ComponentModel.TypeConverter.GetConvertFromException(Object value) +184
System.ComponentModel.TypeConverter.ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, Object value) +8035992
Sitecore.ContentSearch.Azure.Converters.CloudIndexFieldStorageValueFormatter.ReadFromIndexStorageBase(Object indexValue, String fieldName, Type destinationType) +596
Sitecore.ContentSearch.Azure.Converters.CloudIndexFieldStorageValueFormatter.ReadFromIndexStorage(Object indexValue, String fieldName, Type destinationType) +234
Sitecore.ContentSearch.DocumentTypeMapInfo.SetProperty(Object target, String propertyName, String documentFieldName, Object value) +801
Sitecore.ContentSearch.DefaultDocumentMapper`1.MapFieldValuesToType(IDictionary`2 fieldValues, TElement result, DocumentTypeMapInfo documentTypeMapInfo) +1789
Sitecore.ContentSearch.DefaultDocumentMapper`1.MapToType(TDocument document, SelectMethod selectMethod, IEnumerable`1 virtualFieldProcessors, IEnumerable`1 executionContexts, SearchSecurityOptions securityOptions) +760
Sitecore.ContentSearch.Azure.Query.CloudSearchResults`1.Map(IDictionary`2 fields) +813
Sitecore.ContentSearch.Azure.Query.<GetSearchHits>d__13.MoveNext() +366
I have tried changing my EventItem
type to a DateTime instead of DateTimeOffset and query with a DateTime.Now but that gives me a "Specified cast is not valid" exception when it comes the the GetResults()
method:
[InvalidCastException: Specified cast is not valid.]
Sitecore.ContentSearch.Azure.Query.Operations.PrepareValue(Object value, String edmType) +201
Sitecore.ContentSearch.Azure.Query.CloudQueryMapper.HandleGreaterThanOrEqual(String srcFieldName, Object value, Single boost) +244
Sitecore.ContentSearch.Azure.Query.CloudQueryMapper.HandleGreaterThanOrEqual(GreaterThanOrEqualNode node, CloudQueryMapperState state) +147
Sitecore.ContentSearch.Azure.Query.CloudQueryMapper.HandleWhere(WhereNode node, CloudQueryMapperState mappingState) +37
Sitecore.ContentSearch.Azure.Query.CloudQueryMapper.MapQuery(IndexQuery query) +64
Sitecore.ContentSearch.Linq.Parsing.GenericQueryable`2.GetQuery(Expression expression) +271
Sitecore.ContentSearch.Linq.Parsing.GenericQueryable`2.Execute(Expression expression) +37
Sitecore.ContentSearch.Linq.QueryableExtensions.GetResults(IQueryable`1 source) +334
So how do I fix this? What am I doing wrong?