Sarika Niture
Sarika Niture

Reputation: 1

Modify index field in Solr

We have a requirement to search content by tag name using search box.

We are using SXA search box and search results component. We have a multilist field in page items called tags. Tags are already bound with facet checkboxes and that are working fine but we also want content to be searched from text boxes when tag names are supplied. We noticed search box searches from "sxacontent" field of Solr, so we decided to append comma separated tags in sxaconent field. Tags are appended correctly when we republish or rebuild index but later (may be because of any job?), tags are removed from sxacontent field. Can someone please help me understand why tags are removed like this? Or do I need to patch this code somewhere else?

Code added as below in patch and class:

<field fieldName="sxacontent" returnType="textCollection" templates="Content Library Card,Solution Page" set:type="ProjectName.Foundation.Search.ComputedFields.SxaTagsComputedField,ProjectName.Foundation.Search" indexFieldName="SxaTags">
</field>

**class: **

public class SxaTagsComputedField : AggregatedContent
{
    private const string _templatesAttribute = "templates";
    private const string _fieldNameAttribute = "indexFieldName";
    private const char _attributeListSeparator = ',';
    private readonly string[] _templates;
    private readonly string _fieldName;

    public SxaTagsComputedField(XmlNode configNode) : base(configNode)
    {
        _templates = XmlUtil.GetAttribute(_templatesAttribute, configNode).Split(_attributeListSeparator);
        _fieldName = XmlUtil.GetAttribute(_fieldNameAttribute, configNode);
    }
    public override object ComputeFieldValue(IIndexable indexable)
    {
        Item item = indexable as SitecoreIndexableItem;
        if (item == null)
        {
            return null;
        }
        if (_templates.Contains(item.TemplateName))
        {
            var fieldValue = item[_fieldName].TrimStart();
            if (!string.IsNullOrWhiteSpace(fieldValue))
            {
                var tagsField = (MultilistField)item.Fields[_fieldName];
                if (tagsField != null)
                {
                    var tagList = tagsField.GetItems().
                        Select(i => i[Sitecore.XA.Foundation.Search.Templates.Tag.Fields.Title]).ToList();
                    var sxaContent = new JavaScriptSerializer().Serialize(base.ComputeFieldValue(indexable));
                    StringBuilder sxacontentText = new StringBuilder(sxaContent);
                    sxacontentText?.Replace("[", string.Empty)?.Replace("\"", string.Empty)?.Replace("]", string.Empty);
                    return string.Join(_attributeListSeparator.ToString(), tagList) + _attributeListSeparator + sxacontentText;
                }
            }
            return base.ComputeFieldValue(indexable);
        }
        return base.ComputeFieldValue(indexable);
    }
}

It used to work in Sitecore 9.1.1 but since we upgraded Sitecore 10.3.1. We started facing this issue. The only change between two versions is patch config. We added below code in patch in earlier version.

<field fieldName="sxacontent" patch:instead="*[@fieldName='sxacontent']"  returnType="textCollection" templates="Content Library Card,Solution Page" indexFieldName="SxaTags">              ProjectName.Foundation.Search.ComputedFields.SxaTagsComputedField,ProjectName.Foundation.Search
</field>

We had to update this code because patch:instead in Sitecore 10.3.1

Tried above code and expecting sxacontent not to be modified

Upvotes: 0

Views: 28

Answers (0)

Related Questions