0

I am creating computed index field for a Multilist field and want to return the items as String in a comma separated list.

However after indexing the Solr, an type text is getting appended to the result.

Configuration:

<field fieldName="category" returnType="string" />

I've also set the fully qualified name of the below class in the configuration.

enter image description here

Code I've written:

    public class CategoryMultilistField : IComputedIndexField
    {
        public string FieldName { get; set; }
        public string ReturnType { get; set; }

        public virtual object ComputeFieldValue(IIndexable indexable)
        {
            var scIndexable = indexable as SitecoreIndexableItem;

            if(scIndexable != null)
            {
                var item = (Item)scIndexable;

                if(item != null)
                {
                    MultilistField f = item.Fields["category"];                   

                    if (f != null)
                    {
                        var multilist = f.GetItems();
                        if (multilist == null || multilist.Length == 0)
                            return null;

                        return string.Join(",", multilist.Select(t => t[2]));
                    }
                }
            }
            return null;
        }
    }


  • Why do you call multilist.Select(t => t[2])? Shouldn't it be multilist.Select(t => t.Name) or something like that? – Marek Musielak Jun 4 at 20:20
  • @MarekMusielak - The name is coming as empty string. That's why I made this as t[2] which is giving me valid text. – SitecoreSXADeveloper Jun 5 at 4:19
  • Are you using SXA? – Gatogordo Jun 5 at 7:23
  • @Gatogordo - Yes, I am using SXA – SitecoreSXADeveloper Jun 5 at 10:33
3

As you are using SXA, why not use the "ootb" option to add extra fields that include data from a linked item:

The doc here is talking about facets, but the main purpose is actually creating an index field: https://doc.sitecore.com/users/sxa/93/sitecore-experience-accelerator/en/extend-search.html#UUID-3cbc7b12-9aed-f7d2-cd98-fcca239042ee_id__Extend_search_to_3

Add the following definition to your search configuration file:

<field fieldName="categorytext" returnType=”stringCollection” type="Sitecore.XA.Foundation.Search.ComputedFields.ResolvedLinks,     Sitecore.XA.Foundation.Search" referenceField="category" contentField="title"/>

where:

fieldName – defines the index field name where the value will be stored. Make sure this name differs from the reference field name.

returntype – defines the return type for your computed field.

referenceField – defines the field name on the indexed item.

contentField – defines the field on the referenced item from which the value will be retrieved during the indexing.

This way you don't need to write your own code, and this works for string collections. I didn't test the result if you want a string as returntype but I would give this a go...

| improve this answer | |
1

Rather than using the index on the item, try to use either the field name or the field ID to get exactly what you want.

return string.Join(",", multilist.Select(t => t.Fields["My Field Name"].Value).Where(x => !string.IsNullorEmpty(x)));

OR

var fieldID = new ID("{19C00FAC-DB88-4634-B002-94742C2DE715}");
// ... code here ...
return string.Join(",", multilist.Select(t => t.Fields[fieldID].Value).Where(x => !string.IsNullorEmpty(x)));

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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