1
votes

I have created an index field to get/index an Image field from an item in Sitecore. However, the index return the Alternate text of the image, but this is not very useful..

I have tried to add this line in Lucene index configuration

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

I need to get the Image path, or Image ID, or the image tag, but i have no idea how to do this..

any help would be appreciated.

2
What version of Sitecore? That will define if it is a dynamic field or computed field. - Mark Ursino

2 Answers

2
votes

You could add a computed field. Here is John West's post about it. Below is a condensed example to get just the URL of the image.

Create a class that implements 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);
    }
}

Then, add a config include with something like this:

<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>

Note that the field name is hard coded above. I'm not sure if it is possible to pass that in as a parameter from the config. Sitecore seems to be creating separate classes for each of their computed fields and using inheritance to get reuse.

0
votes

I did something similar using scSearchContrib in 6.6.

Created a Dynamic Field to get the image 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;            
        }
    }

Referenced in the config file as :-

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

You should be able to do something similar in 7.0.