2

Sitecore 9.3 with SXA. I'm trying to utilise sxa tags from the inherited _Taggable template to programmatically find related items with similar tags to the context item.

That SXA template is /sitecore/templates/Foundation/Experience Accelerator/Taxonomy/_Taggable

which looks like this

Sxa _Taggable

Looking in SOLR schema shows sxatags indexed as 'sxatags_sm'

sxatags schema

I have an search class inheriting from SearchResultItem.

public class EventSearchResultItem : SearchResultItem
    {
        [Sitecore.ContentSearch.IndexField("eventstartdatetime")]
        public DateTime EventStartDate { get; set; }

        [Sitecore.ContentSearch.IndexField("sxatags_sm")]
        public virtual IEnumerable<ID> Tags { get; set; }
    }

I'm using the following predicate setup to locate future events with similar tags.


                var contentStartItem = Sitecore.Context.Database.GetItem(Sitecore.Context.Site.ContentStartPath);

                var tagsPredicate = PredicateBuilder.True<EventSearchResultItem>(); 
                Sitecore.Data.Fields.MultilistField multiListField = Sitecore.Context.Item.Fields[Constants.Event.Fields.TAGS_ID];
                if (multiListField != null)
                {
                    foreach (ID id in multiListField.TargetIDs)
                    {
                        tagsPredicate = tagsPredicate.Or(x => x.Tags.Contains(id));
                    }
                }

                var mainPredicate = PredicateBuilder.True<EventSearchResultItem>();
                mainPredicate = mainPredicate.And(p => p.Paths.Contains(contentStartItem.ID));
                mainPredicate = mainPredicate.And(p => p.TemplateId == Constants.Event.Template.TEMPLATE_ID);
                mainPredicate = mainPredicate.And(p => p.EventStartDate > DateTime.Now.AddDays(-1)); 
                mainPredicate = mainPredicate.And(tagsPredicate);

                var indexResolver = ServiceLocator.ServiceProvider.GetService<IIndexResolver>();
                using (var searchContext = indexResolver.ResolveIndex(Sitecore.Context.Item).CreateSearchContext())
                {
                    var searchQuery = searchContext.GetQueryable<EventSearchResultItem>().Where(mainPredicate);
                    var results = searchQuery
                        .Take(numberOfResults)
                        .OrderByDescending(p => p.EventStartDate)
                        .GetResults();

                    relatedEvents.Events = string.Join("|", results.Hits.Select(x => x.Document.ItemId));
                }

When this runs, I'm seeing this in SOLR logs ( http://localhost:44011/solr/#/~logging )

org.apache.solr.common.SolrException: undefined field sxatags

solr out

Questions

  1. Why is this still looking up 'sxatags' when I have defined the field as 'sxatags_sm' ?
  2. Is this a valid approach for looking up related item tags?
  3. How do I fix it!
1
  • I am Stuck at the same place while building predicate based on Sxa Tags, Wondering did you found solution ?
    – Tabbuu
    Commented Jan 12, 2022 at 5:43

1 Answer 1

2

When the content search API resolves your Linq code to a Solr query, unless the field is a known named field, then Sitecore will attempt to correct any suffix on the IndexField attribute. This is how it solves the problem that Solr requires a defined schema, so Sitecore adds dynamic fields to the schema and uses the suffix to define the data type.

_sm is defined as a multivalued string

So you just need a couple of changes to your model definition. Change the IndexField to just sxatags and then you need to use a type converter to tell the ContentSearchApi how to convert the Solr data into your model. You can use the IndeFieldEnumerableConverter for this:

public class EventSearchResultItem : SearchResultItem
{
    [Sitecore.ContentSearch.IndexField("eventstartdatetime")]
    public DateTime EventStartDate { get; set; }

    [Sitecore.ContentSearch.IndexField("sxatags")]
    [TypeConverter(typeof (IndexFieldEnumerableConverter))]
    public virtual IEnumerable<ID> Tags { get; set; }
}

For an example of this pattern, look in the SearchResultItem.cs class at the Semantics property (Standard Sitecore Tags field). It is defined like this:

[IndexField("__semantics")]
[DataMember]
[TypeConverter(typeof (IndexFieldEnumerableConverter))]
public virtual IEnumerable<ID> Semantics { get; set; }
1
  • I've since noticed that sxatags is indexed as the Title value i.e. not the ID but the tag itself. Commented Oct 20, 2020 at 15:23

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.