Sitecore如何解决从';System.String';至';Sitecore.Data.ID';在Sitecore中搜索

Sitecore如何解决从';System.String';至';Sitecore.Data.ID';在Sitecore中搜索,sitecore,sitecore7.2,sitecore-lucene,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<Pers

我正在使用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;
}

}
这是堆栈跟踪:

[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
[InvalidCastException:从'System.String'到'Sitecore.Data.ID'的转换无效。]
System.Convert.DefaultToType(IConvertible值,类型targetType,IFormatProvider提供程序)+14116726
System.Convert.ChangeType(对象值、类型转换类型、IFormatProvider提供程序)+14116814
Sitecore.ContentSearch.Converters.IndexFieldStorage ValueFormatter.ReadFromIndexStorage(对象indexValue,类型destinationType)+609
[InvalidCastException:无法将System.String类型的值转换为目标类型Sitecore.Data.ID:从'System.String'转换为'Sitecore.Data.ID'无效。]
Sitecore.ContentSearch.Converters.IndexFieldStorage ValueFormatter.ReadFromIndexStorage(对象indexValue,类型destinationType)+869
Sitecore.ContentSearch.DocumentTypeMapInfo.SetProperty(对象目标,字符串propertyName,字符串documentFieldName,对象值)+172
[InvalidCastException:无法将索引文档字段映射到Starco.ISK.Website.Search.PersonSearchItem类型上的属性“ItemId”:无法将System.String类型的值转换为目标类型Sitecore.Data.ID:从“System.String”转换为“Sitecore.Data.ID”无效。]
Sitecore.ContentSearch.DocumentTypeMapInfo.SetProperty(对象目标,字符串propertyName,字符串documentFieldName,对象值)+360
Sitecore.ContentSearch.DefaultDocumentMapper`1.MapFieldValuesToType(IDictionary`2 fieldValues,TElement结果,DocumentTypeMapInfo DocumentTypeMapInfo)+792
Sitecore.ContentSearch.LuceneProvider.DefaultLuceneDocumentTypeMapper.ReadDocumentFields(文档文档,IEnumerable`1字段名,DocumentTypeMapInfo DocumentTypeMapInfo,IEnumerable`1虚拟字段处理器,远程通讯结果)+1249
Sitecore.ContentSearch.DefaultDocumentMapper`1.MapToType(t文档文档,SelectMethod SelectMethod,IEnumerable`1 VirtualFieldProcessor,SearchSecurityOptions securityOptions)+283
Sitecore.ContentSearch.LuceneProvider.d__8.MoveNext()+1577
Starco.ISK.Website.Search.Output.Page\加载(对象发送方,事件参数e)在e:\IIS Data\Starco\u 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(布尔includeStagesBeforeAsyncPoint,布尔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>

$(id)
$(id)
主人
/sitecore/content/Intranet
以下是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>

通常这意味着您的属性缺少转换属性。但是没有一个是身份证

看起来您的
IndexField
属性设置不正确。您不需要在末尾添加类型后缀。Sitecore API将为您做到这一点

因此,将
Person
类更改为:

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

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

尝试按以下方式更新:

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

  SearchResults<Person> results = query.GetResults();

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

}
使用(var context=ContentSearchManager.GetIndex(“我的文本索引”).CreateSearchContext())
{
IQueryable query=context.GetQueryable()。其中(p=>p.Firstname==“John”);
SearchResults=query.GetResults();
foreach(result in results)//我在这一行得到异常。
{
lbltest.Text=result.Name;
}
}

我认为配置被破坏了

IndexFieldStorageValueFormatter
找不到合适的类型转换器,返回Convert.ChangeType。但是ID类没有任何字符串转换运算符,因此转换失败

它应该找到一个合适的类型转换器。它是从配置中收集的,默认情况下位于
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文件并调试
IndexFieldStorage ValueFormatter
,但我认为您会得出相同的结论,即它没有可用于此类型的转换器。

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

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


索引中的条目可能有一个字段(_组),该字段的格式与Sitecore Guid的格式不符。

我也尝试过此解决方案,但仍然存在相同的异常;(SearchResultItem是标准的Sitecore类还是自定义的?请看一看
<converter handlesType="Sitecore.Data.ID, Sitecore.Kernel"                                    typeConverter="Sitecore.ContentSearch.Converters.IndexFieldIDValueConverter, Sitecore.ContentSearch" />