Sitecore-使用Solr Index中的ContentSearchManager时未映射索引字段

时间:2018-10-03 14:52:53

标签: c# solr sitecore

  • Sitecore 8.2.6
  • Solr 6.6.2

问题:

无法将生日(任何-参见附件)字段映射到C#模型。全部显示为null(使用returnType="string"时)或01/01/0001(使用returnType="datetime"时)

enter image description here

我正在使用sitecore_analytics_index获取EXM联系人的搜索结果。在查看Solr索引时(通过localhost:8984 / solr),我可以清楚地看到这些字段。

dateofbirthcontact.dateofbirth可以在Fields集合中看到,当查看通过搜索索引返回的BirthdaySearchResultItem列表中的对象时。

我能够正确映射contact.tags_smcontact.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();

        }

1 个答案:

答案 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类。