Sitecore如何解决错误来自' System.String'的无效演员表到' Sitecore.Data.ID'在Sitecore搜索中

时间:2015-09-20 14:41:59

标签: sitecore sitecore7.2 sitecore-lucene

我正在使用sitecore Search API,但我收到异常'Exception Details: System.InvalidCastException: Invalid cast from 'System.String' to 'Sitecore.Data.ID'.'在这一行:foreach (var result in query)。我使用以下代码:

using (var context = ContentSearchManager.GetIndex("my_text_index").CreateSearchContext())
{
IQueryable<Person> query = context.GetQueryable<Person>().Where(p=> p.Firstname.Equals("John"));

foreach(result in query) // I am getting the exception here on this line.
{
  lbltest.Text = result.Name;
}

}

这是班级&#34; Person&#34;

public class Person : SearchResultItem
{
    [IndexField("firstname_t")]
    public string Firstname { get; set; }
    [IndexField("surname_t")]
    public string Surname { get; set; }
}

这是堆栈跟踪:

[InvalidCastException: Invalid cast from 'System.String' to 'Sitecore.Data.ID'.]
   System.Convert.DefaultToType(IConvertible value, Type targetType, IFormatProvider provider) +14116726
   System.Convert.ChangeType(Object value, Type conversionType, IFormatProvider provider) +14116814
   Sitecore.ContentSearch.Converters.IndexFieldStorageValueFormatter.ReadFromIndexStorage(Object indexValue, Type destinationType) +609

[InvalidCastException: Could not convert value of type System.String to destination type Sitecore.Data.ID: Invalid cast from 'System.String' to 'Sitecore.Data.ID'.]
   Sitecore.ContentSearch.Converters.IndexFieldStorageValueFormatter.ReadFromIndexStorage(Object indexValue, Type destinationType) +869
   Sitecore.ContentSearch.DocumentTypeMapInfo.SetProperty(Object target, String propertyName, String documentFieldName, Object value) +172

[InvalidCastException: Could not map index document field to property "ItemId" on type Starco.ISK.Website.Search.PersonSearchItem : Could not convert value of type System.String to destination type Sitecore.Data.ID: Invalid cast from 'System.String' to 'Sitecore.Data.ID'.]
   Sitecore.ContentSearch.DocumentTypeMapInfo.SetProperty(Object target, String propertyName, String documentFieldName, Object value) +360
   Sitecore.ContentSearch.DefaultDocumentMapper`1.MapFieldValuesToType(IDictionary`2 fieldValues, TElement result, DocumentTypeMapInfo documentTypeMapInfo) +792
   Sitecore.ContentSearch.LuceneProvider.DefaultLuceneDocumentTypeMapper.ReadDocumentFields(Document document, IEnumerable`1 fieldNames, DocumentTypeMapInfo documentTypeMapInfo, IEnumerable`1 virtualFieldProcessors, TElement result) +1249
   Sitecore.ContentSearch.DefaultDocumentMapper`1.MapToType(TDocument document, SelectMethod selectMethod, IEnumerable`1 virtualFieldProcessors, SearchSecurityOptions securityOptions) +283
   Sitecore.ContentSearch.LuceneProvider.<GetSearchResults>d__8.MoveNext() +1577
   Starco.ISK.Website.Search.Output.Page_Load(Object sender, EventArgs e) in e:\IIS Data\Starco_intra.starco.com\Website\Search\Output.ascx.cs:76
   System.Web.UI.Control.LoadRecursive() +71
   System.Web.UI.Control.LoadRecursive() +190
   System.Web.UI.Control.LoadRecursive() +190
   System.Web.UI.Control.LoadRecursive() +190
   System.Web.UI.Control.LoadRecursive() +190
   System.Web.UI.Control.LoadRecursive() +190
   System.Web.UI.Control.LoadRecursive() +190
   System.Web.UI.Control.LoadRecursive() +190
   System.Web.UI.Control.LoadRecursive() +190
   System.Web.UI.Control.LoadRecursive() +190
   System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +3178

索引的配置文件:

 <configuration xmlns:patch="http://www.sitecore.net/xmlconfig/">
  <sitecore>
    <contentSearch>
      <configuration type="Sitecore.ContentSearch.LuceneProvider.LuceneSearchConfiguration, Sitecore.ContentSearch.LuceneProvider">
        <indexes hint="list:AddIndex"> 
          <index id="my_text_Index" type="Sitecore.ContentSearch.LuceneProvider.LuceneIndex, Sitecore.ContentSearch.LuceneProvider">
            <param desc="name">$(id)</param>
            <param desc="folder">$(id)</param>
            <param desc="propertyStore" ref="contentSearch/databasePropertyStore" param1="$(id)" />
            <Configuration ref="profileSearch/configuration" />
            <strategies hint="list:AddStrategy">
              <strategy ref="contentSearch/indexUpdateStrategies/onPublishEndAsync" />
            </strategies>
            <commitPolicyExecutor type="Sitecore.ContentSearch.CommitPolicyExecutor, Sitecore.ContentSearch">
              <policies hint="list:AddCommitPolicy">
                <policy type="Sitecore.ContentSearch.TimeIntervalCommitPolicy, Sitecore.ContentSearch" />
              </policies>
            </commitPolicyExecutor>
            <locations hint="list:AddCrawler">
              <crawler type="Sitecore.ContentSearch.SitecoreItemCrawler, Sitecore.ContentSearch">
                <Database>master</Database>
                <Root>/sitecore/content/Intranet</Root> 
              </crawler>
            </locations>
          </index>
        </indexes>
      </configuration>
    </contentSearch>
  </sitecore>
</configuration>

