Sitecore 如何解决 Sitecore 搜索中从 'System.String' 到 'Sitecore.Data.ID' 的错误转换

标签 sitecore sitecore7.2 sitecore-lucene

我正在使用 sitecore 搜索 API,但我收到异常 “异常详细信息:System.InvalidCastException:从 'System.String' 到 'Sitecore.Data.ID' 的转换无效。”行:foreach(var 查询结果)。我正在使用以下代码:

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;
}

}

这是“Person”类

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>

最佳答案

我认为配置已损坏。

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" />

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

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

关于Sitecore 如何解决 Sitecore 搜索中从 'System.String' 到 'Sitecore.Data.ID' 的错误转换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32680601/

相关文章:

asp.net - 带有集成管道的 IIS 7.5 中的页面 404,使用经典管道很好

javascript - SheerResponse 未关闭

sitecore - 如何使用下拉菜单创建 Sitecore 功能区按钮?

Sitecore 博客评论审核已启用,但评论会在批准之前发布

sitecore - 如何使用 Sitecore Lucene 搜索的谓词生成器

使用 NGram 自动完成 Sitecore 和 Lucene 搜索

indexing - Sitecore 刷新索引

asp.net-mvc - Sitecore 搜索方面和计算字段

powershell - Sitecore powershell 控制台不显示状态字段