3

I'm trying to use SI4T to index the value of a Keyword Metadata field into Solr. I've been using SI4T to index DCPs (which I posted a question about not so long ago here). The DCPs are going into Solr fine, but I'm not too sure how to check if a Keyword field contains Metadata only to specifically index a certain field. I kind of know what options I have but what I have tried currently doesn't work.

I don't think (looking at the SI4T wiki) that I can access Metadata from my SearchIndexCustomFieldMap field in my Component Template? I did try doing this but it didn't work, no errors just didn't index the Keyword Metadata field.

I have had a look at the ProcessCustomField method in the FieldsProcessor class which seems to check for a KeywordField and if one is found it indexes it's Id, code snippet of this is shown below:

 public virtual void ProcessCustomField(ItemField field, FieldProcessorSettings settings = null)
    {
        IndexField targetField = settings.CustomFieldTarget(field.Name);
        IList<string> values = new List<string>();
        if (field is KeywordField)
        {
            values = ((KeywordField)field).Values.Select(k => k.Id.ItemId.ToString()).ToList();
        }
        else if (field is ComponentLinkField)
        {
            values = ((ComponentLinkField)field).Values.Select(c => c.Id.ItemId.ToString()).ToList();
        }
        else if (field is DateField)
        {
            values = ((DateField)field).Values.Select(d => d.ToString("o") + "Z").ToList();
        }
        else if (field is NumberField)
        {
            values = ((NumberField)field).Values.Select(n => n.ToString()).ToList();
        }
        else if (field is TextField)
        {
            values = ((TextField)field).Values;
        }
        AddToData(values, (field is XhtmlField), targetField);
    }

Now what I was trying to do with the above is get the Xml of the Keyword Metadata and add it's value to values. But I'm not sure if this is the right approach to take?

We are using Tridion 2011 SP1 HR1.

2 Answers 2

3

The best aproach to achieve this is by extending the TemplateBase of SI4T (see http://www.tridiondeveloper.com/inside-si4t-search-integrations-controlling-what-gets-indexed - last chapter)

something like...

Component component = new Component(cp.Component.Id, engine.GetSession());
ItemFields metaFields = new ItemFields(component.Metadata, component.MetadataSchema);

if (metaFields.Contains(categoriefield) && !String.IsNullOrEmpty(metaFields[categoriefield].ToString())) 
{
    field = (ItemField)metaFields[categoriefield];
    IList<string> values = new List<string>();
    values = ((KeywordField)field).Values.Select(k => k.Title.ToString()).ToList();

    foreach (var value in values)
    {
        m_processor.SetCustomFieldValue("CUSTOMFIELDNAME", (object)value);
    }
} 
1

Thought I'd add an answer to this so you know what I did to resolve this.

if (field is KeywordField)
{
   values = ((KeywordField)field).Values.Select(k => k.Id.ItemId.ToString()).ToList();
}

Using the above conditional statement from my initial post, I noticed that field is (in this context) a KeywordField. From there I have access to the Metadata property where I can just use ItemFields to pull out the value of a specific Metadata field.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.