0

In Sitecore, how can I define index field names on derived SearchResultItem class' properties other than [IndexField] attribute?

I'm trying to use the same interfaces I use for Glass.Mapper model definitions, and it already contains [SitecoreField] attribute on properties which define the Sitecore field name (and therefore the index field name).

Thank you!

Zenima
  • 380
  • 2
  • 14

3 Answers3

1

I would check out this project: https://github.com/cardinal252/Lucinq.Sitecore

It links Glass.Mapper and the Lucene index.

Mike

Michael Edwards
  • 6,308
  • 6
  • 44
  • 75
0

I don't think thats possible, since 'IndexField' and 'SitecoreField' represent different things, 'IndexField' attribute sets the name of the index field, for example, 'IndexField("_id")', 'IndexField("_language")' represent the id and language fields names inside lucene document.

Now, Sitecore by default, store the index names of all fields by it's name in Sitecore, for example, a field called 'Content' will be stored inside lucene document as 'content' by default. which you can change to something else if you want.

SitecoreField attribute represent the actual name of a field for an item, so that glass can map that field value into the property.

Bottom line, you just need to specify IndexField and SitecoreField on each property in your class, since each attribute works differently

Ahmed Okour
  • 2,392
  • 1
  • 16
  • 31
  • Yes, they are not necessary the same, but by default the Lucene document fields is named as the sitecore field (.ToLower().Replace(' ',_'), but since this conversion is done automatically, I'd end up having IndexField and SitecoreField attributes with the same parameter. So basicly I'm looking for and entry point to add mapping programmatically. Alternatively, I tried to look for how to derive a custom mapper and use it for this, but I couldn't find it - there is just too much code and since it's opened with reflector, there are no comments to describe the behavour. – Zenima Sep 06 '14 at 11:24
0

I believe you could change Sitecore Glass Mapper SitecoreFieldAttribute to accomplish that.

You could implement the interface IIndexFieldNameFormatterAttribute on the Glass MapperSitecoreFieldAttribute.

This interface you will find in the Sitecore.ContentSearch.Linq.dll and it looks like that:

namespace Sitecore.ContentSearch
{
    public interface IIndexFieldNameFormatterAttribute : _Attribute
    {
        string GetIndexFieldName(string fieldName);

        string GetTypeFieldName(string fieldName);
    }
}

Your implementation would be that, I pasted only the interface methods here.

namespace Glass.Sitecore.Mapper.Configuration.Attributes
{
    /// <summary>
    /// Used to populate the property with data from a Sitecore field
    /// </summary>
    public class SitecoreFieldAttribute: AbstractSitecorePropertyAttribute, IIndexFieldNameFormatterAttribute 
    {
        public string GetIndexFieldName(string fieldName)
        {
            return this.FieldName;
        }

        public string GetTypeFieldName(string fieldName)
        {
            return fieldName;
        }

I haven`t test it, but as I could see Sitecore Linq rely on this interface to find the fields names. You can investigate it yourself, but here is the piece of code which made me deduce that:

var variable = (from p in (IEnumerable<PropertyInfo>)typeof(TItem).GetProperties()
                select new { Property = p, Attribute = (IIndexFieldNameFormatterAttribute)p.GetCustomAttributes().FirstOrDefault<Attribute>((Attribute a) => a is IIndexFieldNameFormatterAttribute) }).FirstOrDefault((p) => p.Attribute != null);
            if (variable != null && variable.Attribute.GetIndexFieldName(variable.Property.Name) == this.FieldName)
            {
                property = variable.Property;
            }

Hope it helps..

RobertoBr
  • 1,781
  • 12
  • 22