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
Looking in SOLR schema shows sxatags indexed as 'sxatags_sm'
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
Questions
- Why is this still looking up 'sxatags' when I have defined the field as 'sxatags_sm' ?
- Is this a valid approach for looking up related item tags?
- How do I fix it!