【问题标题】:Sitecore 8.1 computed index field for image only indexing alt text on solrSitecore 8.1 计算索引字段,用于仅在 solr 上索引 alt 文本的图像
【发布时间】:2016-06-20 14:01:12
【问题描述】:

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

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

谁能在这里阐明如何解决这个问题。

提前致谢。

P.S> 我在这里看到了 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

【问题讨论】:

    标签: indexing solr sitecore sitecore8 computed-field


    【解决方案1】:

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

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

    您应该将配置更改为

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

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

    您还应该注意,如果您重命名媒体项目,Solr 将拥有过时的 url,并且它不会自动重建所有引用文档。

    【讨论】:

      猜你喜欢
      • 2017-03-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多