问题:
无法将生日(任何-参见附件)字段映射到C#模型。全部显示为null
(使用returnType="string"
时)或01/01/0001
(使用returnType="datetime"
时)
我正在使用sitecore_analytics_index
获取EXM联系人的搜索结果。在查看Solr索引时(通过localhost:8984 / solr),我可以清楚地看到这些字段。
dateofbirth
和contact.dateofbirth
可以在Fields
集合中看到,当查看通过搜索索引返回的BirthdaySearchResultItem
列表中的对象时。
我能够正确映射contact.tags_sm
和contact.contactid
。
public class BirthdaySearchResultItem : SearchResultItem, IObjectIndexers
{
[IndexField("contact.tags_sm")]
public string[] ContactList { get; set; } // maps correctly
[DataMember]
[IndexField("contact.contactid")]
public virtual Guid ContactId { get; set; } // maps correctly
[DataMember]
[IndexField("contact.dateofbirth")]
public virtual DateTime BirthDay { get; set; } // have tried using "string"
[DataMember]
[IndexField("dateofbirth")]
public virtual DateTime BirthDay1 { get; set; } // have tried using "string"
[DataMember]
[IndexField("contact.dateofbirth_t")]
public virtual DateTime BirthDay2 { get; set; } // have tried using "string"
[DataMember]
[IndexField("contact.dateofbirth_t_en")]
public virtual DateTime BirthDay3 { get; set; } // have tried using "string"
[DataMember]
[IndexField("birthday")]
public virtual DateTime BirthDay4 { get; set; } // have tried using "string"
}
<index id="sitecore_analytics_index">
<configuration ref="contentSearch/indexConfigurations/defaultSolrIndexConfiguration">
<fieldMap>
<fieldNames hint="raw:AddFieldByFieldName">
<field fieldName="contact.contactid" returnType="guid"/>
<field fieldName="contact.dateofbirth" returnType="datetime"/> <!-- have tried "string" as well -->
<field fieldName="dateofbirth" returnType="datetime" />
<field fieldName="contact.dateofbirth_t" returnType="datetime" />
<field fieldName="contact.dateofbirth_t_en" returnType="datetime" />
.... omitted for brevity
var indexName = "sitecore_analytics_index";
var index = ContentSearchManager.GetIndex(indexName);
using (var context = index.CreateSearchContext()) //IContactDetailFacet
{
var predicate = PredicateBuilder.True<BirthdaySearchResultItem>();
var test = context.GetQueryable<BirthdaySearchResultItem>().ToList();
}
答案 0 :(得分:0)
根据您的数据样本,我可以看到您使用前导的“ /”而不是“-”,这很重要,因为Solr对日期使用ISO格式,请参见here。因此,建议您首先将format属性添加到索引定义中,例如:
<field fieldName="contact.dateofbirth" returnType="datetime" format="yyyy-MM-dd'T'HH:mm:ss'Z'" />
然后在模型中使用Sitecore.DateUtil.IsoDateToDateTime()
方法将ISO格式的日期字符串转换为DateTime类。