2016-03-07 41 views
1

前幾天遇到了這個問題,但找不到任何東西(從谷歌搜索),解決這個問題。Sitecore 8.1計算機索引字段僅用於索引圖像的索引alt文本

我正在使用Solr作爲我的索引引擎。我正在嘗試爲我的模板中的圖像字段編制索引。索引工作正常,但它不是索引媒體網址(我從我的代碼返回),而是索引圖像的ALT文本。如果ALT文本不存在,那麼它將索引媒體URL。 我有我的索引配置在一個單獨的文件。

我認爲在默認的Sitecore.ContentSearch.Solr.DefaultIndexConfiguration.config文件中的下面一行可能與我的配置有關。但是,我怎麼只覆蓋「main_image」字段。

<fieldReader fieldTypeName="image" fieldReaderType="Sitecore.ContentSearch.FieldReaders.ImageFieldReader, Sitecore.ContentSearch" /> 

下面是我的配置看起來像:

<?xml version="1.0" encoding="utf-8" ?> 
<configuration xmlns:patch="http://www.sitecore.net/xmlconfig/"> 
    <sitecore> 
    <myindex> 
     <indexConfigurations> 
     <mySolrIndexConfiguration ref="contentSearch/indexConfigurations/defaultSolrIndexConfiguration"> 
      <fields hint="raw:AddComputedIndexField"> 
       <field fieldName="main_image" returnType="text">My.Indexing.Namespace.MyMainImageIndexing,My.Indexing</field> 
       <field fieldName="thumbnail" returnType="text">My.Indexing.Namespace.MyThumbnailIndexing,My.Indexing</field> 
      </fields> 
     </mySolrIndexConfiguration> 
     </indexConfigurations> 
    </myindex> 
    </sitecore> 
</configuration> 

一個的實現看起來像下面(另一種是類似)

public class MyMainImageIndexing : IComputedIndexField 
{ 
    public string Parameters { get; set; } 
    public string FieldName { get; set; } 
    public string ReturnType { get; set; } 

    public object ComputeFieldValue(IIndexable indexable) 
    { 
     Assert.ArgumentNotNull(indexable, "indexable"); 
     var indexableItem = indexable as SitecoreIndexableItem; 

     if (indexableItem == null) 
     { 
      Log.Warn(string.Format("{0} : unsupported IIndexable type : {1}", this, indexable.GetType()), this); 
      return null; 
     } 

     ImageField img = indexableItem.Item.Fields["Main Image"]; 

     return (img == null || img.MediaItem == null) ? null : MediaManager.GetMediaUrl(img.MediaItem); 
    } 
} 

任何人都可以在這裏提供一些線索關於如何解決這個問題。

在此先感謝。

P.S>我在這裏看到約翰·韋斯特的後http://www.sitecore.net/de-de/learn/blogs/technical-blogs/john-west-sitecore-blog/posts/2013/05/sitecore-7-pre-render-image-fields.aspx

回答

4

你的代碼看起來完全沒問題。你的配置有一個錯誤。

您將您的字段的returnType設置爲text,這意味着Solr將標記這些字段。這意味着Solr不會將值保留爲一個字符串,而是會創建令牌,以便將來可以進行全文搜索。

你應該改變你的配置

<field fieldName="main_image" returnType="string">... 

後重新索引,Solr的將保持整體價值爲一個字符串。

另外你應該知道,如果你重命名一個媒體項目,Solr將有過時的URL,它不會自動重建所有的參考文檔。