以下是showconfig.aspx页面sitecore / admin / showconfig.aspx:

<indexFieldStorageValueFormatter type="Sitecore.ContentSearch.LuceneProvider.Converters.LuceneIndexFieldStorageValueFormatter, Sitecore.ContentSearch.LuceneProvider">
<converters hint="raw:AddConverter">
<converter handlesType="System.Guid" typeConverter="Sitecore.ContentSearch.Converters.IndexFieldGuidValueConverter, Sitecore.ContentSearch"/>
<converter handlesType="Sitecore.Data.ID, Sitecore.Kernel" typeConverter="Sitecore.ContentSearch.Converters.IndexFieldIDValueConverter, Sitecore.ContentSearch"/>
<converter handlesType="Sitecore.Data.ShortID, Sitecore.Kernel" typeConverter="Sitecore.ContentSearch.Converters.IndexFieldShortIDValueConverter, Sitecore.ContentSearch"/>
<converter handlesType="System.Boolean" typeConverter="Sitecore.ContentSearch.LuceneProvider.Converters.IndexFieldBooleanValueConverter, Sitecore.ContentSearch.LuceneProvider"/>
<converter handlesType="System.DateTime" typeConverter="Sitecore.ContentSearch.LuceneProvider.Converters.IndexFieldDateTimeValueConverter, Sitecore.ContentSearch.LuceneProvider"/>
<converter handlesType="System.DateTimeOffset" typeConverter="Sitecore.ContentSearch.Converters.IndexFieldDateTimeOffsetValueConverter, Sitecore.ContentSearch"/>
<converter handlesType="System.TimeSpan" typeConverter="Sitecore.ContentSearch.Converters.IndexFieldTimeSpanValueConverter, Sitecore.ContentSearch"/>
<converter handlesType="Sitecore.ContentSearch.SitecoreItemId, Sitecore.ContentSearch" typeConverter="Sitecore.ContentSearch.Converters.IndexFieldSitecoreItemIDValueConvertor, Sitecore.ContentSearch">
<param type="Sitecore.ContentSearch.Converters.IndexFieldIDValueConverter, Sitecore.ContentSearch"/>
</converter>
<converter handlesType="Sitecore.ContentSearch.SitecoreItemUniqueId, Sitecore.ContentSearch" typeConverter="Sitecore.ContentSearch.Converters.IndexFieldSitecoreItemUniqueIDValueConverter, Sitecore.ContentSearch">
<param type="Sitecore.ContentSearch.Converters.IndexFieldItemUriValueConverter, Sitecore.ContentSearch"/>
</converter>
<converter handlesType="Sitecore.Data.ItemUri, Sitecore.Kernel" typeConverter="Sitecore.ContentSearch.Converters.IndexFieldItemUriValueConverter, Sitecore.ContentSearch"/>
<converter handlesType="Sitecore.Globalization.Language, Sitecore.Kernel" typeConverter="Sitecore.ContentSearch.Converters.IndexFieldLanguageValueConverter, Sitecore.ContentSearch"/>
<converter handlesType="System.Globalization.CultureInfo" typeConverter="Sitecore.ContentSearch.Converters.IndexFieldCultureInfoValueConverter, Sitecore.ContentSearch"/>
<converter handlesType="Sitecore.Data.Version, Sitecore.Kernel" typeConverter="Sitecore.ContentSearch.Converters.IndexFieldVersionValueConverter, Sitecore.ContentSearch"/>
<converter handlesType="Sitecore.Data.Database, Sitecore.Kernel" typeConverter="Sitecore.ContentSearch.Converters.IndexFieldDatabaseValueConverter, Sitecore.ContentSearch"/>
</converters>
</indexFieldStorageValueFormatter>

3 个答案:

答案 0 :(得分:1)

我认为配置已经破裂。

IndexFieldStorageValueFormatter无法找到合适的TypeConverter并回退到Convert.ChangeType。但是ID类没有任何转换操作符用于字符串,因此转换失败。

它应该找到一个合适的TypeConverter。它是从配置中收集的,默认情况下是Sitecore.ContentSearch.Lucene.DefaultIndexConfiguration。它应该有一个indexFieldStorageValueFormatter部分,在这种情况下,这条线特别适用:

<converter handlesType="Sitecore.Data.ID, Sitecore.Kernel"                                    typeConverter="Sitecore.ContentSearch.Converters.IndexFieldIDValueConverter, Sitecore.ContentSearch" />

确保contentsearch配置正确,并且您的自定义my_text_index不会覆盖此部分。

您可以使用dotPeek生成pdb文件并调试IndexFieldStorageValueFormatter但我认为您得出的结论是它没有适用于此类型的转换器。

答案 1 :(得分:0)

通常这意味着您错过了属性的转化属性。但它们都不是Id的。

看起来你的Person属性设置不正确。您不需要在末尾添加类型后缀。 Sitecore API将为您完成此任务。

所以将public class Person : SearchResultItem { [IndexField("firstname")] public string Firstname { get; set; } [IndexField("surname")] public string Surname { get; set; } } 类更改为:

.player {
    color: #000;
    background-color: rgba(40, 180, 40, 0.8);
    padding: 5px;
    border: 2px solid #000;
    border-radius: 50px;
    display: inline-block;
}

还要确保这些字段不是CMS中的ID字段。

答案 2 :(得分:0)

将索引中的字符串转换为Sitecore guid时,基类SearchResultItem中出现此问题。

这将指向itemid字段的索引中的一些流氓数据。首先要检查您的Search.log文件以检查正在运行的实际查询,然后使用Luke运行查询以查看结果。

索引中的条目可能包含一个不属于Sitecore Guid格式的字段(__group)。