3

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).

1
  • Have you checked what is sent to Solr if you use [IndexField("article_categories")]? Have you tried to use a field without _ character in its name?
    – Marek Musielak
    Nov 8, 2017 at 6:27

3 Answers 3

5

In case it works fine with p["article_categories_sm"].Contains(category) I suggest trying the following changes:

public class ArticleSearchResultItem : SearchResultItem
    {
        [IndexField("publish_date")]
        public DateTime PublishDate { get; set; }

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

I added the TypeConverter and made the field virtual.

If it doesn't work, trying without "_sm"

4
  • 1
    You shouldn't need the _sm suffix - the type converter will fix the issue.
    – Richard Seal
    Nov 7, 2017 at 20:58
  • 1
    @RichardSeal I know Solr uses the “_sm” postfix for string fields and we don’t need to use it in the field name. But Teeknow wrote that query with “_sm” works fine for him. I decided to propose trying both various. Nov 7, 2017 at 21:08
  • 3
    yes, a few times (and i don't really know why) I also had to specify list field name with _sm suffix because it was not added by converter.
    – Daniil
    Nov 8, 2017 at 14:11
  • For me, I have a field name hmc_filters I have seen both values i.e. hmc_filters_sm and hmc_filters in document.Fields Array. But my results are still not mapping this field in my searchresultitem class. Please suggest
    – AlokBhatt
    Jun 24, 2021 at 11:42
1

I would inspect the Search.log file to compare the queries that are actually sent to Solr for the working version and the non-working version, this approach has helped me many times in the past. You can also if need be paste the serialized query directly into the url of your solr instance:

https://localhost:8983/solr/#/core_name/query?q=SERIALIZED-QUERY

That way you can see what results get returned and tweak.

0

I uninstalled and reinstalled my solr instance and things started to work as expected using the code provided by @ArtsemPrashkovich's answer without the _sm suffix. I was running version 6.5 and switched it to be 5.1. According to the compatibility chart solr 6.5 was not supported by default for the version of Sitecore I was using: https://kb.sitecore.net/articles/227897.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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