【问题标题】:Sitecore Index for Image type图像类型的 Sitecore 索引
【发布时间】:2013-11-04 12:23:18
【问题描述】:

我创建了一个索引字段来从 Sitecore 中的项目获取/索引图像字段。 但是,索引返回图像的替代文本,但这不是很有用..

我已经尝试在 Lucene 索引配置中添加这一行

<field fieldName="restaurant_image" storageType="YES"  indexType="TOKENIZED"          vectorType="NO" boost="1f" type="System.String" settingType="Sitecore.ContentSearch.LuceneProvider.LuceneSearchFieldConfiguration, Sitecore.ContentSearch.LuceneProvider" />

我需要获取图像路径、图像 ID 或图像标签,但我不知道如何执行此操作..

任何帮助将不胜感激。

【问题讨论】:

  • 什么版本的 Sitecore?这将定义它是动态字段还是计算字段。

标签: image indexing lucene sitecore sitecore7


【解决方案1】:

您可以添加一个计算域。 Here is John West's post about it. 下面是一个仅获取图片 URL 的精简示例。

创建一个实现 Sitecore.ContentSearch.ComputedFields.IComputedIndexField 的类。

public class ImageIndexField : IComputedIndexField
{
    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["MyImageField"];

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

然后,添加一个类似这样的配置包含:

<sitecore>
    <contentSearch>
        <configuration type="Sitecore.ContentSearch.LuceneProvider.LuceneSearchConfiguration, Sitecore.ContentSearch.LuceneProvider">
            <defaultIndexConfiguration type="Sitecore.ContentSearch.LuceneProvider.LuceneIndexConfiguration, Sitecore.ContentSearch.LuceneProvider">
                <fields hint="raw:AddComputedIndexField">
                    <field fieldName="MyImageFieldUrl" storageType="YES" indexType="TOKENIZED">sc70.Search.ComputedFields.ImageUrlIndexField, sc70</field>
                </fields>
            </defaultIndexConfiguration>
        </configuration>
    </contentSearch>
</sitecore>

请注意,上面的字段名称是硬编码的。我不确定是否可以将其作为配置中的参数传递。 Sitecore 似乎正在为每个计算字段创建单独的类,并使用继承来获得重用。

【讨论】:

    【解决方案2】:

    我在 6.6 中使用 scSearchContrib 做了类似的事情。

    创建了一个动态字段来获取图片的url

    public class ImageUrlField : BaseDynamicField
        {
            public override string ResolveValue(Item item)
            {
                    FileField fileField = item.Fields["Image"];
    
                    var url = StringUtil.EnsurePrefix('/', MediaManager.GetMediaUrl(fileField.MediaItem));
    
                    return url;            
            }
        }
    

    在配置文件中引用为:-

    <dynamicField type="[NAMESPACE].ImageUrlField, [DLL]" name="image url" storageType="YES" indexType="UN_TOKENIZED" vectorType="NO" boost="1f"  />    
    

    你应该能够在 7.0 中做类似的事情。

    【讨论】:

    • 这可能对我有帮助,但我仍然很困惑。在我处理模板的类中,我创建了这样的索引: [IndexField("restaurant_image")] public string RestaurantImage { get;放;并在 DefaultIndexConfig 我添加了这一行: 如果我写的是 restaurant.RestaurantImage,我会得到替代文本..
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-06-20
    • 2015-07-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多