2017-05-24 85 views
1

我已经在Sitecore 7.2中实现了计算字段索引。然而,索引管理器现在是坏,我得到以下信息从'System.String'无效转换为'Sitecore.ContentSearch.ProviderIndexConfiguration'

Invalid cast from 'System.String' to 'Sitecore.ContentSearch.ProviderIndexConfiguration'. 
    Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. 

    Exception Details: System.InvalidCastException: Invalid cast from 'System.String' to 'Sitecore.ContentSearch.ProviderIndexConfiguration'. 

我已经实现了以下类为我的计算字段:

namespace Computed.Search 
{ 
    public class OfficeLocationComputedField : IComputedIndexField 
    { 
     public string FieldName { get; set; } 
     public string ReturnType { get; set; } 

     public object ComputeFieldValue(IIndexable indexable) 
     { 
      Assert.ArgumentNotNull(indexable, nameof(indexable)); 
      try 
      { 
       var result = new List<string>(); 

       var indexableItem = indexable as SitecoreIndexableItem; 
       if (indexableItem == null) 
        return null; 

       var item = (Item) indexableItem; 

       // Treelist fields map to the MultilistField type as per ~/App_Config/FieldTypes.config 
       MultilistField field = item?.Fields["officelocation"]; 
       if (field == null) 
        return null; 

       var items = field.GetItems(); 
       if (items == null || items.Length == 0) 
        return result; 

       foreach (var locationItem in items) 
       { 
        //result.Add(locationItem.Name); // if you want the name of the item 
        result.Add(locationItem.DisplayName); // if you want the display name of the item 
        //result.Add(locationItem["Title"]); // if you want the value of a field on the item 
       } 

       return result; 

      } 
      catch (Exception exception) 
      { 
       Log.Error($"An Error occured in custom computed index. {exception.Message}", exception); 
      } 
      return null; 
     } 
    } 
} 

和配置文件如下:

<?xml version="1.0" encoding="utf-8" ?> 
<configuration xmlns:patch="http://www.sitecore.net/xmlconfig/"> 
    <sitecore> 
    <contentSearch> 
     <indexConfigurations> 
     <defaultLuceneIndexConfiguration> 
      <fields hint="raw:AddComputedIndexField"> 
      <field fieldName="officelocation" storageType="YES" indexType="TOKENIZED">Computed.Search.OfficeLocationComputedField, Computed</field> 
      </fields> 
     </defaultLuceneIndexConfiguration> 
     </indexConfigurations> 
    </contentSearch> 
    </sitecore> 
</configuration> 

我不知道我在做什么错误或者我需要改变什么?

回答

2

Sitecore Invalid cast from 'System.String' to ... 90%情况下的异常是由配置错误引起的。

在你的情况下,Sitecore试图将字符串转换为ProviderIndexConfiguration。它是从ProviderIndexConfiguration继承的LuceneIndexConfiguration

它看起来像Sitecore无法匹配你的补丁文件内容与默认的Lucene索引配置。

确保在Sitecore.ContentSearch.Lucene.DefaultIndexConfiguration.config之后您的补丁文件名是按字母顺序排列的。

它的问题仍然存在,加type属性defaultLuceneIndexConfiguration标签并将其设置为type="Sitecore.ContentSearch.LuceneProvider.LuceneIndexConfiguration, Sitecore.ContentSearch.LuceneProvider":我已经创建,是按字母顺序排列的Sitecore.ContentSearch.Lucene.DefaultIndexConfiguration.conf前

<configuration xmlns:patch="http://www.sitecore.net/xmlconfig/"> 
    <sitecore> 
    <contentSearch> 
     <indexConfigurations> 
     <defaultLuceneIndexConfiguration type="Sitecore.ContentSearch.LuceneProvider.LuceneIndexConfiguration, Sitecore.ContentSearch.LuceneProvider"> 
      <fields hint="raw:AddComputedIndexField"> 
      <field fieldName="officelocation" storageType="YES" indexType="TOKENIZED">Computed.Search.OfficeLocationComputedField, Computed</field> 
      </fields> 
     </defaultLuceneIndexConfiguration> 
     </indexConfigurations> 
    </contentSearch> 
    </sitecore> 
</configuration> 
+0

我实施了您的建议,但不幸的是,问题依然存在。同样的例外。 – user843681

+0

你可以打开'/ sitecore/admin/showconfig.aspx'网址,看看'sitecore/contentSearch/indexConfigurations'标签的所有子节点是什么? –

+0

你也可以尝试添加你的领域到'Sitecore.ContentSearch.Lucene.DefaultIndexConfiguration.config'而不是自定义补丁文件只是为了测试,以确保它是一个配置问题,而不是代码问题。 –

0

年长的补丁文件ig仍在Website \ App_Config \ Include文件夹中。我忘了删除它。删除旧文件解决了问题。一切都在工作。

相关问题