I have a multilist field on article items called Categories
. In a module I am trying to filter based on one or more items in that multilist field. I can do this if I directly supply the name of the field in the index to use as follows:
var filterPredicate = PredicateBuilder.False<ArticleSearchResultItem>();
if (categories?.Any() == true)
{
foreach (var category in categories)
{
filterPredicate = filterPredicate
.Or(p => p["article_categories_sm"].Contains(category));
}
predicate = predicate.And(filterPredicate);
}
When I try to do this by adding article categories to my ArticleSearchResultItem
it seems that the value the property gets is always null.
public class ArticleSearchResultItem : SearchResultItem
{
[IndexField("publish_date")]
public DateTime PublishDate { get; set; }
[IndexField("article_categories")]
public IEnumerable<ID> ArticleCategories { get; set; }
}
...
var categoryIds = categories?.Where(ShortID.IsShortID)
.Select(cat => ShortID.Parse(cat).ToID());
if (categoryIds?.Any() == true)
{
var filterPredicate = PredicateBuilder.False<ArticleSearchResultItem>();
foreach (var category in categoryIds)
{
filterPredicate = filterPredicate
.Or(p => p.ArticleCategories.Contains(category));
}
predicate = predicate.And(filterPredicate);
}
I tested having a suffix of _sm
for the IndexField
attribute above ArticleCategories
and had the same results (all items were filtered when filtering by the same category that worked in the first block of code). Publish Date
is also a custom field however that seems to be working fine (in queries shown by the search log it shows up as publish_date_tdt
). When I look in the search logs it seems that using the property ArticleCategories
causes the query to use article_categories_t
which does not exist on my items. Could this be an issue with my schema or config? This instance is Sitecore.NET 8.2 (rev. 170614).
[IndexField("article_categories")]
? Have you tried to use a field without_
character in its name?