1

前几天遇到了这个问题,但还没有找到解决这个问题的任何东西(来自谷歌搜索)。

我使用 Solr 作为我的索引引擎。我正在尝试在我的模板中索引图像字段。索引工作正常,但它没有索引媒体 URL(我从我的代码返回),而是索引图像的 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);
    }
}

任何人都可以在这里阐明如何解决此问题。

提前致谢。

PS> 我在这里看到了 John West 的帖子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

1 回答 1

4

您的代码看起来完全没问题。您的配置中有错误。

您将returnType字段设置为text这意味着 Solr 将对这些字段进行标记。这意味着 Solr 不会将值保留为一个字符串,而是会创建标记,以便将来进行全文搜索。

您应该将配置更改为

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

重新索引后,Solr 会将整个值保留为单个字符串。

另外你应该知道,如果你重命名一个媒体项目,Solr 将有过时的 url,它不会自动重建所有引用文档。

于 2016-03-07T07:48:00.550 回答