Quantcast
Channel: briancaos – Brian Pedersen's Sitecore and .NET Blog
Viewing all articles
Browse latest Browse all 276

Sitecore SOLR Search in multiselect lists fails unless you add the field to the defaultSolrIndexConfiguration config

$
0
0

So I have this multiselect field in Sitecore:

Sitecore Treelist field
Sitecore Treelist Field

The field is a treelist field, but this article works for all multiselect fields, treelist, checklist, multilist, etc.

Treelist field
Treelist field

So how do you return the field as a list of Sitecore ID’s from the SOLR index?

STEP 1: CREATE A MODEL CLASS IN SITECORE

Your model class models the item to be returned from the search. I’m just showing a partial section of the class, yours will have all the other fields defined as well.

  [Serializable]
  public class MyClass
  {
    // define some fields

    // This is our field in question:
    private IEnumerable<ID> _tradeLabels;

    // The field is protected against null references
    // using a private variable. You don't need this
    // if your code allows the value to be null.
    [IndexField("AdvertTradeLabels")]
    [TypeConverter(typeof(IndexFieldEnumerableConverter))]
    public IEnumerable<ID> TradeLabels
    {
      {
        if (_tradeLabels != null)
          return _tradeLabels;

        return Enumerable.Empty<ID>();
      }
      set
      {
        _tradeLabels = value;
      }
    }

    // define more fields
  }

STEP 2: EXTEND THE defaultSolrIndexConfiguration SECTION OF THE Sitecore.ContentSearch.Solr.DefaultIndexConfiguration.config FILE

The field will not automatically be mapped to an array if Sitecore ID’s. You need to define it in the Sitecore.ContentSearch.Solr.DefaultIndexConfiguration.config file.

The field needs to be defined as returning a guidCollection. It does not in fact return a list of GUID’s, but a list of Sitecore ID’s.

<?xml version="1.0" encoding="utf-8" ?>
<configuration xmlns:x="http://www.sitecore.net/xmlconfig/" xmlns:role="http://www.sitecore.net/xmlconfig/role/" xmlns:search="http://www.sitecore.net/xmlconfig/search/" xmlns:exmEnabled="http://www.sitecore.net/xmlconfig/exmEnabled/">
  <sitecore exmEnabled:require="yes" search:require="Solr">
    <contentSearch>
      <indexConfigurations>
        <defaultSolrIndexConfiguration>
          <fieldMap>
            <fieldNames>
              <field fieldName="AdvertTradeLabels" returnType="guidCollection" />
            </fieldNames>
          </fieldMap>
        </defaultSolrIndexConfiguration>
      </indexConfigurations>
    </contentSearch>
  </sitecore>
</configuration>

STEP 3: PERFORM THE SEARCH

Your field will now be part of the search result when doing a search.

public IEnumerable<MyClass> Get(Expression<Func<MyClass, bool>> predicate)
{
  using (IProviderSearchContext context = ContentSearchManager
    .GetIndex("sitecore_master_index")
    .CreateSearchContext(SearchSecurityOptions.DisableSecurityCheck))
  {
    return context.GetQueryable<MyClass>()
      .Where(predicate).ToList();
  }
}

You can also access it if you wish to filter using predicates:

public IEnumerable<MyClass> GetWithTradeLabel(ID tradeLabel)
{
  var hasTradeLabel = PredicateBuilder.True<MyClass>().And(advert => advert.TradeLabels.Contains(tradeLabel));
  return Get(hasTradeLabel);
}

FINAL NOTES:

For an easier approach to SOLR Content Searching in Sitecore, I recommend that you use a base class for all of your SOLR model classes. This makes searching so much easier. Read more here:

Sitecore ContentSearch – Get items from SOLR or Lucene – A base class implementation

MORE TO READ:


Viewing all articles
Browse latest Browse all 276

Trending Articles