Sitecore SXA – Faceted search on a GUID field

Within SXA it’s really easy to add a new facet, however this will only work on fields that contain string values. If the field contains a GUID value, it will just display the GUID’s as the facet value, which renders it pretty much useless.

If we want to make the facet output the string values instead of GUID values, we will need to store the string value in the Solr index as well. Luckily there’s an really easy way to do this. All you need to do is to add a config file to add a computed field, and change your facet to filter on the newly added field.

For the example below, please envision that you have a field called Categories, which is a TreeList where you can select one or more categories.

<?xml version="1.0" encoding="utf-8"?>
<configuration xmlns:patch="http://www.sitecore.net/xmlconfig/" xmlns:search="http://www.sitecore.net/xmlconfig/search/" xmlns:role="http://www.sitecore.net/xmlconfig/role/">
  <sitecore role:require="Standalone or ContentManagement or ContentDelivery" search:require="solr">
    <contentSearch>
      <indexConfigurations>
        <defaultSolrIndexConfiguration type="Sitecore.ContentSearch.SolrProvider.SolrIndexConfiguration, Sitecore.ContentSearch.SolrProvider">
          <documentOptions type="Sitecore.ContentSearch.SolrProvider.SolrDocumentBuilderOptions, Sitecore.ContentSearch.SolrProvider">
            <fields hint="raw:AddComputedIndexField">
              <field fieldName="categoriesreference" type="Sitecore.XA.Foundation.Search.ComputedFields.ResolvedLinks, Sitecore.XA.Foundation.Search" referenceField="categories" contentField="title" returnType="stringCollection" />
            </fields>
          </documentOptions>
        </defaultSolrIndexConfiguration>
      </indexConfigurations>
    </contentSearch>
  </sitecore>
</configuration>

As you can see in the example above, we’ve added a new Computed field to our solr index. This field uses the Sitecore.XA.Foundation.Search.ComputedFields.ResolvedLinks type, that loops through all the GUIDS that are defined in the referenceField, retrieves the item that belongs to the guid, and then takes the value from the field that is defined in contentField to get the human readable value.

We can now use the field that is defined as fieldName within our facet. In this example, that would be categoriesreference.

Leave a Reply

Your email address will not be published.